#include<bits/stdc++.h>
using namespace std;
const int N = 405;
int n, cnt[N][3], pos[N][3];
char a[N];
inline void upd(int &x, int y){
if(x > y){
x = y;
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
char c;
cin >> c;
if(c == 'R'){
a[i] = 0;
}
else if(c =='G'){
a[i] = 1;
}
else{
a[i] = 2;
}
for(int j = 0; j < 3; j++){
cnt[i][j] = cnt[i - 1][j];
}
cnt[i][a[i]] += 1;
pos[cnt[i][a[i]]][a[i]] = i;
}
int dp[cnt[n][0] + 1][cnt[n][1] + 1][cnt[n][2] + 1][3];
memset(dp, 63, sizeof(dp));
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for(int i = 0; i <= cnt[n][0]; i++){
for(int j = 0; j <= cnt[n][1]; j++){
for(int k = 0; k <= cnt[n][2]; k++){
for(int d = 0; d < 3; d++){
int p;
if(d != 0 && i + 1 <= cnt[n][0]){
p = pos[i + 1][0];
upd(dp[i + 1][j][k][0], dp[i][j][k][d] + max(0, cnt[p][1] - j) + max(0, cnt[p][2] - k));
}
if(d != 1 && j + 1 <= cnt[n][1]){
p = pos[j + 1][1];
upd(dp[i][j + 1][k][1], dp[i][j][k][d] + max(0, cnt[p][0] - i) + max(0, cnt[p][2] - k));
}
if(d != 2 && k + 1 <= cnt[n][2]){
p = pos[k + 1][2];
upd(dp[i][j][k + 1][2], dp[i][j][k][d] + max(0, cnt[p][0] - i) + max(0, cnt[p][1] - j));
}
}
}
}
}
int ans = 2e9;
for(int i = 0; i < 3; i++){
upd(ans, dp[cnt[n][0]][cnt[n][1]][cnt[n][2]][i]);
}
if(ans > 1e7){
return cout << "-1", 0;
}
cout << ans;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:36:20: warning: array subscript has type 'char' [-Wchar-subscripts]
cnt[i][a[i]] += 1;
^
joi2019_ho_t3.cpp:37:24: warning: array subscript has type 'char' [-Wchar-subscripts]
pos[cnt[i][a[i]]][a[i]] = i;
^
joi2019_ho_t3.cpp:37:31: warning: array subscript has type 'char' [-Wchar-subscripts]
pos[cnt[i][a[i]]][a[i]] = i;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
3 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Correct |
3 ms |
384 KB |
Output is correct |
34 |
Correct |
3 ms |
384 KB |
Output is correct |
35 |
Correct |
3 ms |
384 KB |
Output is correct |
36 |
Correct |
2 ms |
384 KB |
Output is correct |
37 |
Correct |
3 ms |
384 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
39 |
Correct |
2 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
3 ms |
768 KB |
Output is correct |
5 |
Correct |
3 ms |
768 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
3 ms |
768 KB |
Output is correct |
8 |
Correct |
3 ms |
768 KB |
Output is correct |
9 |
Correct |
5 ms |
768 KB |
Output is correct |
10 |
Correct |
3 ms |
896 KB |
Output is correct |
11 |
Correct |
0 ms |
768 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
4 ms |
640 KB |
Output is correct |
15 |
Correct |
3 ms |
896 KB |
Output is correct |
16 |
Correct |
3 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
3 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Correct |
3 ms |
384 KB |
Output is correct |
34 |
Correct |
3 ms |
384 KB |
Output is correct |
35 |
Correct |
3 ms |
384 KB |
Output is correct |
36 |
Correct |
2 ms |
384 KB |
Output is correct |
37 |
Correct |
3 ms |
384 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
39 |
Correct |
2 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
42 |
Correct |
3 ms |
384 KB |
Output is correct |
43 |
Correct |
3 ms |
768 KB |
Output is correct |
44 |
Correct |
3 ms |
768 KB |
Output is correct |
45 |
Correct |
3 ms |
768 KB |
Output is correct |
46 |
Correct |
3 ms |
768 KB |
Output is correct |
47 |
Correct |
3 ms |
768 KB |
Output is correct |
48 |
Correct |
3 ms |
768 KB |
Output is correct |
49 |
Correct |
3 ms |
768 KB |
Output is correct |
50 |
Correct |
5 ms |
768 KB |
Output is correct |
51 |
Correct |
3 ms |
896 KB |
Output is correct |
52 |
Correct |
0 ms |
768 KB |
Output is correct |
53 |
Correct |
2 ms |
512 KB |
Output is correct |
54 |
Correct |
3 ms |
512 KB |
Output is correct |
55 |
Correct |
4 ms |
640 KB |
Output is correct |
56 |
Correct |
3 ms |
896 KB |
Output is correct |
57 |
Correct |
3 ms |
768 KB |
Output is correct |
58 |
Correct |
67 ms |
28544 KB |
Output is correct |
59 |
Correct |
68 ms |
28536 KB |
Output is correct |
60 |
Correct |
64 ms |
28280 KB |
Output is correct |
61 |
Correct |
66 ms |
28544 KB |
Output is correct |
62 |
Correct |
10 ms |
2688 KB |
Output is correct |
63 |
Correct |
12 ms |
4480 KB |
Output is correct |
64 |
Correct |
40 ms |
14336 KB |
Output is correct |
65 |
Correct |
49 ms |
20316 KB |
Output is correct |
66 |
Correct |
68 ms |
28160 KB |
Output is correct |
67 |
Correct |
71 ms |
28160 KB |
Output is correct |
68 |
Correct |
69 ms |
28636 KB |
Output is correct |
69 |
Correct |
69 ms |
28860 KB |
Output is correct |
70 |
Correct |
72 ms |
28724 KB |
Output is correct |
71 |
Correct |
97 ms |
28004 KB |
Output is correct |
72 |
Correct |
21 ms |
7552 KB |
Output is correct |
73 |
Correct |
6 ms |
2176 KB |
Output is correct |