#include <iostream>
using namespace std;
const int maxn = 405;
int n;
char a[maxn];
int f[maxn][3][maxn][maxn];
int x[3][maxn];
int cnt[3];
int g[3][maxn][3];
int main()
{
ios_base::sync_with_stdio(NULL); cin.tie(nullptr);
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= n; ++i) {
if(a[i] == 'R') {
x[0][++cnt[0]] = i;
g[0][cnt[0]][1] = cnt[1];
g[0][cnt[0]][2] = cnt[2];
}
else if(a[i] == 'G') {
x[1][++cnt[1]] = i;
g[1][cnt[1]][0] = cnt[0];
g[1][cnt[1]][2] = cnt[2];
}
else {
x[2][++cnt[2]] = i;
g[2][cnt[2]][0] = cnt[0];
g[2][cnt[2]][1] = cnt[1];
}
}
for(int i = 0; i <= n; ++i) {
for(int j = 0; j <= 2; ++j) {
for(int p = 0; p <= n; ++p) {
for(int q = 0; q <= n; ++q) f[i][j][p][q] = 1e9;
}
}
}
for(int i = 0; i <= 2; ++i) {
if(cnt[i] == 0) continue;
f[1][i][0][0] = x[i][1] - 1;
}
for(int i = 2; i <= n; ++i) {
for(int j = 0; j <= 2; ++j) {
for(int p = 0; p <= i - 1; ++p) {
for(int q = 0; q + p <= i - 1; ++q) {
int j1 = 0;
int j2 = 1;
if(j == 0) {j1 = 1; j2 = 2;}
else if(j == 1) j2 = 2;
if(p > cnt[j1]) continue;
if(q > cnt[j2]) continue;
int tmp = i - p - q;
if(tmp > cnt[j]) continue;
int g1 = g[j][tmp][j1];
int g2 = g[j][tmp][j2];
if(j < j2) f[i][j][p][q] = min(f[i][j][p][q], f[i - 1][j1][tmp - 1][q] + max(0, g1 - p) + max(0, g2 - q));
else f[i][j][p][q] = min(f[i][j][p][q], f[i - 1][j1][q][tmp - 1] + max(0, g1 - p) + max(0, g2 - q));
if(j < j1) f[i][j][p][q] = min(f[i][j][p][q], f[i - 1][j2][tmp - 1][p] + max(0, g1 - p) + max(0, g2 - q));
else f[i][j][p][q] = min(f[i][j][p][q], f[i - 1][j2][p][tmp - 1] + max(0, g1 - p) + max(0, g2 - q));
}
}
}
}
int res = min(f[n][2][cnt[0]][cnt[1]], min(f[n][0][cnt[1]][cnt[2]], f[n][1][cnt[0]][cnt[2]]));
if(res == 1e9) cout << -1;
else cout << res;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1740 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1620 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
1 ms |
1620 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1736 KB |
Output is correct |
14 |
Correct |
1 ms |
1492 KB |
Output is correct |
15 |
Correct |
1 ms |
1620 KB |
Output is correct |
16 |
Correct |
1 ms |
1620 KB |
Output is correct |
17 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1740 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1620 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
1 ms |
1620 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1736 KB |
Output is correct |
14 |
Correct |
1 ms |
1492 KB |
Output is correct |
15 |
Correct |
1 ms |
1620 KB |
Output is correct |
16 |
Correct |
1 ms |
1620 KB |
Output is correct |
17 |
Correct |
1 ms |
1492 KB |
Output is correct |
18 |
Correct |
8 ms |
18644 KB |
Output is correct |
19 |
Correct |
9 ms |
18644 KB |
Output is correct |
20 |
Correct |
10 ms |
18612 KB |
Output is correct |
21 |
Correct |
8 ms |
18600 KB |
Output is correct |
22 |
Correct |
8 ms |
18644 KB |
Output is correct |
23 |
Correct |
8 ms |
18644 KB |
Output is correct |
24 |
Correct |
8 ms |
18644 KB |
Output is correct |
25 |
Correct |
8 ms |
18644 KB |
Output is correct |
26 |
Correct |
8 ms |
18644 KB |
Output is correct |
27 |
Correct |
9 ms |
18636 KB |
Output is correct |
28 |
Correct |
9 ms |
18644 KB |
Output is correct |
29 |
Correct |
9 ms |
18644 KB |
Output is correct |
30 |
Correct |
12 ms |
18640 KB |
Output is correct |
31 |
Correct |
9 ms |
18700 KB |
Output is correct |
32 |
Correct |
12 ms |
18608 KB |
Output is correct |
33 |
Correct |
9 ms |
18004 KB |
Output is correct |
34 |
Correct |
9 ms |
18004 KB |
Output is correct |
35 |
Correct |
8 ms |
17492 KB |
Output is correct |
36 |
Correct |
8 ms |
18124 KB |
Output is correct |
37 |
Correct |
7 ms |
16980 KB |
Output is correct |
38 |
Correct |
8 ms |
18644 KB |
Output is correct |
39 |
Correct |
9 ms |
18640 KB |
Output is correct |
40 |
Correct |
8 ms |
18004 KB |
Output is correct |
41 |
Correct |
8 ms |
18712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
461 ms |
769768 KB |
Output is correct |
3 |
Correct |
400 ms |
765968 KB |
Output is correct |
4 |
Correct |
403 ms |
769752 KB |
Output is correct |
5 |
Correct |
428 ms |
769732 KB |
Output is correct |
6 |
Correct |
398 ms |
769760 KB |
Output is correct |
7 |
Correct |
391 ms |
765984 KB |
Output is correct |
8 |
Correct |
390 ms |
766040 KB |
Output is correct |
9 |
Correct |
392 ms |
762088 KB |
Output is correct |
10 |
Correct |
387 ms |
769720 KB |
Output is correct |
11 |
Correct |
432 ms |
769796 KB |
Output is correct |
12 |
Correct |
94 ms |
206540 KB |
Output is correct |
13 |
Correct |
179 ms |
363104 KB |
Output is correct |
14 |
Correct |
251 ms |
525200 KB |
Output is correct |
15 |
Correct |
437 ms |
769784 KB |
Output is correct |
16 |
Correct |
395 ms |
769736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1740 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1620 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
1 ms |
1620 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1736 KB |
Output is correct |
14 |
Correct |
1 ms |
1492 KB |
Output is correct |
15 |
Correct |
1 ms |
1620 KB |
Output is correct |
16 |
Correct |
1 ms |
1620 KB |
Output is correct |
17 |
Correct |
1 ms |
1492 KB |
Output is correct |
18 |
Correct |
8 ms |
18644 KB |
Output is correct |
19 |
Correct |
9 ms |
18644 KB |
Output is correct |
20 |
Correct |
10 ms |
18612 KB |
Output is correct |
21 |
Correct |
8 ms |
18600 KB |
Output is correct |
22 |
Correct |
8 ms |
18644 KB |
Output is correct |
23 |
Correct |
8 ms |
18644 KB |
Output is correct |
24 |
Correct |
8 ms |
18644 KB |
Output is correct |
25 |
Correct |
8 ms |
18644 KB |
Output is correct |
26 |
Correct |
8 ms |
18644 KB |
Output is correct |
27 |
Correct |
9 ms |
18636 KB |
Output is correct |
28 |
Correct |
9 ms |
18644 KB |
Output is correct |
29 |
Correct |
9 ms |
18644 KB |
Output is correct |
30 |
Correct |
12 ms |
18640 KB |
Output is correct |
31 |
Correct |
9 ms |
18700 KB |
Output is correct |
32 |
Correct |
12 ms |
18608 KB |
Output is correct |
33 |
Correct |
9 ms |
18004 KB |
Output is correct |
34 |
Correct |
9 ms |
18004 KB |
Output is correct |
35 |
Correct |
8 ms |
17492 KB |
Output is correct |
36 |
Correct |
8 ms |
18124 KB |
Output is correct |
37 |
Correct |
7 ms |
16980 KB |
Output is correct |
38 |
Correct |
8 ms |
18644 KB |
Output is correct |
39 |
Correct |
9 ms |
18640 KB |
Output is correct |
40 |
Correct |
8 ms |
18004 KB |
Output is correct |
41 |
Correct |
8 ms |
18712 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
461 ms |
769768 KB |
Output is correct |
44 |
Correct |
400 ms |
765968 KB |
Output is correct |
45 |
Correct |
403 ms |
769752 KB |
Output is correct |
46 |
Correct |
428 ms |
769732 KB |
Output is correct |
47 |
Correct |
398 ms |
769760 KB |
Output is correct |
48 |
Correct |
391 ms |
765984 KB |
Output is correct |
49 |
Correct |
390 ms |
766040 KB |
Output is correct |
50 |
Correct |
392 ms |
762088 KB |
Output is correct |
51 |
Correct |
387 ms |
769720 KB |
Output is correct |
52 |
Correct |
432 ms |
769796 KB |
Output is correct |
53 |
Correct |
94 ms |
206540 KB |
Output is correct |
54 |
Correct |
179 ms |
363104 KB |
Output is correct |
55 |
Correct |
251 ms |
525200 KB |
Output is correct |
56 |
Correct |
437 ms |
769784 KB |
Output is correct |
57 |
Correct |
395 ms |
769736 KB |
Output is correct |
58 |
Correct |
485 ms |
769676 KB |
Output is correct |
59 |
Correct |
451 ms |
769792 KB |
Output is correct |
60 |
Correct |
457 ms |
762188 KB |
Output is correct |
61 |
Correct |
429 ms |
765944 KB |
Output is correct |
62 |
Correct |
420 ms |
769880 KB |
Output is correct |
63 |
Correct |
424 ms |
769696 KB |
Output is correct |
64 |
Correct |
479 ms |
769756 KB |
Output is correct |
65 |
Correct |
450 ms |
766084 KB |
Output is correct |
66 |
Correct |
459 ms |
758336 KB |
Output is correct |
67 |
Correct |
456 ms |
769788 KB |
Output is correct |
68 |
Correct |
462 ms |
765900 KB |
Output is correct |
69 |
Correct |
451 ms |
769688 KB |
Output is correct |
70 |
Correct |
439 ms |
769808 KB |
Output is correct |
71 |
Correct |
457 ms |
765900 KB |
Output is correct |
72 |
Correct |
402 ms |
765852 KB |
Output is correct |
73 |
Correct |
396 ms |
769700 KB |
Output is correct |