#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string S;
cin >> S;
array<int, 3> cnt{};
vector<vector<int>> g(3);
for (int i = 0; i < n; ++i) {
int a = S[i] == 'R' ? 0 : S[i] == 'G' ? 1 : 2;
++cnt[a];
g[a].push_back(i);
}
if (max({cnt[0], cnt[1], cnt[2]}) > (n + 1) / 2) {
cout << -1;
return 0;
}
array<int, 3> pl[cnt[0] + 1][cnt[1] + 1][cnt[2] + 1];
int dp[3][cnt[0] + 1][cnt[1] + 1][cnt[2] + 1];
pl[0][0][0] = {-1, -1, -1};
for (int t = 0; t < 3; ++t) {
reverse(g[t].begin(), g[t].end());
dp[t][0][0][0] = 0;
}
for (int sum = 1; sum <= n; ++sum) {
for (int f = 0; f <= cnt[0]; ++f) {
for (int s = 0; s <= cnt[1]; ++s) {
if (f + s > sum) {
break;
}
int t = sum - f - s;
if (t > cnt[2]) {
continue;
}
array<int, 3> temp = {f == 0 ? n + 1 : g[0][f - 1], s == 0 ? n + 1 : g[1][s - 1],
t == 0 ? n + 1 : g[2][t - 1]};
int idx = min_element(temp.begin(), temp.end()) - temp.begin();
if (idx == 0) {
pl[f][s][t] = pl[f - 1][s][t];
} else if (idx == 1) {
pl[f][s][t] = pl[f][s - 1][t];
} else {
pl[f][s][t] = pl[f][s][t - 1];
}
for (int k = 0; k < 3; ++k) if (pl[f][s][t][k] != -1) pl[f][s][t][k] += 1;
pl[f][s][t][idx] = 0;
array<int, 3> num = {f, s, t};
if (sum != n) {
for (int p = 0; p < 3; ++p) {
dp[p][f][s][t] = 1000'000'000;
for (int nxt = 0; nxt < 3; ++nxt) {
if (p == nxt || num[nxt] == 0) {
continue;
}
int ans = dp[nxt][f - (nxt == 0)][s - (nxt == 1)][t - (nxt == 2)] + pl[f][s][t][nxt];
if (dp[p][f][s][t] > ans) {
dp[p][f][s][t] = ans;
}
}
}
}
}
}
}
int ans = 1000'000'000;
int f = cnt[0], s = cnt[1], t = cnt[2];
array<int, 3> k = pl[f][s][t];
for (int nxt = 0; nxt < 3; ++nxt) {
if (cnt[nxt] == 0) {
continue;
}
int val = dp[nxt][f - (nxt == 0)][s - (nxt == 1)][t - (nxt == 2)] + pl[f][s][t][nxt];
ans = min(ans, val);
}
cout << ans;
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:72:19: warning: variable 'k' set but not used [-Wunused-but-set-variable]
72 | array<int, 3> k = pl[f][s][t];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
20 ms |
1260 KB |
Output is correct |
3 |
Correct |
20 ms |
1256 KB |
Output is correct |
4 |
Correct |
21 ms |
1264 KB |
Output is correct |
5 |
Correct |
21 ms |
1236 KB |
Output is correct |
6 |
Correct |
20 ms |
1264 KB |
Output is correct |
7 |
Correct |
22 ms |
1236 KB |
Output is correct |
8 |
Correct |
22 ms |
1236 KB |
Output is correct |
9 |
Correct |
20 ms |
1236 KB |
Output is correct |
10 |
Correct |
20 ms |
1256 KB |
Output is correct |
11 |
Correct |
20 ms |
1264 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
7 ms |
724 KB |
Output is correct |
14 |
Correct |
11 ms |
852 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
20 ms |
1260 KB |
Output is correct |
44 |
Correct |
20 ms |
1256 KB |
Output is correct |
45 |
Correct |
21 ms |
1264 KB |
Output is correct |
46 |
Correct |
21 ms |
1236 KB |
Output is correct |
47 |
Correct |
20 ms |
1264 KB |
Output is correct |
48 |
Correct |
22 ms |
1236 KB |
Output is correct |
49 |
Correct |
22 ms |
1236 KB |
Output is correct |
50 |
Correct |
20 ms |
1236 KB |
Output is correct |
51 |
Correct |
20 ms |
1256 KB |
Output is correct |
52 |
Correct |
20 ms |
1264 KB |
Output is correct |
53 |
Correct |
3 ms |
468 KB |
Output is correct |
54 |
Correct |
7 ms |
724 KB |
Output is correct |
55 |
Correct |
11 ms |
852 KB |
Output is correct |
56 |
Correct |
0 ms |
212 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
176 ms |
56584 KB |
Output is correct |
59 |
Correct |
242 ms |
56624 KB |
Output is correct |
60 |
Correct |
181 ms |
56172 KB |
Output is correct |
61 |
Correct |
186 ms |
56788 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
33 ms |
8404 KB |
Output is correct |
64 |
Correct |
76 ms |
28116 KB |
Output is correct |
65 |
Correct |
122 ms |
40404 KB |
Output is correct |
66 |
Correct |
216 ms |
55932 KB |
Output is correct |
67 |
Correct |
169 ms |
55916 KB |
Output is correct |
68 |
Correct |
253 ms |
56756 KB |
Output is correct |
69 |
Correct |
168 ms |
57180 KB |
Output is correct |
70 |
Correct |
219 ms |
56980 KB |
Output is correct |
71 |
Correct |
211 ms |
55708 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |