#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
array<int, 255> colorCode;
const int iINF = 1e9;
const int maxN = 423;
int N, cnt[3], color[maxN], pos[3][maxN], pf[3][maxN];
int dp[maxN][maxN][maxN][3];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
colorCode['R'] = 0;
colorCode['G'] = 1;
colorCode['Y'] = 2;
cin >> N;
char buf;
for (int i = 1; i <= N; i++) {
cin >> buf;
color[i] = colorCode[buf];
}
#define R cnt[0]
#define G cnt[1]
#define Y cnt[2]
#define r lpos[0]
#define g lpos[1]
#define y lpos[2]
for (int i = 1; i <= N; i++) {
pos[color[i]][++cnt[color[i]]] = i;
for (int j = 0; j <= 2; j++) {
pf[j][i] = pf[j][i-1] + (color[i] == j);
}
}
// for (int j = 0; j <= 2; j++) {
// for (int i = 1; i <= N; i++) cout << pos[j][i] << ' ';
// cout << '\n';
// for (int i = 1; i <= N; i++) cout << pf[j][i] << ' ';
// cout << '\n';
// cout << '\n';
// }
// dp[r][g][y][lst] = number of swaps to the left that must be done
// to get r red, g green, and y yellow with last color lst
array<int, 3> lpos;
for (int n = 1; n <= N; n++) {
for (r = 0; r <= n; r++) {
for (g = 0; g <= n-r; g++) {
y = n-r-g;
for (int lst = 0; lst <= 2; lst++) {
if (!lpos[lst]) {
dp[r][g][y][lst] = iINF;
continue;
}
int prv = iINF;
lpos[lst]--;
for (int plst = 0; plst <= 2; plst++) {
if (plst == lst) continue;
prv = min(prv, dp[r][g][y][plst]);
}
lpos[lst]++;
int swaps = 0;
for (int i = 0; i <= 2; i++) {
if (i == lst) continue;
swaps += max(0, pf[i][pos[lst][lpos[lst]]] - lpos[i]);
}
dp[r][g][y][lst] = min(iINF, prv + swaps);
// fprintf(stderr, "dp[%d][%d][%d][%d] = %d\n", r, g, y, lst, dp[r][g][y][lst]);
}
}
}
}
int ans = iINF;
for (int lst = 0; lst <= 2; lst++) ans = min(ans, dp[R][G][Y][lst]);
if (ans == iINF) ans = -1;
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
956 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
872 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
932 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
956 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
872 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
932 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
4 ms |
8588 KB |
Output is correct |
19 |
Correct |
3 ms |
8528 KB |
Output is correct |
20 |
Correct |
5 ms |
8532 KB |
Output is correct |
21 |
Correct |
4 ms |
8532 KB |
Output is correct |
22 |
Correct |
4 ms |
8532 KB |
Output is correct |
23 |
Correct |
4 ms |
8532 KB |
Output is correct |
24 |
Correct |
3 ms |
8532 KB |
Output is correct |
25 |
Correct |
4 ms |
8532 KB |
Output is correct |
26 |
Correct |
4 ms |
8532 KB |
Output is correct |
27 |
Correct |
4 ms |
8532 KB |
Output is correct |
28 |
Correct |
4 ms |
8532 KB |
Output is correct |
29 |
Correct |
4 ms |
8532 KB |
Output is correct |
30 |
Correct |
4 ms |
8532 KB |
Output is correct |
31 |
Correct |
5 ms |
8532 KB |
Output is correct |
32 |
Correct |
4 ms |
8532 KB |
Output is correct |
33 |
Correct |
4 ms |
8332 KB |
Output is correct |
34 |
Correct |
4 ms |
8276 KB |
Output is correct |
35 |
Correct |
4 ms |
8020 KB |
Output is correct |
36 |
Correct |
3 ms |
8276 KB |
Output is correct |
37 |
Correct |
4 ms |
7764 KB |
Output is correct |
38 |
Correct |
4 ms |
8532 KB |
Output is correct |
39 |
Correct |
4 ms |
8532 KB |
Output is correct |
40 |
Correct |
4 ms |
8276 KB |
Output is correct |
41 |
Correct |
4 ms |
8508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
391 ms |
386992 KB |
Output is correct |
3 |
Correct |
367 ms |
384896 KB |
Output is correct |
4 |
Correct |
338 ms |
386956 KB |
Output is correct |
5 |
Correct |
359 ms |
386900 KB |
Output is correct |
6 |
Correct |
405 ms |
386936 KB |
Output is correct |
7 |
Correct |
372 ms |
384840 KB |
Output is correct |
8 |
Correct |
390 ms |
384868 KB |
Output is correct |
9 |
Correct |
335 ms |
383004 KB |
Output is correct |
10 |
Correct |
347 ms |
386892 KB |
Output is correct |
11 |
Correct |
451 ms |
386952 KB |
Output is correct |
12 |
Correct |
72 ms |
100784 KB |
Output is correct |
13 |
Correct |
118 ms |
179860 KB |
Output is correct |
14 |
Correct |
304 ms |
262120 KB |
Output is correct |
15 |
Correct |
387 ms |
386924 KB |
Output is correct |
16 |
Correct |
357 ms |
387052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
956 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
872 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
932 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
4 ms |
8588 KB |
Output is correct |
19 |
Correct |
3 ms |
8528 KB |
Output is correct |
20 |
Correct |
5 ms |
8532 KB |
Output is correct |
21 |
Correct |
4 ms |
8532 KB |
Output is correct |
22 |
Correct |
4 ms |
8532 KB |
Output is correct |
23 |
Correct |
4 ms |
8532 KB |
Output is correct |
24 |
Correct |
3 ms |
8532 KB |
Output is correct |
25 |
Correct |
4 ms |
8532 KB |
Output is correct |
26 |
Correct |
4 ms |
8532 KB |
Output is correct |
27 |
Correct |
4 ms |
8532 KB |
Output is correct |
28 |
Correct |
4 ms |
8532 KB |
Output is correct |
29 |
Correct |
4 ms |
8532 KB |
Output is correct |
30 |
Correct |
4 ms |
8532 KB |
Output is correct |
31 |
Correct |
5 ms |
8532 KB |
Output is correct |
32 |
Correct |
4 ms |
8532 KB |
Output is correct |
33 |
Correct |
4 ms |
8332 KB |
Output is correct |
34 |
Correct |
4 ms |
8276 KB |
Output is correct |
35 |
Correct |
4 ms |
8020 KB |
Output is correct |
36 |
Correct |
3 ms |
8276 KB |
Output is correct |
37 |
Correct |
4 ms |
7764 KB |
Output is correct |
38 |
Correct |
4 ms |
8532 KB |
Output is correct |
39 |
Correct |
4 ms |
8532 KB |
Output is correct |
40 |
Correct |
4 ms |
8276 KB |
Output is correct |
41 |
Correct |
4 ms |
8508 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
391 ms |
386992 KB |
Output is correct |
44 |
Correct |
367 ms |
384896 KB |
Output is correct |
45 |
Correct |
338 ms |
386956 KB |
Output is correct |
46 |
Correct |
359 ms |
386900 KB |
Output is correct |
47 |
Correct |
405 ms |
386936 KB |
Output is correct |
48 |
Correct |
372 ms |
384840 KB |
Output is correct |
49 |
Correct |
390 ms |
384868 KB |
Output is correct |
50 |
Correct |
335 ms |
383004 KB |
Output is correct |
51 |
Correct |
347 ms |
386892 KB |
Output is correct |
52 |
Correct |
451 ms |
386952 KB |
Output is correct |
53 |
Correct |
72 ms |
100784 KB |
Output is correct |
54 |
Correct |
118 ms |
179860 KB |
Output is correct |
55 |
Correct |
304 ms |
262120 KB |
Output is correct |
56 |
Correct |
387 ms |
386924 KB |
Output is correct |
57 |
Correct |
357 ms |
387052 KB |
Output is correct |
58 |
Correct |
419 ms |
386892 KB |
Output is correct |
59 |
Correct |
404 ms |
386892 KB |
Output is correct |
60 |
Correct |
374 ms |
383000 KB |
Output is correct |
61 |
Correct |
378 ms |
384968 KB |
Output is correct |
62 |
Correct |
364 ms |
386892 KB |
Output is correct |
63 |
Correct |
339 ms |
386984 KB |
Output is correct |
64 |
Correct |
416 ms |
386916 KB |
Output is correct |
65 |
Correct |
357 ms |
384900 KB |
Output is correct |
66 |
Correct |
354 ms |
380932 KB |
Output is correct |
67 |
Correct |
346 ms |
386912 KB |
Output is correct |
68 |
Correct |
354 ms |
384888 KB |
Output is correct |
69 |
Correct |
404 ms |
387024 KB |
Output is correct |
70 |
Correct |
439 ms |
386996 KB |
Output is correct |
71 |
Correct |
367 ms |
384828 KB |
Output is correct |
72 |
Correct |
438 ms |
384836 KB |
Output is correct |
73 |
Correct |
400 ms |
386940 KB |
Output is correct |