#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);
// cout << "[" << i << ' ' << j << ' ' << k << "] ";
// for(int l=0;l<3;l++) cout << dp[i][j][k][l] << ' ';
// cout << '\n';
}
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++) {
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 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 |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
2 ms |
1620 KB |
Output is correct |
25 |
Correct |
3 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3016 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
2 ms |
2248 KB |
Output is correct |
30 |
Correct |
2 ms |
2248 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2388 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3524 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 |
1 ms |
328 KB |
Output is correct |
41 |
Correct |
2 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
56 ms |
162760 KB |
Output is correct |
3 |
Correct |
57 ms |
161984 KB |
Output is correct |
4 |
Correct |
62 ms |
162804 KB |
Output is correct |
5 |
Correct |
54 ms |
162832 KB |
Output is correct |
6 |
Correct |
60 ms |
162764 KB |
Output is correct |
7 |
Correct |
55 ms |
162012 KB |
Output is correct |
8 |
Correct |
55 ms |
161960 KB |
Output is correct |
9 |
Correct |
56 ms |
161216 KB |
Output is correct |
10 |
Correct |
55 ms |
162792 KB |
Output is correct |
11 |
Correct |
61 ms |
162740 KB |
Output is correct |
12 |
Correct |
16 ms |
44028 KB |
Output is correct |
13 |
Correct |
26 ms |
77048 KB |
Output is correct |
14 |
Correct |
37 ms |
111232 KB |
Output is correct |
15 |
Correct |
55 ms |
162720 KB |
Output is correct |
16 |
Correct |
54 ms |
162704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 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 |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
2 ms |
1620 KB |
Output is correct |
25 |
Correct |
3 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3016 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
2 ms |
2248 KB |
Output is correct |
30 |
Correct |
2 ms |
2248 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2388 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3524 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 |
1 ms |
328 KB |
Output is correct |
41 |
Correct |
2 ms |
2132 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
56 ms |
162760 KB |
Output is correct |
44 |
Correct |
57 ms |
161984 KB |
Output is correct |
45 |
Correct |
62 ms |
162804 KB |
Output is correct |
46 |
Correct |
54 ms |
162832 KB |
Output is correct |
47 |
Correct |
60 ms |
162764 KB |
Output is correct |
48 |
Correct |
55 ms |
162012 KB |
Output is correct |
49 |
Correct |
55 ms |
161960 KB |
Output is correct |
50 |
Correct |
56 ms |
161216 KB |
Output is correct |
51 |
Correct |
55 ms |
162792 KB |
Output is correct |
52 |
Correct |
61 ms |
162740 KB |
Output is correct |
53 |
Correct |
16 ms |
44028 KB |
Output is correct |
54 |
Correct |
26 ms |
77048 KB |
Output is correct |
55 |
Correct |
37 ms |
111232 KB |
Output is correct |
56 |
Correct |
55 ms |
162720 KB |
Output is correct |
57 |
Correct |
54 ms |
162704 KB |
Output is correct |
58 |
Correct |
48 ms |
83408 KB |
Output is correct |
59 |
Correct |
54 ms |
105844 KB |
Output is correct |
60 |
Correct |
56 ms |
96152 KB |
Output is correct |
61 |
Correct |
50 ms |
92960 KB |
Output is correct |
62 |
Correct |
55 ms |
160408 KB |
Output is correct |
63 |
Correct |
59 ms |
160076 KB |
Output is correct |
64 |
Correct |
64 ms |
150348 KB |
Output is correct |
65 |
Correct |
59 ms |
138552 KB |
Output is correct |
66 |
Correct |
57 ms |
94304 KB |
Output is correct |
67 |
Correct |
47 ms |
90828 KB |
Output is correct |
68 |
Correct |
51 ms |
97128 KB |
Output is correct |
69 |
Correct |
58 ms |
93612 KB |
Output is correct |
70 |
Correct |
64 ms |
101456 KB |
Output is correct |
71 |
Correct |
49 ms |
91968 KB |
Output is correct |
72 |
Correct |
19 ms |
42452 KB |
Output is correct |
73 |
Correct |
3 ms |
3028 KB |
Output is correct |