#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
unordered_map<char, int> conv = {{'R', 0}, {'G', 1}, {'Y', 2}};
const int mxN = 402;
int dp[mxN][mxN][mxN][3];
int a[mxN];
vector<int> v[3];
int pref[3][mxN];
int suff[3][mxN];
int n;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
string temp; cin >> temp;
for (int i = 1; i <= n; i++) a[i] = conv[temp[i - 1]];
for (int i = 0; i < 3; i++) {
v[i] = {-1};
pref[i][0] = 0;
for (int j = 1; j <= n; j++) {
pref[i][j] = pref[i][j - 1];
if (a[j] == i) {
pref[i][j]++; v[i].pb(j);
}
}
suff[i][n + 1] = 0;
for (int j = n; j >= 1; j--) {
suff[i][j] = suff[i][j + 1];
if (a[j] == i) {
suff[i][j]++;
}
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n; k++) {
for (int l = 0; l < 3; l++) {
dp[i][j][k][l] = 1e8;
}
}
}
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int a = 0; a <= pref[0][n]; a++) {
for (int b = 0; b <= pref[1][n]; b++) {
for (int c = 0; c <= pref[2][n]; c++) {
if (a + b + c == 0) continue;
if (a) {
dp[a][b][c][0] = min(dp[a - 1][b][c][1], dp[a - 1][b][c][2]) + max(b - pref[1][v[0][a]], 0) + max(c - pref[2][v[0][a]], 0);
}
if (b) {
dp[a][b][c][1] = min(dp[a][b - 1][c][0], dp[a][b - 1][c][2]) + max(a - pref[0][v[1][b]], 0) + max(c - pref[2][v[1][b]], 0);
}
if (c) {
dp[a][b][c][2] = min(dp[a][b][c - 1][0], dp[a][b][c - 1][1]) + max(a - pref[0][v[2][c]], 0) + max(b - pref[1][v[2][c]], 0);
}
// cerr << a << ' ' << b << ' ' << c << ":\n";
// for (int i = 0; i < 3; i++) cerr << dp[a][b][c][i] << ' ';
// cerr << '\n';
}
}
}
int ans = 1e8;
for (int i = 0; i < 3; i++) ans = min(ans, dp[pref[0][n]][pref[1][n]][pref[2][n]][i]);
cout << (ans == 1e8 ? -1 : ans) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4564 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
5 ms |
20824 KB |
Output is correct |
5 |
Correct |
4 ms |
29244 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
5 ms |
29016 KB |
Output is correct |
9 |
Correct |
5 ms |
29020 KB |
Output is correct |
10 |
Correct |
4 ms |
29020 KB |
Output is correct |
11 |
Correct |
5 ms |
29020 KB |
Output is correct |
12 |
Correct |
4 ms |
29020 KB |
Output is correct |
13 |
Correct |
5 ms |
29020 KB |
Output is correct |
14 |
Correct |
4 ms |
26972 KB |
Output is correct |
15 |
Correct |
5 ms |
29020 KB |
Output is correct |
16 |
Correct |
5 ms |
29016 KB |
Output is correct |
17 |
Correct |
4 ms |
26972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4564 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
5 ms |
20824 KB |
Output is correct |
5 |
Correct |
4 ms |
29244 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
5 ms |
29016 KB |
Output is correct |
9 |
Correct |
5 ms |
29020 KB |
Output is correct |
10 |
Correct |
4 ms |
29020 KB |
Output is correct |
11 |
Correct |
5 ms |
29020 KB |
Output is correct |
12 |
Correct |
4 ms |
29020 KB |
Output is correct |
13 |
Correct |
5 ms |
29020 KB |
Output is correct |
14 |
Correct |
4 ms |
26972 KB |
Output is correct |
15 |
Correct |
5 ms |
29020 KB |
Output is correct |
16 |
Correct |
5 ms |
29016 KB |
Output is correct |
17 |
Correct |
4 ms |
26972 KB |
Output is correct |
18 |
Correct |
16 ms |
85848 KB |
Output is correct |
19 |
Correct |
14 ms |
85852 KB |
Output is correct |
20 |
Correct |
14 ms |
85852 KB |
Output is correct |
21 |
Correct |
14 ms |
85848 KB |
Output is correct |
22 |
Correct |
14 ms |
85852 KB |
Output is correct |
23 |
Correct |
15 ms |
85720 KB |
Output is correct |
24 |
Correct |
15 ms |
85852 KB |
Output is correct |
25 |
Correct |
14 ms |
85852 KB |
Output is correct |
26 |
Correct |
14 ms |
85852 KB |
Output is correct |
27 |
Correct |
14 ms |
85852 KB |
Output is correct |
28 |
Correct |
15 ms |
85852 KB |
Output is correct |
29 |
Correct |
14 ms |
85752 KB |
Output is correct |
30 |
Correct |
15 ms |
85852 KB |
Output is correct |
31 |
Correct |
14 ms |
85852 KB |
Output is correct |
32 |
Correct |
15 ms |
85848 KB |
Output is correct |
33 |
Correct |
13 ms |
85340 KB |
Output is correct |
34 |
Correct |
15 ms |
85340 KB |
Output is correct |
35 |
Correct |
15 ms |
85084 KB |
Output is correct |
36 |
Correct |
14 ms |
85420 KB |
Output is correct |
37 |
Correct |
15 ms |
84572 KB |
Output is correct |
38 |
Correct |
14 ms |
85852 KB |
Output is correct |
39 |
Correct |
14 ms |
85852 KB |
Output is correct |
40 |
Correct |
14 ms |
85340 KB |
Output is correct |
41 |
Correct |
14 ms |
85852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
368 ms |
761032 KB |
Output is correct |
3 |
Correct |
352 ms |
757340 KB |
Output is correct |
4 |
Correct |
344 ms |
760916 KB |
Output is correct |
5 |
Correct |
322 ms |
760976 KB |
Output is correct |
6 |
Correct |
321 ms |
761076 KB |
Output is correct |
7 |
Correct |
318 ms |
757588 KB |
Output is correct |
8 |
Correct |
325 ms |
757828 KB |
Output is correct |
9 |
Correct |
317 ms |
754188 KB |
Output is correct |
10 |
Correct |
327 ms |
761020 KB |
Output is correct |
11 |
Correct |
326 ms |
760912 KB |
Output is correct |
12 |
Correct |
84 ms |
279384 KB |
Output is correct |
13 |
Correct |
151 ms |
408616 KB |
Output is correct |
14 |
Correct |
204 ms |
546132 KB |
Output is correct |
15 |
Correct |
316 ms |
760916 KB |
Output is correct |
16 |
Correct |
318 ms |
761168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4564 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
5 ms |
20824 KB |
Output is correct |
5 |
Correct |
4 ms |
29244 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
5 ms |
29016 KB |
Output is correct |
9 |
Correct |
5 ms |
29020 KB |
Output is correct |
10 |
Correct |
4 ms |
29020 KB |
Output is correct |
11 |
Correct |
5 ms |
29020 KB |
Output is correct |
12 |
Correct |
4 ms |
29020 KB |
Output is correct |
13 |
Correct |
5 ms |
29020 KB |
Output is correct |
14 |
Correct |
4 ms |
26972 KB |
Output is correct |
15 |
Correct |
5 ms |
29020 KB |
Output is correct |
16 |
Correct |
5 ms |
29016 KB |
Output is correct |
17 |
Correct |
4 ms |
26972 KB |
Output is correct |
18 |
Correct |
16 ms |
85848 KB |
Output is correct |
19 |
Correct |
14 ms |
85852 KB |
Output is correct |
20 |
Correct |
14 ms |
85852 KB |
Output is correct |
21 |
Correct |
14 ms |
85848 KB |
Output is correct |
22 |
Correct |
14 ms |
85852 KB |
Output is correct |
23 |
Correct |
15 ms |
85720 KB |
Output is correct |
24 |
Correct |
15 ms |
85852 KB |
Output is correct |
25 |
Correct |
14 ms |
85852 KB |
Output is correct |
26 |
Correct |
14 ms |
85852 KB |
Output is correct |
27 |
Correct |
14 ms |
85852 KB |
Output is correct |
28 |
Correct |
15 ms |
85852 KB |
Output is correct |
29 |
Correct |
14 ms |
85752 KB |
Output is correct |
30 |
Correct |
15 ms |
85852 KB |
Output is correct |
31 |
Correct |
14 ms |
85852 KB |
Output is correct |
32 |
Correct |
15 ms |
85848 KB |
Output is correct |
33 |
Correct |
13 ms |
85340 KB |
Output is correct |
34 |
Correct |
15 ms |
85340 KB |
Output is correct |
35 |
Correct |
15 ms |
85084 KB |
Output is correct |
36 |
Correct |
14 ms |
85420 KB |
Output is correct |
37 |
Correct |
15 ms |
84572 KB |
Output is correct |
38 |
Correct |
14 ms |
85852 KB |
Output is correct |
39 |
Correct |
14 ms |
85852 KB |
Output is correct |
40 |
Correct |
14 ms |
85340 KB |
Output is correct |
41 |
Correct |
14 ms |
85852 KB |
Output is correct |
42 |
Correct |
2 ms |
6492 KB |
Output is correct |
43 |
Correct |
368 ms |
761032 KB |
Output is correct |
44 |
Correct |
352 ms |
757340 KB |
Output is correct |
45 |
Correct |
344 ms |
760916 KB |
Output is correct |
46 |
Correct |
322 ms |
760976 KB |
Output is correct |
47 |
Correct |
321 ms |
761076 KB |
Output is correct |
48 |
Correct |
318 ms |
757588 KB |
Output is correct |
49 |
Correct |
325 ms |
757828 KB |
Output is correct |
50 |
Correct |
317 ms |
754188 KB |
Output is correct |
51 |
Correct |
327 ms |
761020 KB |
Output is correct |
52 |
Correct |
326 ms |
760912 KB |
Output is correct |
53 |
Correct |
84 ms |
279384 KB |
Output is correct |
54 |
Correct |
151 ms |
408616 KB |
Output is correct |
55 |
Correct |
204 ms |
546132 KB |
Output is correct |
56 |
Correct |
316 ms |
760916 KB |
Output is correct |
57 |
Correct |
318 ms |
761168 KB |
Output is correct |
58 |
Correct |
335 ms |
760908 KB |
Output is correct |
59 |
Correct |
335 ms |
761236 KB |
Output is correct |
60 |
Correct |
331 ms |
754256 KB |
Output is correct |
61 |
Correct |
335 ms |
757696 KB |
Output is correct |
62 |
Correct |
322 ms |
760912 KB |
Output is correct |
63 |
Correct |
332 ms |
761424 KB |
Output is correct |
64 |
Correct |
329 ms |
760912 KB |
Output is correct |
65 |
Correct |
338 ms |
757752 KB |
Output is correct |
66 |
Correct |
326 ms |
750928 KB |
Output is correct |
67 |
Correct |
337 ms |
761216 KB |
Output is correct |
68 |
Correct |
331 ms |
757584 KB |
Output is correct |
69 |
Correct |
335 ms |
761040 KB |
Output is correct |
70 |
Correct |
334 ms |
760916 KB |
Output is correct |
71 |
Correct |
337 ms |
757760 KB |
Output is correct |
72 |
Correct |
319 ms |
757584 KB |
Output is correct |
73 |
Correct |
320 ms |
761016 KB |
Output is correct |