#include <bits/stdc++.h>
using namespace std;
const int N = 401;
int dp[3][N][N][N];
array<int, 3> pl[N][N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string S;
cin >> S;
vector<int> a(n);
array<int, 3> cnt{};
vector<vector<int>> g(3);
for (int i = 0; i < n; ++i) {
a[i] = S[i] == 'R' ? 0 : S[i] == 'G' ? 1 : 2;
++cnt[a[i]];
g[a[i]].push_back(i);
}
if (max({cnt[0], cnt[1], cnt[2]}) > (n + 1) / 2) {
cout << -1;
return 0;
}
pl[0][0][0] = {-1, -1, -1};
for (int t = 0; t < 3; ++t) {
reverse(g[t].begin(), g[t].end());
}
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) {
array<int, 3> k = pl[f][s][t];
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:57:35: warning: variable 'k' set but not used [-Wunused-but-set-variable]
57 | array<int, 3> k = pl[f][s][t];
| ^
joi2019_ho_t3.cpp:76:19: warning: variable 'k' set but not used [-Wunused-but-set-variable]
76 | array<int, 3> k = pl[f][s][t];
| ^
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
0 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
724 KB |
Output is correct |
12 |
Correct |
0 ms |
724 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
724 KB |
Output is correct |
15 |
Correct |
0 ms |
596 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
0 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
724 KB |
Output is correct |
12 |
Correct |
0 ms |
724 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
724 KB |
Output is correct |
15 |
Correct |
0 ms |
596 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
4692 KB |
Output is correct |
19 |
Correct |
2 ms |
3796 KB |
Output is correct |
20 |
Correct |
3 ms |
4564 KB |
Output is correct |
21 |
Correct |
2 ms |
4820 KB |
Output is correct |
22 |
Correct |
2 ms |
3284 KB |
Output is correct |
23 |
Correct |
2 ms |
4180 KB |
Output is correct |
24 |
Correct |
3 ms |
3028 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
3 ms |
6356 KB |
Output is correct |
28 |
Correct |
4 ms |
4692 KB |
Output is correct |
29 |
Correct |
2 ms |
4564 KB |
Output is correct |
30 |
Correct |
3 ms |
4436 KB |
Output is correct |
31 |
Correct |
2 ms |
3924 KB |
Output is correct |
32 |
Correct |
2 ms |
4692 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
3 ms |
5460 KB |
Output is correct |
36 |
Correct |
2 ms |
3796 KB |
Output is correct |
37 |
Correct |
2 ms |
3284 KB |
Output is correct |
38 |
Correct |
2 ms |
4180 KB |
Output is correct |
39 |
Correct |
2 ms |
4692 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
148 ms |
354968 KB |
Output is correct |
3 |
Correct |
142 ms |
353228 KB |
Output is correct |
4 |
Correct |
142 ms |
354912 KB |
Output is correct |
5 |
Correct |
143 ms |
354968 KB |
Output is correct |
6 |
Correct |
146 ms |
354972 KB |
Output is correct |
7 |
Correct |
140 ms |
353216 KB |
Output is correct |
8 |
Correct |
142 ms |
353172 KB |
Output is correct |
9 |
Correct |
141 ms |
351464 KB |
Output is correct |
10 |
Correct |
149 ms |
354992 KB |
Output is correct |
11 |
Correct |
145 ms |
354912 KB |
Output is correct |
12 |
Correct |
38 ms |
95940 KB |
Output is correct |
13 |
Correct |
65 ms |
168020 KB |
Output is correct |
14 |
Correct |
98 ms |
242576 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
0 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
724 KB |
Output is correct |
12 |
Correct |
0 ms |
724 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
724 KB |
Output is correct |
15 |
Correct |
0 ms |
596 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
4692 KB |
Output is correct |
19 |
Correct |
2 ms |
3796 KB |
Output is correct |
20 |
Correct |
3 ms |
4564 KB |
Output is correct |
21 |
Correct |
2 ms |
4820 KB |
Output is correct |
22 |
Correct |
2 ms |
3284 KB |
Output is correct |
23 |
Correct |
2 ms |
4180 KB |
Output is correct |
24 |
Correct |
3 ms |
3028 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
3 ms |
6356 KB |
Output is correct |
28 |
Correct |
4 ms |
4692 KB |
Output is correct |
29 |
Correct |
2 ms |
4564 KB |
Output is correct |
30 |
Correct |
3 ms |
4436 KB |
Output is correct |
31 |
Correct |
2 ms |
3924 KB |
Output is correct |
32 |
Correct |
2 ms |
4692 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
3 ms |
5460 KB |
Output is correct |
36 |
Correct |
2 ms |
3796 KB |
Output is correct |
37 |
Correct |
2 ms |
3284 KB |
Output is correct |
38 |
Correct |
2 ms |
4180 KB |
Output is correct |
39 |
Correct |
2 ms |
4692 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
148 ms |
354968 KB |
Output is correct |
44 |
Correct |
142 ms |
353228 KB |
Output is correct |
45 |
Correct |
142 ms |
354912 KB |
Output is correct |
46 |
Correct |
143 ms |
354968 KB |
Output is correct |
47 |
Correct |
146 ms |
354972 KB |
Output is correct |
48 |
Correct |
140 ms |
353216 KB |
Output is correct |
49 |
Correct |
142 ms |
353172 KB |
Output is correct |
50 |
Correct |
141 ms |
351464 KB |
Output is correct |
51 |
Correct |
149 ms |
354992 KB |
Output is correct |
52 |
Correct |
145 ms |
354912 KB |
Output is correct |
53 |
Correct |
38 ms |
95940 KB |
Output is correct |
54 |
Correct |
65 ms |
168020 KB |
Output is correct |
55 |
Correct |
98 ms |
242576 KB |
Output is correct |
56 |
Correct |
0 ms |
212 KB |
Output is correct |
57 |
Correct |
0 ms |
340 KB |
Output is correct |
58 |
Correct |
245 ms |
151628 KB |
Output is correct |
59 |
Correct |
280 ms |
192124 KB |
Output is correct |
60 |
Correct |
254 ms |
174716 KB |
Output is correct |
61 |
Correct |
272 ms |
168764 KB |
Output is correct |
62 |
Correct |
0 ms |
340 KB |
Output is correct |
63 |
Correct |
153 ms |
344616 KB |
Output is correct |
64 |
Correct |
185 ms |
311232 KB |
Output is correct |
65 |
Correct |
217 ms |
278732 KB |
Output is correct |
66 |
Correct |
231 ms |
171324 KB |
Output is correct |
67 |
Correct |
226 ms |
164632 KB |
Output is correct |
68 |
Correct |
254 ms |
176444 KB |
Output is correct |
69 |
Correct |
268 ms |
169992 KB |
Output is correct |
70 |
Correct |
252 ms |
183992 KB |
Output is correct |
71 |
Correct |
252 ms |
167152 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
340 KB |
Output is correct |