/**
GOT A FEELING THAT I"M GOING UNDER
**/
#include <bits/stdc++.h>
#define fr first
#define sc second
#define mk make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
using namespace std;
const int N = 405;
int dp[N][N][N][3], n, in[3][N], cnt[3], cn[3][2][N][N];
string s;
inline int min(int a, int b) {
return a < b ? a : b;
}
main(){
cin >> n >> s;
s = ' ' + s;
for (int i = 1; i <= n; i++) {
if (s[i] == 'R') {
cnt[0]++;
in[0][cnt[0]] = i;
}
else if (s[i] == 'G') {
cnt[1]++;
in[1][cnt[1]] = i;
}
else {
cnt[2]++;
in[2][cnt[2]] = i;
}
}
for (int i = 1; i <= cnt[0]; i++) {
for (int j = 1; j <= cnt[1]; j++) {
if ( in[1][j] > in[0][i] )
cn[0][0][i][j] = cn[0][0][i][j - 1] + 1;
}
for (int j = 1; j <= cnt[2]; j++) {
if ( in[2][j] > in[0][i] )
cn[0][1][i][j] = cn[0][1][i][j - 1] + 1;
}
}
for (int i = 1; i <= cnt[1]; i++) {
for (int j = 1; j <= cnt[0]; j++) {
if ( in[0][j] > in[1][i] )
cn[1][0][i][j] = cn[1][0][i][j - 1] + 1;
}
for (int j = 1; j <= cnt[2]; j++) {
if ( in[2][j] > in[1][i] )
cn[1][1][i][j] = cn[1][1][i][j - 1] + 1;
}
}
for (int i = 1; i <= cnt[2]; i++) {
for (int j = 1; j <= cnt[1]; j++) {
if ( in[1][j] > in[2][i] )
cn[2][0][i][j] = cn[2][0][i][j - 1] + 1;
}
for (int j = 1; j <= cnt[0]; j++) {
if ( in[0][j] > in[2][i] )
cn[2][1][i][j] = cn[2][1][i][j - 1] + 1;
}
}
for (int i = 0; i <= cnt[0]; i++)
for (int j = 0; j <= cnt[1]; j++)
for (int k = 0; k <= cnt[2]; k++)
for (int last = 0; last < 3; last++)
dp[i][j][k][last] = 1e9 + 7;
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (int i = 0; i <= cnt[0]; i++) {
for (int j = 0; j <= cnt[1]; j++) {
for (int k = 0; k <= cnt[2]; k++) {
for (int last = 0; last < 3; last++) {
if (i + 1 <= cnt[0] && last != 0)
dp[i + 1][j][k][0] = min(dp[i + 1][j][k][0], dp[i][j][k][last] + abs( (i + j + k + 1) - (in[0][i + 1] + cn[0][0][i + 1][j] + cn[0][1][i + 1][k]) ) );
if (j + 1 <= cnt[1] && last != 1)
dp[i][j + 1][k][1] = min(dp[i][j + 1][k][1], dp[i][j][k][last] + abs( (i + j + k + 1) - (in[1][j + 1] + cn[1][0][j + 1][i] + cn[1][1][j + 1][k]) ) );
if (k + 1 <= cnt[2] && last != 2){
dp[i][j][k + 1][2] = min(dp[i][j][k + 1][2], dp[i][j][k][last] + abs( (i + j + k + 1) - (in[2][k + 1] + cn[2][0][k + 1][j] + cn[2][1][k + 1][i]) ) );
}
}
}
}
}
if ( min( dp[cnt[0]][cnt[1]][cnt[2]][0], min( dp[cnt[0]][cnt[1]][cnt[2]][1], dp[cnt[0]][cnt[1]][cnt[2]][2] ) ) > 1e9 )
cout << -1 << endl;
else
cout << min( dp[cnt[0]][cnt[1]][cnt[2]][0], min( dp[cnt[0]][cnt[1]][cnt[2]][1], dp[cnt[0]][cnt[1]][cnt[2]][2] ) ) << endl;
}
Compilation message
joi2019_ho_t3.cpp:24:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
2560 KB |
Output is correct |
19 |
Correct |
3 ms |
2176 KB |
Output is correct |
20 |
Correct |
3 ms |
2432 KB |
Output is correct |
21 |
Correct |
4 ms |
2688 KB |
Output is correct |
22 |
Correct |
3 ms |
1920 KB |
Output is correct |
23 |
Correct |
3 ms |
2432 KB |
Output is correct |
24 |
Correct |
3 ms |
1792 KB |
Output is correct |
25 |
Correct |
4 ms |
4096 KB |
Output is correct |
26 |
Correct |
5 ms |
4352 KB |
Output is correct |
27 |
Correct |
4 ms |
3328 KB |
Output is correct |
28 |
Correct |
4 ms |
2432 KB |
Output is correct |
29 |
Correct |
4 ms |
2432 KB |
Output is correct |
30 |
Correct |
3 ms |
2432 KB |
Output is correct |
31 |
Correct |
3 ms |
2176 KB |
Output is correct |
32 |
Correct |
4 ms |
2560 KB |
Output is correct |
33 |
Correct |
5 ms |
4096 KB |
Output is correct |
34 |
Correct |
4 ms |
3840 KB |
Output is correct |
35 |
Correct |
4 ms |
2944 KB |
Output is correct |
36 |
Correct |
3 ms |
2048 KB |
Output is correct |
37 |
Correct |
4 ms |
1920 KB |
Output is correct |
38 |
Correct |
3 ms |
2304 KB |
Output is correct |
39 |
Correct |
4 ms |
2560 KB |
Output is correct |
40 |
Correct |
2 ms |
512 KB |
Output is correct |
41 |
Correct |
4 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
121 ms |
163348 KB |
Output is correct |
3 |
Correct |
137 ms |
162528 KB |
Output is correct |
4 |
Correct |
117 ms |
163604 KB |
Output is correct |
5 |
Correct |
118 ms |
163752 KB |
Output is correct |
6 |
Correct |
115 ms |
163680 KB |
Output is correct |
7 |
Correct |
131 ms |
162808 KB |
Output is correct |
8 |
Correct |
123 ms |
162808 KB |
Output is correct |
9 |
Correct |
121 ms |
162040 KB |
Output is correct |
10 |
Correct |
116 ms |
163676 KB |
Output is correct |
11 |
Correct |
117 ms |
163804 KB |
Output is correct |
12 |
Correct |
35 ms |
44416 KB |
Output is correct |
13 |
Correct |
57 ms |
77560 KB |
Output is correct |
14 |
Correct |
83 ms |
112088 KB |
Output is correct |
15 |
Correct |
117 ms |
163676 KB |
Output is correct |
16 |
Correct |
116 ms |
163704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
2560 KB |
Output is correct |
19 |
Correct |
3 ms |
2176 KB |
Output is correct |
20 |
Correct |
3 ms |
2432 KB |
Output is correct |
21 |
Correct |
4 ms |
2688 KB |
Output is correct |
22 |
Correct |
3 ms |
1920 KB |
Output is correct |
23 |
Correct |
3 ms |
2432 KB |
Output is correct |
24 |
Correct |
3 ms |
1792 KB |
Output is correct |
25 |
Correct |
4 ms |
4096 KB |
Output is correct |
26 |
Correct |
5 ms |
4352 KB |
Output is correct |
27 |
Correct |
4 ms |
3328 KB |
Output is correct |
28 |
Correct |
4 ms |
2432 KB |
Output is correct |
29 |
Correct |
4 ms |
2432 KB |
Output is correct |
30 |
Correct |
3 ms |
2432 KB |
Output is correct |
31 |
Correct |
3 ms |
2176 KB |
Output is correct |
32 |
Correct |
4 ms |
2560 KB |
Output is correct |
33 |
Correct |
5 ms |
4096 KB |
Output is correct |
34 |
Correct |
4 ms |
3840 KB |
Output is correct |
35 |
Correct |
4 ms |
2944 KB |
Output is correct |
36 |
Correct |
3 ms |
2048 KB |
Output is correct |
37 |
Correct |
4 ms |
1920 KB |
Output is correct |
38 |
Correct |
3 ms |
2304 KB |
Output is correct |
39 |
Correct |
4 ms |
2560 KB |
Output is correct |
40 |
Correct |
2 ms |
512 KB |
Output is correct |
41 |
Correct |
4 ms |
2432 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
121 ms |
163348 KB |
Output is correct |
44 |
Correct |
137 ms |
162528 KB |
Output is correct |
45 |
Correct |
117 ms |
163604 KB |
Output is correct |
46 |
Correct |
118 ms |
163752 KB |
Output is correct |
47 |
Correct |
115 ms |
163680 KB |
Output is correct |
48 |
Correct |
131 ms |
162808 KB |
Output is correct |
49 |
Correct |
123 ms |
162808 KB |
Output is correct |
50 |
Correct |
121 ms |
162040 KB |
Output is correct |
51 |
Correct |
116 ms |
163676 KB |
Output is correct |
52 |
Correct |
117 ms |
163804 KB |
Output is correct |
53 |
Correct |
35 ms |
44416 KB |
Output is correct |
54 |
Correct |
57 ms |
77560 KB |
Output is correct |
55 |
Correct |
83 ms |
112088 KB |
Output is correct |
56 |
Correct |
117 ms |
163676 KB |
Output is correct |
57 |
Correct |
116 ms |
163704 KB |
Output is correct |
58 |
Correct |
128 ms |
77604 KB |
Output is correct |
59 |
Correct |
136 ms |
97916 KB |
Output is correct |
60 |
Correct |
122 ms |
89160 KB |
Output is correct |
61 |
Correct |
122 ms |
86132 KB |
Output is correct |
62 |
Correct |
123 ms |
161656 KB |
Output is correct |
63 |
Correct |
128 ms |
161528 KB |
Output is correct |
64 |
Correct |
145 ms |
151396 KB |
Output is correct |
65 |
Correct |
150 ms |
139896 KB |
Output is correct |
66 |
Correct |
181 ms |
86776 KB |
Output is correct |
67 |
Correct |
109 ms |
83576 KB |
Output is correct |
68 |
Correct |
117 ms |
89720 KB |
Output is correct |
69 |
Correct |
119 ms |
86776 KB |
Output is correct |
70 |
Correct |
132 ms |
93924 KB |
Output is correct |
71 |
Correct |
117 ms |
85240 KB |
Output is correct |
72 |
Correct |
49 ms |
42872 KB |
Output is correct |
73 |
Correct |
8 ms |
4096 KB |
Output is correct |