#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
string s;
cin >> n >> s;
vector<int> pos[3];
for (int i = 0; i < n; i++) {
if (s[i] == 'R') {
pos[0].push_back(i);
} else if (s[i] == 'G') {
pos[1].push_back(i);
} else {
pos[2].push_back(i);
}
}
int R = pos[0].size(), G = pos[1].size(), Y = pos[2].size();
vector lb(3, vector<int>(n));
for (int i = 0; i < 3; i++) {
for (int j = 0; j < n; j++) {
lb[i][j] = lower_bound(pos[i].begin(), pos[i].end(), j) - pos[i].begin();
}
}
vector dp(R + 1, vector(G + 1, vector(Y + 1, vector<int>(4, 1e9))));
dp[0][0][0][3] = 0;
for (int i = 0; i < n; i++) {
for (int r = 0; r <= R && r <= i; r++) {
for (int g = 0; g <= G && r + g <= i; g++) {
int y = i - r - g;
if (y > Y) {
continue;
}
for (int l = 0; l < 4; l++) {
if (l != 0 && r < R) {
int a = lb[1][pos[0][r]], b = lb[2][pos[0][r]];
dp[r + 1][g][y][0] = min(dp[r + 1][g][y][0], dp[r][g][y][l] + max(0, g - a) + max(0, y - b));
}
if (l != 1 && g < G) {
int a = lb[0][pos[1][g]], b = lb[2][pos[1][g]];
dp[r][g + 1][y][1] = min(dp[r][g + 1][y][1], dp[r][g][y][l] + max(0, r - a) + max(0, y - b));
}
if (l != 2 && y < Y) {
int a = lb[0][pos[2][y]], b = lb[1][pos[2][y]];
dp[r][g][y + 1][2] = min(dp[r][g][y + 1][2], dp[r][g][y][l] + max(0, r - a) + max(0, g - b));
}
}
}
}
}
int ans = min({dp[R][G][Y][0], dp[R][G][Y][1], dp[R][G][Y][2]});
cout << (ans == 1e9 ? -1 : ans) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
# |
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 |
1 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 |
0 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 |
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 |
1 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
2 ms |
852 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
2 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
852 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
2 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
38 |
Correct |
2 ms |
724 KB |
Output is correct |
39 |
Correct |
2 ms |
852 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
17 ms |
3796 KB |
Output is correct |
3 |
Correct |
18 ms |
3796 KB |
Output is correct |
4 |
Correct |
18 ms |
3820 KB |
Output is correct |
5 |
Correct |
19 ms |
3824 KB |
Output is correct |
6 |
Correct |
19 ms |
3796 KB |
Output is correct |
7 |
Correct |
17 ms |
3796 KB |
Output is correct |
8 |
Correct |
15 ms |
3796 KB |
Output is correct |
9 |
Correct |
15 ms |
3668 KB |
Output is correct |
10 |
Correct |
15 ms |
3824 KB |
Output is correct |
11 |
Correct |
15 ms |
3832 KB |
Output is correct |
12 |
Correct |
3 ms |
1236 KB |
Output is correct |
13 |
Correct |
6 ms |
1876 KB |
Output is correct |
14 |
Correct |
12 ms |
2716 KB |
Output is correct |
15 |
Correct |
16 ms |
3828 KB |
Output is correct |
16 |
Correct |
15 ms |
3796 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 |
1 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
2 ms |
852 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
2 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
852 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
2 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
38 |
Correct |
2 ms |
724 KB |
Output is correct |
39 |
Correct |
2 ms |
852 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
17 ms |
3796 KB |
Output is correct |
44 |
Correct |
18 ms |
3796 KB |
Output is correct |
45 |
Correct |
18 ms |
3820 KB |
Output is correct |
46 |
Correct |
19 ms |
3824 KB |
Output is correct |
47 |
Correct |
19 ms |
3796 KB |
Output is correct |
48 |
Correct |
17 ms |
3796 KB |
Output is correct |
49 |
Correct |
15 ms |
3796 KB |
Output is correct |
50 |
Correct |
15 ms |
3668 KB |
Output is correct |
51 |
Correct |
15 ms |
3824 KB |
Output is correct |
52 |
Correct |
15 ms |
3832 KB |
Output is correct |
53 |
Correct |
3 ms |
1236 KB |
Output is correct |
54 |
Correct |
6 ms |
1876 KB |
Output is correct |
55 |
Correct |
12 ms |
2716 KB |
Output is correct |
56 |
Correct |
16 ms |
3828 KB |
Output is correct |
57 |
Correct |
15 ms |
3796 KB |
Output is correct |
58 |
Correct |
372 ms |
133208 KB |
Output is correct |
59 |
Correct |
271 ms |
133200 KB |
Output is correct |
60 |
Correct |
277 ms |
132348 KB |
Output is correct |
61 |
Correct |
280 ms |
133792 KB |
Output is correct |
62 |
Correct |
32 ms |
12372 KB |
Output is correct |
63 |
Correct |
50 ms |
20784 KB |
Output is correct |
64 |
Correct |
146 ms |
66752 KB |
Output is correct |
65 |
Correct |
200 ms |
95600 KB |
Output is correct |
66 |
Correct |
299 ms |
131916 KB |
Output is correct |
67 |
Correct |
279 ms |
131900 KB |
Output is correct |
68 |
Correct |
269 ms |
133708 KB |
Output is correct |
69 |
Correct |
283 ms |
134820 KB |
Output is correct |
70 |
Correct |
278 ms |
134228 KB |
Output is correct |
71 |
Correct |
267 ms |
130980 KB |
Output is correct |
72 |
Correct |
75 ms |
34132 KB |
Output is correct |
73 |
Correct |
21 ms |
10580 KB |
Output is correct |