#include <bits/stdc++.h>
#define fast cin.tie(0)->sync_with_stdio(0);
#define inf ((int)1e6)
using namespace std;
const int N = 403;
int dp[N][N][N][3];
int bs[3][3][N];
int32_t main(){
fast
int n;
cin >> n;
string s;
cin >> s;
int cnt[3] = {0, 0, 0};
vector <vector<int> > pos(3);
for(int i = 0; i < n; i++) {
if(s[i] == 'R') {
cnt[0]++;
pos[0].push_back(i);
}
else if(s[i] == 'G') {
cnt[1]++;
pos[1].push_back(i);
}
else {
cnt[2]++;
pos[2].push_back(i);
}
}
pos[0].push_back(inf);
pos[1].push_back(inf);
pos[2].push_back(inf);
for(int r = 0; r <= cnt[0]; r++)
for(int g = 0; g <= cnt[1]; g++)
for(int y = 0; y <= cnt[2]; y++)
for(int last = 0; last < 3; last++)
dp[r][g][y][last] = inf;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
for(int k = 0; k < pos[i].size(); k++) {
bs[i][j][k] = lower_bound(pos[j].begin(), pos[j].end(), pos[i][k]) - pos[j].begin();
}
}
}
dp[1][0][0][0] = pos[0][0];
dp[0][1][0][1] = pos[1][0];
dp[0][0][1][2] = pos[2][0];
auto adjust = [&](int x, int y, int z) {
int ix = pos[0][x];
ix += max(0, y - bs[0][1][x]);
ix += max(0, z - bs[0][2][x]);
//cout << bs[0][1][x] << " " << bs[0][2][x] << "\n";
int iy = pos[1][y];
iy += max(0, x - bs[1][0][y]);
iy += max(0, z - bs[1][2][y]);
int iz = pos[2][z];
iz += max(0, x - bs[2][0][z]);
iz += max(0, y - bs[2][1][z]);
return array<int, 3>{ix, iy, iz};
};
auto a = adjust(0, 0, 0);
//cout << a[0] << " " << a[1] << " " << a[2] << "\n";
for(int r = 0; r <= cnt[0]; r++) {
for(int g = 0; g <= cnt[1]; g++) {
for(int y = 0; y <= cnt[2]; y++) {
auto [ir, ig, iy] = adjust(r, g, y);
int ind = r + g + y;
for(int last : {0, 1, 2}) {
int val = dp[r][g][y][last];
// cout << r << " " << g << " " << y << " " << last << "\n";
// cout << ir << " " << ig << " " << iy << " " << ind << "\n";
// cout << val << "\n";
if(val >= inf) continue;
if(last != 0) {
dp[r+1][g][y][0] = min(dp[r+1][g][y][0], val + abs(ir - ind));
}
if(last != 1) {
dp[r][g+1][y][1] = min(dp[r][g+1][y][1], val + abs(ig - ind));
}
if(last != 2) {
dp[r][g][y+1][2] = min(dp[r][g][y+1][2], val + abs(iy - ind));
}
}
}
}
}
int ans = inf;
for(int i = 0; i < 3; i++) {
ans = min(ans, dp[cnt[0]][cnt[1]][cnt[2]][i]);
}
if(ans == inf) cout << -1;
else cout << ans;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int32_t main()':
joi2019_ho_t3.cpp:41:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int k = 0; k < pos[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:63:7: warning: variable 'a' set but not used [-Wunused-but-set-variable]
63 | auto a = adjust(0, 0, 0);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
5 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10704 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
5 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10704 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
13 ms |
45660 KB |
Output is correct |
19 |
Correct |
5 ms |
37464 KB |
Output is correct |
20 |
Correct |
6 ms |
49756 KB |
Output is correct |
21 |
Correct |
5 ms |
39516 KB |
Output is correct |
22 |
Correct |
7 ms |
47708 KB |
Output is correct |
23 |
Correct |
5 ms |
33368 KB |
Output is correct |
24 |
Correct |
3 ms |
21080 KB |
Output is correct |
25 |
Correct |
13 ms |
72344 KB |
Output is correct |
26 |
Correct |
7 ms |
59996 KB |
Output is correct |
27 |
Correct |
7 ms |
53852 KB |
Output is correct |
28 |
Correct |
5 ms |
41560 KB |
Output is correct |
29 |
Correct |
6 ms |
43628 KB |
Output is correct |
30 |
Correct |
4 ms |
33372 KB |
Output is correct |
31 |
Correct |
4 ms |
29276 KB |
Output is correct |
32 |
Correct |
5 ms |
35532 KB |
Output is correct |
33 |
Correct |
6 ms |
49756 KB |
Output is correct |
34 |
Correct |
5 ms |
43612 KB |
Output is correct |
35 |
Correct |
6 ms |
45660 KB |
Output is correct |
36 |
Correct |
6 ms |
39516 KB |
Output is correct |
37 |
Correct |
3 ms |
27228 KB |
Output is correct |
38 |
Correct |
4 ms |
27228 KB |
Output is correct |
39 |
Correct |
5 ms |
37468 KB |
Output is correct |
40 |
Correct |
1 ms |
2648 KB |
Output is correct |
41 |
Correct |
2 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
74 ms |
242164 KB |
Output is correct |
3 |
Correct |
56 ms |
244052 KB |
Output is correct |
4 |
Correct |
51 ms |
246612 KB |
Output is correct |
5 |
Correct |
59 ms |
246612 KB |
Output is correct |
6 |
Correct |
53 ms |
245528 KB |
Output is correct |
7 |
Correct |
53 ms |
245840 KB |
Output is correct |
8 |
Correct |
51 ms |
244728 KB |
Output is correct |
9 |
Correct |
51 ms |
244100 KB |
Output is correct |
10 |
Correct |
53 ms |
245332 KB |
Output is correct |
11 |
Correct |
57 ms |
245328 KB |
Output is correct |
12 |
Correct |
20 ms |
157620 KB |
Output is correct |
13 |
Correct |
31 ms |
178780 KB |
Output is correct |
14 |
Correct |
37 ms |
204372 KB |
Output is correct |
15 |
Correct |
51 ms |
245356 KB |
Output is correct |
16 |
Correct |
51 ms |
245120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
5 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10704 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
13 ms |
45660 KB |
Output is correct |
19 |
Correct |
5 ms |
37464 KB |
Output is correct |
20 |
Correct |
6 ms |
49756 KB |
Output is correct |
21 |
Correct |
5 ms |
39516 KB |
Output is correct |
22 |
Correct |
7 ms |
47708 KB |
Output is correct |
23 |
Correct |
5 ms |
33368 KB |
Output is correct |
24 |
Correct |
3 ms |
21080 KB |
Output is correct |
25 |
Correct |
13 ms |
72344 KB |
Output is correct |
26 |
Correct |
7 ms |
59996 KB |
Output is correct |
27 |
Correct |
7 ms |
53852 KB |
Output is correct |
28 |
Correct |
5 ms |
41560 KB |
Output is correct |
29 |
Correct |
6 ms |
43628 KB |
Output is correct |
30 |
Correct |
4 ms |
33372 KB |
Output is correct |
31 |
Correct |
4 ms |
29276 KB |
Output is correct |
32 |
Correct |
5 ms |
35532 KB |
Output is correct |
33 |
Correct |
6 ms |
49756 KB |
Output is correct |
34 |
Correct |
5 ms |
43612 KB |
Output is correct |
35 |
Correct |
6 ms |
45660 KB |
Output is correct |
36 |
Correct |
6 ms |
39516 KB |
Output is correct |
37 |
Correct |
3 ms |
27228 KB |
Output is correct |
38 |
Correct |
4 ms |
27228 KB |
Output is correct |
39 |
Correct |
5 ms |
37468 KB |
Output is correct |
40 |
Correct |
1 ms |
2648 KB |
Output is correct |
41 |
Correct |
2 ms |
19036 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
74 ms |
242164 KB |
Output is correct |
44 |
Correct |
56 ms |
244052 KB |
Output is correct |
45 |
Correct |
51 ms |
246612 KB |
Output is correct |
46 |
Correct |
59 ms |
246612 KB |
Output is correct |
47 |
Correct |
53 ms |
245528 KB |
Output is correct |
48 |
Correct |
53 ms |
245840 KB |
Output is correct |
49 |
Correct |
51 ms |
244728 KB |
Output is correct |
50 |
Correct |
51 ms |
244100 KB |
Output is correct |
51 |
Correct |
53 ms |
245332 KB |
Output is correct |
52 |
Correct |
57 ms |
245328 KB |
Output is correct |
53 |
Correct |
20 ms |
157620 KB |
Output is correct |
54 |
Correct |
31 ms |
178780 KB |
Output is correct |
55 |
Correct |
37 ms |
204372 KB |
Output is correct |
56 |
Correct |
51 ms |
245356 KB |
Output is correct |
57 |
Correct |
51 ms |
245120 KB |
Output is correct |
58 |
Correct |
56 ms |
175664 KB |
Output is correct |
59 |
Correct |
63 ms |
190628 KB |
Output is correct |
60 |
Correct |
62 ms |
185704 KB |
Output is correct |
61 |
Correct |
60 ms |
181332 KB |
Output is correct |
62 |
Correct |
58 ms |
247888 KB |
Output is correct |
63 |
Correct |
60 ms |
243380 KB |
Output is correct |
64 |
Correct |
64 ms |
233784 KB |
Output is correct |
65 |
Correct |
64 ms |
217424 KB |
Output is correct |
66 |
Correct |
61 ms |
184332 KB |
Output is correct |
67 |
Correct |
56 ms |
173136 KB |
Output is correct |
68 |
Correct |
60 ms |
184144 KB |
Output is correct |
69 |
Correct |
59 ms |
184148 KB |
Output is correct |
70 |
Correct |
60 ms |
185536 KB |
Output is correct |
71 |
Correct |
63 ms |
186644 KB |
Output is correct |
72 |
Correct |
34 ms |
174864 KB |
Output is correct |
73 |
Correct |
3 ms |
9052 KB |
Output is correct |