[Codeforces Round #247 (Div. 2)] B. Shower Line
16172 단어 codeforces
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks.
There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower.
Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the(2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one.
Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower.
We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower.
Input
The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i.
Assume that the students are numbered from 1 to 5.
Output
Print a single integer — the maximum possible total happiness of the students.
Sample test(s)
input
0 0 0 0 9
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
7 0 0 0 0
output
32
input
0 43 21 18 2
3 0 21 11 65
5 2 0 1 4
54 62 12 0 99
87 64 81 33 0
output
620
Note
In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals:(g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32
문제풀이: 대열에 다섯 명만 있고 직접 폭력 검색, 전체 배열, 각 배열은'행복감'을 합쳐서 max를 업데이트합니다.
코드:
1 #include<stdio.h>
2 #include<string.h>
3 #include<stdbool.h>
4 #include<stdlib.h>
5 #include<math.h>
6 #include<ctype.h>
7 #include<time.h>
8
9 #define rep(i,a,b) for(i=(a);i<=(b);i++)
10 #define red(i,a,b) for(i=(a);i>=(b);i--)
11 #define sqr(x) ((x)*(x))
12 #define clr(x,y) memset(x,y,sizeof(x))
13 #define LL long long
14
15 int i,j,n,m,max,
16 a[10][10];
17
18 char b[10],c[10];
19
20 bool f[7];
21
22 void pre()
23 {
24 clr(a,0);
25 clr(b,'\0');
26 clr(f,1);
27 max=-1;
28 }
29
30 int init()
31 {
32 int i,j;
33
34 rep(i,1,5)
35 rep(j,1,5)
36 scanf("%d",&a[i][j]);
37
38 return 0;
39 }
40
41 int check(char d[])
42 {
43 int i,s;
44 s=0;
45
46 s=a[d[0]-'0'][d[1]-'0']+
47 a[d[1]-'0'][d[0]-'0']+
48 a[d[2]-'0'][d[3]-'0']+
49 a[d[3]-'0'][d[2]-'0']+
50
51 a[d[1]-'0'][d[2]-'0']+
52 a[d[2]-'0'][d[1]-'0']+
53 a[d[3]-'0'][d[4]-'0']+
54 a[d[4]-'0'][d[3]-'0']+
55
56 a[d[2]-'0'][d[3]-'0']+
57 a[d[3]-'0'][d[2]-'0']+
58
59 a[d[3]-'0'][d[4]-'0']+
60 a[d[4]-'0'][d[3]-'0'];
61
62 if(s>max) max=s;
63
64 }
65
66 void dfs(int x)
67 {
68 int i;
69
70 if(x>5){
71 rep(i,0,4)
72 c[i]=b[i+1];
73 check(c);
74 return;
75 }
76
77 rep(i,1,5)
78 if(f[i]){
79 f[i]=0;
80 b[x]=i+48;
81 dfs(x+1);
82 f[i]=1;
83 }
84
85 }
86
87 int main()
88 {
89 pre();
90 init();
91 dfs(1);
92 printf("%d
",max);
93 return 0;
94 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Codeforces Round #715 Div. 2C The Sports Festival: 구간 DP전형구간 DP의 초전형. 이하, 0-indexed. 입력을 정렬하여 어디서나 시작하고 최적으로 좌우로 계속 유지하면 좋다는 것을 알 수 있습니다. {2000})$의 주문이 된다. 우선, 입력을 소트하여 n개의 요소를 $...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.