#include<iostream>
#include<cassert>
#include<vector>
using namespace std;
int const N = 405;
int a[N], n;
int const R = 0, G = 1, Y = 2, inf = 1e9;
vector<int> point[3];
int dp[N][N][N][3], backp[3][N];
void solve(){
dp[0][0][0][0] = 0; dp[0][0][0][1] = 0; dp[0][0][0][2] = 0;
for(int i = 0; i <= point[0].size(); i++){
for(int j = 0; j <= point[1].size(); j++){
for(int k = 0; k <= point[2].size(); k++){
if(!i && !j && !k)
continue;
for(int t = 0; t < 3; t++){
int backcnt = 0;
if((t == 0 && !i) || (t == 1 && !j) || (t == 2 && !k)){
dp[i][j][k][t] = inf;
continue;
}
if(t == 0){
if(j)
backcnt += max(j - backp[1][point[t][i - 1]], 0);
if(k)
backcnt += max(k - backp[2][point[t][i - 1]], 0);
dp[i][j][k][t] = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + abs(point[0][i - 1] + backcnt - (i + j + k - 1));
}else if(t == 1){
if(i)
backcnt += max(i - backp[0][point[t][j - 1]], 0);
if(k)
backcnt += max(k - backp[2][point[t][j - 1]], 0);
dp[i][j][k][t] = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + abs(point[1][j - 1] + backcnt - (i + j + k - 1));
}else if(t == 2){
if(i)
backcnt += max(i - backp[0][point[t][k - 1]], 0);
if(j)
backcnt += max(j - backp[1][point[t][k - 1]], 0);
dp[i][j][k][t] = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + abs(point[2][k - 1] + backcnt - (i + j + k - 1));
}
}
}
}
}
cout << min(dp[point[0].size()][point[1].size()][point[2].size()][0],min(dp[point[0].size()][point[1].size()][point[2].size()][1], dp[point[0].size()][point[1].size()][point[2].size()][2])) << '\n';
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
char in;
if(n == 1){
cout << 0 << '\n';
return 0;
}
for(int i = 0; i < n; i++){
cin >> in;
if(in == 'R'){
a[i] = 0;
}else if(in == 'G')
a[i] = 1;
else{
a[i] = 2;
}
point[a[i]].push_back(i);
}
for(int i = 0; i < 3; i++){
if(point[i].size() > (n + 1) / 2){
cout << -1 << '\n';
return 0;
}
backp[i][n] = point[i].size();
int p = point[i].size() - 1;
for(int j = n - 1; j >= 0; j--){
if(p >= 0 && point[i][p] == j){
backp[i][j] = p;
p--;
}else{
backp[i][j] = backp[i][j + 1];
}
}
}
solve();
}
Compilation message
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:12:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for(int i = 0; i <= point[0].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:13:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for(int j = 0; j <= point[1].size(); j++){
| ~~^~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:14:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for(int k = 0; k <= point[2].size(); k++){
| ~~^~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:69:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
69 | if(point[i].size() > (n + 1) / 2){
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
448 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
448 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
2252 KB |
Output is correct |
19 |
Correct |
1 ms |
1868 KB |
Output is correct |
20 |
Correct |
1 ms |
2252 KB |
Output is correct |
21 |
Correct |
1 ms |
2376 KB |
Output is correct |
22 |
Correct |
1 ms |
1612 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
1 ms |
316 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
1 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
2252 KB |
Output is correct |
31 |
Correct |
1 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
2372 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
320 KB |
Output is correct |
35 |
Correct |
1 ms |
2636 KB |
Output is correct |
36 |
Correct |
1 ms |
1868 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
1 ms |
2124 KB |
Output is correct |
39 |
Correct |
1 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
58 ms |
162920 KB |
Output is correct |
3 |
Correct |
57 ms |
162168 KB |
Output is correct |
4 |
Correct |
74 ms |
162956 KB |
Output is correct |
5 |
Correct |
61 ms |
162860 KB |
Output is correct |
6 |
Correct |
59 ms |
162868 KB |
Output is correct |
7 |
Correct |
56 ms |
162112 KB |
Output is correct |
8 |
Correct |
62 ms |
162116 KB |
Output is correct |
9 |
Correct |
65 ms |
161316 KB |
Output is correct |
10 |
Correct |
76 ms |
162876 KB |
Output is correct |
11 |
Correct |
57 ms |
162884 KB |
Output is correct |
12 |
Correct |
16 ms |
43980 KB |
Output is correct |
13 |
Correct |
30 ms |
77108 KB |
Output is correct |
14 |
Correct |
44 ms |
111316 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
448 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
2252 KB |
Output is correct |
19 |
Correct |
1 ms |
1868 KB |
Output is correct |
20 |
Correct |
1 ms |
2252 KB |
Output is correct |
21 |
Correct |
1 ms |
2376 KB |
Output is correct |
22 |
Correct |
1 ms |
1612 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
1 ms |
316 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
1 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
2252 KB |
Output is correct |
31 |
Correct |
1 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
2372 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
320 KB |
Output is correct |
35 |
Correct |
1 ms |
2636 KB |
Output is correct |
36 |
Correct |
1 ms |
1868 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
1 ms |
2124 KB |
Output is correct |
39 |
Correct |
1 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
58 ms |
162920 KB |
Output is correct |
44 |
Correct |
57 ms |
162168 KB |
Output is correct |
45 |
Correct |
74 ms |
162956 KB |
Output is correct |
46 |
Correct |
61 ms |
162860 KB |
Output is correct |
47 |
Correct |
59 ms |
162868 KB |
Output is correct |
48 |
Correct |
56 ms |
162112 KB |
Output is correct |
49 |
Correct |
62 ms |
162116 KB |
Output is correct |
50 |
Correct |
65 ms |
161316 KB |
Output is correct |
51 |
Correct |
76 ms |
162876 KB |
Output is correct |
52 |
Correct |
57 ms |
162884 KB |
Output is correct |
53 |
Correct |
16 ms |
43980 KB |
Output is correct |
54 |
Correct |
30 ms |
77108 KB |
Output is correct |
55 |
Correct |
44 ms |
111316 KB |
Output is correct |
56 |
Correct |
1 ms |
204 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
65 ms |
76152 KB |
Output is correct |
59 |
Correct |
77 ms |
96568 KB |
Output is correct |
60 |
Correct |
65 ms |
87732 KB |
Output is correct |
61 |
Correct |
61 ms |
84836 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
61 ms |
160140 KB |
Output is correct |
64 |
Correct |
71 ms |
150116 KB |
Output is correct |
65 |
Correct |
74 ms |
138436 KB |
Output is correct |
66 |
Correct |
88 ms |
86020 KB |
Output is correct |
67 |
Correct |
91 ms |
82756 KB |
Output is correct |
68 |
Correct |
65 ms |
88648 KB |
Output is correct |
69 |
Correct |
63 ms |
85396 KB |
Output is correct |
70 |
Correct |
68 ms |
92528 KB |
Output is correct |
71 |
Correct |
59 ms |
83908 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
1 ms |
204 KB |
Output is correct |