#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 405;
const int oo = 1e9;
vector <int> pos[3];
int dp[N][N][N][4];
int cnt[N][3];
int sz[3];
int n;
template <typename T>
void mini(T &a, T b) {
if (a > b)
a = b;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
char c; cin >> c;
int val = 2;
if (c == 'R')
val = 0;
if (c == 'G')
val = 1;
pos[val].push_back(i);
cnt[i][val]++;
}
for (int i = 1; i <= n; i++)
for (int j = 0; j < 3; j++)
cnt[i][j] += cnt[i - 1][j];
for (int i = 0; i < 3; i++)
sz[i] = pos[i].size();
for (int i = 0; i <= sz[0]; i++)
for (int j = 0; j <= sz[1]; j++)
for (int k = 0; k <= sz[2]; k++)
for (int pre = 0; pre < 3; pre++)
if (i || j || k)
dp[i][j][k][pre] = oo;
for (int i = 0; i <= sz[0]; i++)
for (int j = 0; j <= sz[1]; j++)
for (int k = 0; k <= sz[2]; k++)
for (int pre = 0; pre < 3; pre++) if (dp[i][j][k][pre] < oo) {
int cur = dp[i][j][k][pre];
int x = i + j + k + 1;
if (i < sz[0] && pre != 0) {
int posval = pos[0][i];
if (j > 0)
posval += max(0, cnt[pos[1][j - 1]][1] - cnt[pos[0][i]][1]);
if (k > 0)
posval += max(0, cnt[pos[2][k - 1]][2] - cnt[pos[0][i]][2]);
mini(dp[i + 1][j][k][0], cur + posval - x);
}
if (j < sz[1] && pre != 1) {
int posval = pos[1][j];
if (i > 0)
posval += max(0, cnt[pos[0][i - 1]][0] - cnt[pos[1][j]][0]);
if (k > 0)
posval += max(0, cnt[pos[2][k - 1]][2] - cnt[pos[1][j]][2]);
mini(dp[i][j + 1][k][1], cur + posval - x);
}
if (k < sz[2] && pre != 2) {
int posval = pos[2][k];
if (j > 0)
posval += max(0, cnt[pos[1][j - 1]][1] - cnt[pos[2][k]][1]);
if (i > 0)
posval += max(0, cnt[pos[0][i - 1]][0] - cnt[pos[2][k]][0]);
mini(dp[i][j][k + 1][2], cur + posval - x);
}
}
int ans = oo;
for (int i = 0; i < 3; i++)
ans = min(ans, dp[sz[0]][sz[1]][sz[2]][i]);
if (ans == oo)
ans = -1;
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
1 ms |
580 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
1 ms |
580 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
1860 KB |
Output is correct |
20 |
Correct |
2 ms |
2248 KB |
Output is correct |
21 |
Correct |
2 ms |
2380 KB |
Output is correct |
22 |
Correct |
2 ms |
1736 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
2 ms |
1612 KB |
Output is correct |
25 |
Correct |
2 ms |
3916 KB |
Output is correct |
26 |
Correct |
3 ms |
4172 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2372 KB |
Output is correct |
29 |
Correct |
2 ms |
2252 KB |
Output is correct |
30 |
Correct |
2 ms |
2244 KB |
Output is correct |
31 |
Correct |
2 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
2 ms |
3916 KB |
Output is correct |
34 |
Correct |
2 ms |
3532 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
1868 KB |
Output is correct |
37 |
Correct |
2 ms |
1740 KB |
Output is correct |
38 |
Correct |
2 ms |
2124 KB |
Output is correct |
39 |
Correct |
2 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
2 ms |
2124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
63 ms |
162840 KB |
Output is correct |
3 |
Correct |
63 ms |
162084 KB |
Output is correct |
4 |
Correct |
61 ms |
162896 KB |
Output is correct |
5 |
Correct |
61 ms |
162884 KB |
Output is correct |
6 |
Correct |
62 ms |
162880 KB |
Output is correct |
7 |
Correct |
63 ms |
161988 KB |
Output is correct |
8 |
Correct |
63 ms |
162000 KB |
Output is correct |
9 |
Correct |
62 ms |
161204 KB |
Output is correct |
10 |
Correct |
63 ms |
162912 KB |
Output is correct |
11 |
Correct |
62 ms |
162820 KB |
Output is correct |
12 |
Correct |
18 ms |
44000 KB |
Output is correct |
13 |
Correct |
29 ms |
77100 KB |
Output is correct |
14 |
Correct |
43 ms |
111296 KB |
Output is correct |
15 |
Correct |
67 ms |
162812 KB |
Output is correct |
16 |
Correct |
63 ms |
162840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
1 ms |
580 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
1860 KB |
Output is correct |
20 |
Correct |
2 ms |
2248 KB |
Output is correct |
21 |
Correct |
2 ms |
2380 KB |
Output is correct |
22 |
Correct |
2 ms |
1736 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
2 ms |
1612 KB |
Output is correct |
25 |
Correct |
2 ms |
3916 KB |
Output is correct |
26 |
Correct |
3 ms |
4172 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2372 KB |
Output is correct |
29 |
Correct |
2 ms |
2252 KB |
Output is correct |
30 |
Correct |
2 ms |
2244 KB |
Output is correct |
31 |
Correct |
2 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
2 ms |
3916 KB |
Output is correct |
34 |
Correct |
2 ms |
3532 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
1868 KB |
Output is correct |
37 |
Correct |
2 ms |
1740 KB |
Output is correct |
38 |
Correct |
2 ms |
2124 KB |
Output is correct |
39 |
Correct |
2 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
2 ms |
2124 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
63 ms |
162840 KB |
Output is correct |
44 |
Correct |
63 ms |
162084 KB |
Output is correct |
45 |
Correct |
61 ms |
162896 KB |
Output is correct |
46 |
Correct |
61 ms |
162884 KB |
Output is correct |
47 |
Correct |
62 ms |
162880 KB |
Output is correct |
48 |
Correct |
63 ms |
161988 KB |
Output is correct |
49 |
Correct |
63 ms |
162000 KB |
Output is correct |
50 |
Correct |
62 ms |
161204 KB |
Output is correct |
51 |
Correct |
63 ms |
162912 KB |
Output is correct |
52 |
Correct |
62 ms |
162820 KB |
Output is correct |
53 |
Correct |
18 ms |
44000 KB |
Output is correct |
54 |
Correct |
29 ms |
77100 KB |
Output is correct |
55 |
Correct |
43 ms |
111296 KB |
Output is correct |
56 |
Correct |
67 ms |
162812 KB |
Output is correct |
57 |
Correct |
63 ms |
162840 KB |
Output is correct |
58 |
Correct |
87 ms |
101312 KB |
Output is correct |
59 |
Correct |
97 ms |
118732 KB |
Output is correct |
60 |
Correct |
92 ms |
111044 KB |
Output is correct |
61 |
Correct |
94 ms |
108964 KB |
Output is correct |
62 |
Correct |
67 ms |
161172 KB |
Output is correct |
63 |
Correct |
67 ms |
161336 KB |
Output is correct |
64 |
Correct |
78 ms |
154476 KB |
Output is correct |
65 |
Correct |
84 ms |
144968 KB |
Output is correct |
66 |
Correct |
90 ms |
109508 KB |
Output is correct |
67 |
Correct |
89 ms |
106764 KB |
Output is correct |
68 |
Correct |
94 ms |
112192 KB |
Output is correct |
69 |
Correct |
92 ms |
109800 KB |
Output is correct |
70 |
Correct |
94 ms |
115540 KB |
Output is correct |
71 |
Correct |
90 ms |
107488 KB |
Output is correct |
72 |
Correct |
25 ms |
44796 KB |
Output is correct |
73 |
Correct |
3 ms |
3660 KB |
Output is correct |