#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9;
int n, dp[444][444][444][3], p[444][3];
string s;
vector<int> r, g, y;
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> s;
for(int i=0;i<n;i++) {
if(s[i] == 'R') r.push_back(i+1);
if(s[i] == 'G') g.push_back(i+1);
if(s[i] == 'Y') y.push_back(i+1);
p[i+1][0] = r.size();
p[i+1][1] = g.size();
p[i+1][2] = y.size();
}
for(int i=0;i<=r.size();i++) for(int j=0;j<=g.size();j++) for(int k=0;k<=y.size();k++) {
if(!i && !j && !k) continue;
for(int l=0;l<3;l++) dp[i][j][k][l] = INF;
int u = i+j+k;
if(i) dp[i][j][k][0] = min(dp[i-1][j][k][1], dp[i-1][j][k][2]) + ((r[i-1] + max(0, j-p[r[i-1]][1]) + max(0, k-p[r[i-1]][2])) - u);
if(j) dp[i][j][k][1] = min(dp[i][j-1][k][0], dp[i][j-1][k][2]) + ((g[j-1] + max(0, i-p[g[j-1]][0]) + max(0, k-p[g[j-1]][2])) - u);
if(k) dp[i][j][k][2] = min(dp[i][j][k-1][0], dp[i][j][k-1][1]) + ((y[k-1] + max(0, i-p[y[k-1]][0]) + max(0, j-p[y[k-1]][1])) - u);
}
int ans = *min_element(dp[r.size()][g.size()][y.size()], dp[r.size()][g.size()][y.size()]+3);
cout << (ans >= INF ? -1 : ans);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:24:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<=r.size();i++) for(int j=0;j<=g.size();j++) for(int k=0;k<=y.size();k++) {
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:24:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<=r.size();i++) for(int j=0;j<=g.size();j++) for(int k=0;k<=y.size();k++) {
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:24:76: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<=r.size();i++) for(int j=0;j<=g.size();j++) for(int k=0;k<=y.size();k++) {
| ~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
520 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
520 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1876 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2380 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1608 KB |
Output is correct |
25 |
Correct |
1 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
1 ms |
3008 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2380 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2260 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
50 ms |
162796 KB |
Output is correct |
3 |
Correct |
49 ms |
162008 KB |
Output is correct |
4 |
Correct |
47 ms |
162776 KB |
Output is correct |
5 |
Correct |
54 ms |
162916 KB |
Output is correct |
6 |
Correct |
50 ms |
162752 KB |
Output is correct |
7 |
Correct |
49 ms |
161976 KB |
Output is correct |
8 |
Correct |
48 ms |
161984 KB |
Output is correct |
9 |
Correct |
49 ms |
161100 KB |
Output is correct |
10 |
Correct |
50 ms |
162740 KB |
Output is correct |
11 |
Correct |
50 ms |
162704 KB |
Output is correct |
12 |
Correct |
17 ms |
43988 KB |
Output is correct |
13 |
Correct |
25 ms |
77016 KB |
Output is correct |
14 |
Correct |
34 ms |
111196 KB |
Output is correct |
15 |
Correct |
48 ms |
162768 KB |
Output is correct |
16 |
Correct |
48 ms |
162780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
520 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1876 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2380 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1608 KB |
Output is correct |
25 |
Correct |
1 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
1 ms |
3008 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2380 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2260 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
50 ms |
162796 KB |
Output is correct |
44 |
Correct |
49 ms |
162008 KB |
Output is correct |
45 |
Correct |
47 ms |
162776 KB |
Output is correct |
46 |
Correct |
54 ms |
162916 KB |
Output is correct |
47 |
Correct |
50 ms |
162752 KB |
Output is correct |
48 |
Correct |
49 ms |
161976 KB |
Output is correct |
49 |
Correct |
48 ms |
161984 KB |
Output is correct |
50 |
Correct |
49 ms |
161100 KB |
Output is correct |
51 |
Correct |
50 ms |
162740 KB |
Output is correct |
52 |
Correct |
50 ms |
162704 KB |
Output is correct |
53 |
Correct |
17 ms |
43988 KB |
Output is correct |
54 |
Correct |
25 ms |
77016 KB |
Output is correct |
55 |
Correct |
34 ms |
111196 KB |
Output is correct |
56 |
Correct |
48 ms |
162768 KB |
Output is correct |
57 |
Correct |
48 ms |
162780 KB |
Output is correct |
58 |
Correct |
40 ms |
83524 KB |
Output is correct |
59 |
Correct |
48 ms |
105816 KB |
Output is correct |
60 |
Correct |
45 ms |
96224 KB |
Output is correct |
61 |
Correct |
44 ms |
92980 KB |
Output is correct |
62 |
Correct |
55 ms |
160512 KB |
Output is correct |
63 |
Correct |
50 ms |
159972 KB |
Output is correct |
64 |
Correct |
53 ms |
150224 KB |
Output is correct |
65 |
Correct |
62 ms |
138520 KB |
Output is correct |
66 |
Correct |
44 ms |
94368 KB |
Output is correct |
67 |
Correct |
45 ms |
90720 KB |
Output is correct |
68 |
Correct |
45 ms |
97100 KB |
Output is correct |
69 |
Correct |
45 ms |
93644 KB |
Output is correct |
70 |
Correct |
58 ms |
101416 KB |
Output is correct |
71 |
Correct |
45 ms |
91892 KB |
Output is correct |
72 |
Correct |
19 ms |
42344 KB |
Output is correct |
73 |
Correct |
2 ms |
3148 KB |
Output is correct |