#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);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 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 |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 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 |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 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 |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 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 |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 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 |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 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 |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
6 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 |
6 ms |
47708 KB |
Output is correct |
23 |
Correct |
4 ms |
33376 KB |
Output is correct |
24 |
Correct |
3 ms |
21084 KB |
Output is correct |
25 |
Correct |
9 ms |
72280 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 |
41564 KB |
Output is correct |
29 |
Correct |
5 ms |
43612 KB |
Output is correct |
30 |
Correct |
5 ms |
33372 KB |
Output is correct |
31 |
Correct |
4 ms |
29268 KB |
Output is correct |
32 |
Correct |
5 ms |
35672 KB |
Output is correct |
33 |
Correct |
6 ms |
49756 KB |
Output is correct |
34 |
Correct |
6 ms |
43612 KB |
Output is correct |
35 |
Correct |
6 ms |
45660 KB |
Output is correct |
36 |
Correct |
5 ms |
39516 KB |
Output is correct |
37 |
Correct |
4 ms |
27196 KB |
Output is correct |
38 |
Correct |
4 ms |
27228 KB |
Output is correct |
39 |
Correct |
5 ms |
37504 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
3 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
56 ms |
242092 KB |
Output is correct |
3 |
Correct |
55 ms |
243712 KB |
Output is correct |
4 |
Correct |
53 ms |
245532 KB |
Output is correct |
5 |
Correct |
51 ms |
246608 KB |
Output is correct |
6 |
Correct |
52 ms |
245332 KB |
Output is correct |
7 |
Correct |
54 ms |
244604 KB |
Output is correct |
8 |
Correct |
51 ms |
245844 KB |
Output is correct |
9 |
Correct |
54 ms |
244048 KB |
Output is correct |
10 |
Correct |
51 ms |
246472 KB |
Output is correct |
11 |
Correct |
51 ms |
245404 KB |
Output is correct |
12 |
Correct |
21 ms |
154324 KB |
Output is correct |
13 |
Correct |
28 ms |
177288 KB |
Output is correct |
14 |
Correct |
41 ms |
203092 KB |
Output is correct |
15 |
Correct |
52 ms |
242776 KB |
Output is correct |
16 |
Correct |
51 ms |
244048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 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 |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 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 |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 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 |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
6 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 |
6 ms |
47708 KB |
Output is correct |
23 |
Correct |
4 ms |
33376 KB |
Output is correct |
24 |
Correct |
3 ms |
21084 KB |
Output is correct |
25 |
Correct |
9 ms |
72280 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 |
41564 KB |
Output is correct |
29 |
Correct |
5 ms |
43612 KB |
Output is correct |
30 |
Correct |
5 ms |
33372 KB |
Output is correct |
31 |
Correct |
4 ms |
29268 KB |
Output is correct |
32 |
Correct |
5 ms |
35672 KB |
Output is correct |
33 |
Correct |
6 ms |
49756 KB |
Output is correct |
34 |
Correct |
6 ms |
43612 KB |
Output is correct |
35 |
Correct |
6 ms |
45660 KB |
Output is correct |
36 |
Correct |
5 ms |
39516 KB |
Output is correct |
37 |
Correct |
4 ms |
27196 KB |
Output is correct |
38 |
Correct |
4 ms |
27228 KB |
Output is correct |
39 |
Correct |
5 ms |
37504 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
3 ms |
19036 KB |
Output is correct |
42 |
Correct |
1 ms |
4440 KB |
Output is correct |
43 |
Correct |
56 ms |
242092 KB |
Output is correct |
44 |
Correct |
55 ms |
243712 KB |
Output is correct |
45 |
Correct |
53 ms |
245532 KB |
Output is correct |
46 |
Correct |
51 ms |
246608 KB |
Output is correct |
47 |
Correct |
52 ms |
245332 KB |
Output is correct |
48 |
Correct |
54 ms |
244604 KB |
Output is correct |
49 |
Correct |
51 ms |
245844 KB |
Output is correct |
50 |
Correct |
54 ms |
244048 KB |
Output is correct |
51 |
Correct |
51 ms |
246472 KB |
Output is correct |
52 |
Correct |
51 ms |
245404 KB |
Output is correct |
53 |
Correct |
21 ms |
154324 KB |
Output is correct |
54 |
Correct |
28 ms |
177288 KB |
Output is correct |
55 |
Correct |
41 ms |
203092 KB |
Output is correct |
56 |
Correct |
52 ms |
242776 KB |
Output is correct |
57 |
Correct |
51 ms |
244048 KB |
Output is correct |
58 |
Correct |
56 ms |
172880 KB |
Output is correct |
59 |
Correct |
64 ms |
189224 KB |
Output is correct |
60 |
Correct |
60 ms |
184400 KB |
Output is correct |
61 |
Correct |
58 ms |
180024 KB |
Output is correct |
62 |
Correct |
61 ms |
246612 KB |
Output is correct |
63 |
Correct |
56 ms |
240096 KB |
Output is correct |
64 |
Correct |
62 ms |
231504 KB |
Output is correct |
65 |
Correct |
64 ms |
216400 KB |
Output is correct |
66 |
Correct |
59 ms |
180304 KB |
Output is correct |
67 |
Correct |
55 ms |
169552 KB |
Output is correct |
68 |
Correct |
65 ms |
181548 KB |
Output is correct |
69 |
Correct |
61 ms |
180044 KB |
Output is correct |
70 |
Correct |
63 ms |
182964 KB |
Output is correct |
71 |
Correct |
60 ms |
185176 KB |
Output is correct |
72 |
Correct |
34 ms |
171092 KB |
Output is correct |
73 |
Correct |
3 ms |
9052 KB |
Output is correct |