#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
typedef vector<array<int, 3>> va;
typedef vector<va> vva;
typedef vector<vva> vvva;
const int MXN = 405;
int N, n[3], M[256], ans;
string s;
vvva dp;
vector<int> ID[3];
array<int, 3> cnt[MXN];
void init() {
M['R'] = 0;
M['G'] = 1;
M['Y'] = 2;
}
int operator-(array<int, 3> arr1, array<int, 3> arr2) {
// cout << "---" << max(0LL, arr1[0] - arr2[0]) + max(0LL, arr1[1] - arr2[1]) + max(0LL, arr1[2] - arr2[2]) << ' ';
// return (arr1[0] + arr1[1] + arr1[2]) - (arr2[0] + arr2[1] + arr2[2]);
return max(0LL, arr1[0] - arr2[0]) + max(0LL, arr1[1] - arr2[1]) + max(0LL, arr1[2] - arr2[2]);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
init();
cin >> N >> s;
{
array<int, 3> br = {0, 0, 0};
for (int i = 0; i < N; i++) {
br[M[s[i]]]++;
ID[M[s[i]]].push_back(i);
cnt[i] = br;
}
}
for (int i = 0; i < 3; i++) n[i] = ID[i].size();
dp = vvva(n[0] + 1, vva(n[1] + 1, va(n[2] + 1)));
for (int i = 0; i <= n[0]; i++) {
for (int j = 0; j <= n[1]; j++) {
for (int k = 0; k <= n[2]; k++) {
array<int, 3> br = {i, j, k};
// cout << i << ' ' << j << ' ' << k << endl;
for (int l = 0; l < 3; l++) {
// cout << l << ' ';
if (i == 0 && j == 0 && k == 0) {
dp[i][j][k][l] = 0;
// cout << dp[i][j][k][l] << endl;
continue;
}
dp[i][j][k][l] = INT_MAX;
if (br[l] == 0) {
// cout << dp[i][j][k][l] << endl;
continue;
}
br[l]--;
for (int dl = 1; dl < 3; dl++) {
int tar = (l + dl) % 3;
// cout << 't' << tar << ' ';
// if (br[tar] == 0) continue;
dp[i][j][k][l] = min(dp[i][j][k][l], dp[br[0]][br[1]][br[2]][tar] + (cnt[ID[l][br[l]]] - br) - 1);
// cout << dp[i][j][k][l] << ' ';
}
// cout << dp[i][j][k][l] << endl;
br[l]++;
}
}
}
}
ans = INT_MAX;
for (int i = 0; i < 3; i++) {
ans = min(ans, dp[n[0]][n[1]][n[2]][i]);
}
cout << (ans == INT_MAX ? -1 : ans) << endl;
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int32_t main()':
joi2019_ho_t3.cpp:37:22: warning: array subscript has type 'char' [-Wchar-subscripts]
37 | br[M[s[i]]]++;
| ^
joi2019_ho_t3.cpp:38:22: warning: array subscript has type 'char' [-Wchar-subscripts]
38 | ID[M[s[i]]].push_back(i);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
456 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
2516 KB |
Output is correct |
3 |
Correct |
6 ms |
2508 KB |
Output is correct |
4 |
Correct |
4 ms |
2516 KB |
Output is correct |
5 |
Correct |
5 ms |
2516 KB |
Output is correct |
6 |
Correct |
4 ms |
2516 KB |
Output is correct |
7 |
Correct |
6 ms |
2496 KB |
Output is correct |
8 |
Correct |
4 ms |
2516 KB |
Output is correct |
9 |
Correct |
4 ms |
2500 KB |
Output is correct |
10 |
Correct |
4 ms |
2504 KB |
Output is correct |
11 |
Correct |
4 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
840 KB |
Output is correct |
13 |
Correct |
2 ms |
1364 KB |
Output is correct |
14 |
Correct |
3 ms |
1748 KB |
Output is correct |
15 |
Correct |
4 ms |
2516 KB |
Output is correct |
16 |
Correct |
4 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
456 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
4 ms |
2516 KB |
Output is correct |
44 |
Correct |
6 ms |
2508 KB |
Output is correct |
45 |
Correct |
4 ms |
2516 KB |
Output is correct |
46 |
Correct |
5 ms |
2516 KB |
Output is correct |
47 |
Correct |
4 ms |
2516 KB |
Output is correct |
48 |
Correct |
6 ms |
2496 KB |
Output is correct |
49 |
Correct |
4 ms |
2516 KB |
Output is correct |
50 |
Correct |
4 ms |
2500 KB |
Output is correct |
51 |
Correct |
4 ms |
2504 KB |
Output is correct |
52 |
Correct |
4 ms |
2516 KB |
Output is correct |
53 |
Correct |
2 ms |
840 KB |
Output is correct |
54 |
Correct |
2 ms |
1364 KB |
Output is correct |
55 |
Correct |
3 ms |
1748 KB |
Output is correct |
56 |
Correct |
4 ms |
2516 KB |
Output is correct |
57 |
Correct |
4 ms |
2516 KB |
Output is correct |
58 |
Correct |
124 ms |
57560 KB |
Output is correct |
59 |
Correct |
128 ms |
57672 KB |
Output is correct |
60 |
Correct |
114 ms |
57324 KB |
Output is correct |
61 |
Correct |
113 ms |
57940 KB |
Output is correct |
62 |
Correct |
12 ms |
6100 KB |
Output is correct |
63 |
Correct |
20 ms |
9816 KB |
Output is correct |
64 |
Correct |
60 ms |
29360 KB |
Output is correct |
65 |
Correct |
85 ms |
41812 KB |
Output is correct |
66 |
Correct |
119 ms |
57080 KB |
Output is correct |
67 |
Correct |
118 ms |
56984 KB |
Output is correct |
68 |
Correct |
121 ms |
57940 KB |
Output is correct |
69 |
Correct |
134 ms |
58328 KB |
Output is correct |
70 |
Correct |
115 ms |
58040 KB |
Output is correct |
71 |
Correct |
124 ms |
56676 KB |
Output is correct |
72 |
Correct |
37 ms |
14988 KB |
Output is correct |
73 |
Correct |
9 ms |
4812 KB |
Output is correct |