#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 1e9+9;
int f[401][401][401][3];
void solve(){
int n;
string s;
cin >> n;
cin >> s;
s = ' ' + s;
int R = 0, G = 0, Y = 0;
vector<int> a(n+1);
vector<int> pfr(n+1, 0), pfg(n+1, 0), pfy(n+1, 0);
vector<int> pr(n+1, 0), pg(n+1, 0), py(n+1, 0);
for(int i = 1; i <= n; i++){
pfr[i] = pfr[i-1];
pfg[i] = pfg[i-1];
pfy[i] = pfy[i-1];
if(s[i] == 'R'){
R++;
a[i] = 0;
pr[R] = i;
pfr[i]++;
}
if(s[i] == 'G'){
G++;
a[i] = 1;
pg[G] = i;
pfg[i]++;
}
if(s[i] == 'Y'){
Y++;
a[i] = 2;
py[Y] = i;
pfy[i]++;
}
}
auto chmin = [&](int &a, int b) -> void{
a = min(a, b);
};
for(int r = 0; r <= R; r++){
for(int g = 0; g <= G; g++){
for(int y = 0; y <= Y; y++){
for(int k = 0; k < 3; k++){
f[r][g][y][k] = INF;
}
}
}
}
for(int k = 0; k < 3; k++){
f[0][0][0][k] = 0;
}
for(int i = 1; i <= n; i++){
for(int r = 0; r <= R; r++){
for(int g = 0; g <= G; g++){
int y = i-r-g;
if(y < 0 || y > Y) continue;
if(r > 0){
chmin(f[r][g][y][0], min(f[r-1][g][y][1], f[r-1][g][y][2]) + pr[r]+max(0, g-pfg[pr[r]]) + max(0, y-pfy[pr[r]])-i);
}
if(g > 0){
chmin(f[r][g][y][1], min(f[r][g-1][y][0], f[r][g-1][y][2]) + pg[g]+max(0, r-pfr[pg[g]]) + max(0, y-pfy[pg[g]])-i);
}
if(y > 0){
chmin(f[r][g][y][2], min(f[r][g][y-1][0], f[r][g][y-1][1]) + py[y]+max(0, r-pfr[py[y]]) + max(0, g-pfg[py[y]])-i);
}
}
}
}
int ans = INF;
for(int k = 0; k < 3; k++){
chmin(ans, f[R][G][Y][k]);
}
int g = 1, y = 1;
if(ans > 1e8) ans = -1;
cout << ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
Compilation message
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:76:9: warning: unused variable 'g' [-Wunused-variable]
76 | int g = 1, y = 1;
| ^
joi2019_ho_t3.cpp:76:16: warning: unused variable 'y' [-Wunused-variable]
76 | int g = 1, y = 1;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
2 ms |
6480 KB |
Output is correct |
5 |
Correct |
2 ms |
10576 KB |
Output is correct |
6 |
Correct |
3 ms |
12624 KB |
Output is correct |
7 |
Correct |
3 ms |
14840 KB |
Output is correct |
8 |
Correct |
2 ms |
12624 KB |
Output is correct |
9 |
Correct |
2 ms |
12624 KB |
Output is correct |
10 |
Correct |
3 ms |
20816 KB |
Output is correct |
11 |
Correct |
2 ms |
10576 KB |
Output is correct |
12 |
Correct |
2 ms |
10576 KB |
Output is correct |
13 |
Correct |
2 ms |
6480 KB |
Output is correct |
14 |
Correct |
2 ms |
12624 KB |
Output is correct |
15 |
Correct |
2 ms |
8696 KB |
Output is correct |
16 |
Correct |
3 ms |
22864 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
2 ms |
6480 KB |
Output is correct |
5 |
Correct |
2 ms |
10576 KB |
Output is correct |
6 |
Correct |
3 ms |
12624 KB |
Output is correct |
7 |
Correct |
3 ms |
14840 KB |
Output is correct |
8 |
Correct |
2 ms |
12624 KB |
Output is correct |
9 |
Correct |
2 ms |
12624 KB |
Output is correct |
10 |
Correct |
3 ms |
20816 KB |
Output is correct |
11 |
Correct |
2 ms |
10576 KB |
Output is correct |
12 |
Correct |
2 ms |
10576 KB |
Output is correct |
13 |
Correct |
2 ms |
6480 KB |
Output is correct |
14 |
Correct |
2 ms |
12624 KB |
Output is correct |
15 |
Correct |
2 ms |
8696 KB |
Output is correct |
16 |
Correct |
3 ms |
22864 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
5 ms |
35664 KB |
Output is correct |
19 |
Correct |
4 ms |
37476 KB |
Output is correct |
20 |
Correct |
5 ms |
47708 KB |
Output is correct |
21 |
Correct |
5 ms |
39504 KB |
Output is correct |
22 |
Correct |
5 ms |
47696 KB |
Output is correct |
23 |
Correct |
4 ms |
33360 KB |
Output is correct |
24 |
Correct |
4 ms |
21072 KB |
Output is correct |
25 |
Correct |
7 ms |
43124 KB |
Output is correct |
26 |
Correct |
6 ms |
50256 KB |
Output is correct |
27 |
Correct |
6 ms |
51792 KB |
Output is correct |
28 |
Correct |
5 ms |
41552 KB |
Output is correct |
29 |
Correct |
5 ms |
43480 KB |
Output is correct |
30 |
Correct |
4 ms |
33360 KB |
Output is correct |
31 |
Correct |
4 ms |
29432 KB |
Output is correct |
32 |
Correct |
3 ms |
21888 KB |
Output is correct |
33 |
Correct |
3 ms |
3920 KB |
Output is correct |
34 |
Correct |
3 ms |
3484 KB |
Output is correct |
35 |
Correct |
2 ms |
2640 KB |
Output is correct |
36 |
Correct |
2 ms |
1872 KB |
Output is correct |
37 |
Correct |
2 ms |
1872 KB |
Output is correct |
38 |
Correct |
2 ms |
2128 KB |
Output is correct |
39 |
Correct |
2 ms |
2384 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
2 ms |
2128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
105 ms |
162964 KB |
Output is correct |
3 |
Correct |
113 ms |
162120 KB |
Output is correct |
4 |
Correct |
122 ms |
163112 KB |
Output is correct |
5 |
Correct |
121 ms |
184224 KB |
Output is correct |
6 |
Correct |
114 ms |
185096 KB |
Output is correct |
7 |
Correct |
120 ms |
188040 KB |
Output is correct |
8 |
Correct |
122 ms |
188064 KB |
Output is correct |
9 |
Correct |
116 ms |
188472 KB |
Output is correct |
10 |
Correct |
127 ms |
190016 KB |
Output is correct |
11 |
Correct |
113 ms |
188632 KB |
Output is correct |
12 |
Correct |
31 ms |
53596 KB |
Output is correct |
13 |
Correct |
67 ms |
91464 KB |
Output is correct |
14 |
Correct |
86 ms |
121928 KB |
Output is correct |
15 |
Correct |
117 ms |
174408 KB |
Output is correct |
16 |
Correct |
129 ms |
162892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
2 ms |
6480 KB |
Output is correct |
5 |
Correct |
2 ms |
10576 KB |
Output is correct |
6 |
Correct |
3 ms |
12624 KB |
Output is correct |
7 |
Correct |
3 ms |
14840 KB |
Output is correct |
8 |
Correct |
2 ms |
12624 KB |
Output is correct |
9 |
Correct |
2 ms |
12624 KB |
Output is correct |
10 |
Correct |
3 ms |
20816 KB |
Output is correct |
11 |
Correct |
2 ms |
10576 KB |
Output is correct |
12 |
Correct |
2 ms |
10576 KB |
Output is correct |
13 |
Correct |
2 ms |
6480 KB |
Output is correct |
14 |
Correct |
2 ms |
12624 KB |
Output is correct |
15 |
Correct |
2 ms |
8696 KB |
Output is correct |
16 |
Correct |
3 ms |
22864 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
5 ms |
35664 KB |
Output is correct |
19 |
Correct |
4 ms |
37476 KB |
Output is correct |
20 |
Correct |
5 ms |
47708 KB |
Output is correct |
21 |
Correct |
5 ms |
39504 KB |
Output is correct |
22 |
Correct |
5 ms |
47696 KB |
Output is correct |
23 |
Correct |
4 ms |
33360 KB |
Output is correct |
24 |
Correct |
4 ms |
21072 KB |
Output is correct |
25 |
Correct |
7 ms |
43124 KB |
Output is correct |
26 |
Correct |
6 ms |
50256 KB |
Output is correct |
27 |
Correct |
6 ms |
51792 KB |
Output is correct |
28 |
Correct |
5 ms |
41552 KB |
Output is correct |
29 |
Correct |
5 ms |
43480 KB |
Output is correct |
30 |
Correct |
4 ms |
33360 KB |
Output is correct |
31 |
Correct |
4 ms |
29432 KB |
Output is correct |
32 |
Correct |
3 ms |
21888 KB |
Output is correct |
33 |
Correct |
3 ms |
3920 KB |
Output is correct |
34 |
Correct |
3 ms |
3484 KB |
Output is correct |
35 |
Correct |
2 ms |
2640 KB |
Output is correct |
36 |
Correct |
2 ms |
1872 KB |
Output is correct |
37 |
Correct |
2 ms |
1872 KB |
Output is correct |
38 |
Correct |
2 ms |
2128 KB |
Output is correct |
39 |
Correct |
2 ms |
2384 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
2 ms |
2128 KB |
Output is correct |
42 |
Correct |
1 ms |
504 KB |
Output is correct |
43 |
Correct |
105 ms |
162964 KB |
Output is correct |
44 |
Correct |
113 ms |
162120 KB |
Output is correct |
45 |
Correct |
122 ms |
163112 KB |
Output is correct |
46 |
Correct |
121 ms |
184224 KB |
Output is correct |
47 |
Correct |
114 ms |
185096 KB |
Output is correct |
48 |
Correct |
120 ms |
188040 KB |
Output is correct |
49 |
Correct |
122 ms |
188064 KB |
Output is correct |
50 |
Correct |
116 ms |
188472 KB |
Output is correct |
51 |
Correct |
127 ms |
190016 KB |
Output is correct |
52 |
Correct |
113 ms |
188632 KB |
Output is correct |
53 |
Correct |
31 ms |
53596 KB |
Output is correct |
54 |
Correct |
67 ms |
91464 KB |
Output is correct |
55 |
Correct |
86 ms |
121928 KB |
Output is correct |
56 |
Correct |
117 ms |
174408 KB |
Output is correct |
57 |
Correct |
129 ms |
162892 KB |
Output is correct |
58 |
Correct |
82 ms |
75456 KB |
Output is correct |
59 |
Correct |
97 ms |
99660 KB |
Output is correct |
60 |
Correct |
84 ms |
90952 KB |
Output is correct |
61 |
Correct |
87 ms |
85320 KB |
Output is correct |
62 |
Correct |
131 ms |
160632 KB |
Output is correct |
63 |
Correct |
122 ms |
166984 KB |
Output is correct |
64 |
Correct |
125 ms |
155876 KB |
Output is correct |
65 |
Correct |
118 ms |
149392 KB |
Output is correct |
66 |
Correct |
85 ms |
112896 KB |
Output is correct |
67 |
Correct |
88 ms |
95640 KB |
Output is correct |
68 |
Correct |
101 ms |
98368 KB |
Output is correct |
69 |
Correct |
93 ms |
98548 KB |
Output is correct |
70 |
Correct |
94 ms |
103256 KB |
Output is correct |
71 |
Correct |
107 ms |
99132 KB |
Output is correct |
72 |
Correct |
40 ms |
75476 KB |
Output is correct |
73 |
Correct |
4 ms |
8784 KB |
Output is correct |