#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MAX = 400+2;
int N;
string S;
int dp[3][MAX][MAX][MAX];
int cross[MAX][MAX][MAX];
vector<int> R,G,Y;
int main() {
cin >> N >> S;
for (int i = 0; i < N; i++) {
if (S[i] == 'R') R.push_back(i);
else if (S[i] == 'G') G.push_back(i);
else Y.push_back(i);
}
for (int r = 0; r <= R.size(); r++) {
for (int g = 0; g <= G.size(); g++) {
for (int y = 0; y <= Y.size(); y++) {
int i = r+g+y;
for (int c = 0; c < 3; c++) {
if (r == 0 && g == 0 && y == 0) {
dp[c][r][g][y] = 0;
continue;
}
dp[c][r][g][y] = INF;
if (c == 0 && r > 0) {
int yInv = y - (upper_bound(Y.begin(),Y.begin()+y,R[r-1]) - Y.begin());
int gInv = g - (upper_bound(G.begin(),G.begin()+g,R[r-1]) - G.begin());
// if (r == 2 && g == 0 && y == 1) cout << yInv << " " << gInv << "\n";
dp[c][r][g][y] = min(dp[c][r][g][y],min(dp[1][r-1][g][y],dp[2][r-1][g][y]) + gInv + yInv);
}
if (c == 1 && g > 0) {
int yInv = y - (upper_bound(Y.begin(),Y.begin()+y,G[g-1]) - Y.begin());
int rInv = r - (upper_bound(R.begin(),R.begin()+r,G[g-1]) - R.begin());
dp[c][r][g][y] = min(dp[c][r][g][y],min(dp[0][r][g-1][y],dp[2][r][g-1][y]) + rInv + yInv);
}
if (c == 2 && y > 0) {
int rInv = r - (upper_bound(R.begin(),R.begin()+r,Y[y-1]) - R.begin());
int gInv = g - (upper_bound(G.begin(),G.begin()+g,Y[y-1]) - G.begin());
// if (r == 2 && g == 0 && y == 1) cout << rInv << " " << gInv << "\n";
dp[c][r][g][y] = min(dp[c][r][g][y],min(dp[1][r][g][y-1],dp[0][r][g][y-1]) + gInv + rInv);
}
}
}
}
}
//cout << dp[0][1][0][1] << "\n";
int ans = INF;
for (int c = 0; c < 3; c++) ans = min(ans,dp[c][R.size()][G.size()][Y.size()]);
cout << (ans == INF ? -1 : ans) << "\n";
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:26:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int r = 0; r <= R.size(); r++) {
~~^~~~~~~~~~~
joi2019_ho_t3.cpp:27:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int g = 0; g <= G.size(); g++) {
~~^~~~~~~~~~~
joi2019_ho_t3.cpp:28:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int y = 0; y <= Y.size(); y++) {
~~^~~~~~~~~~~
joi2019_ho_t3.cpp:29:21: warning: unused variable 'i' [-Wunused-variable]
int i = r+g+y;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
768 KB |
Output is correct |
11 |
Correct |
3 ms |
628 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
484 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
640 KB |
Output is correct |
17 |
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 |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
768 KB |
Output is correct |
11 |
Correct |
3 ms |
628 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
484 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
640 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
2816 KB |
Output is correct |
19 |
Correct |
5 ms |
2176 KB |
Output is correct |
20 |
Correct |
4 ms |
2688 KB |
Output is correct |
21 |
Correct |
4 ms |
2816 KB |
Output is correct |
22 |
Correct |
3 ms |
2048 KB |
Output is correct |
23 |
Correct |
4 ms |
2432 KB |
Output is correct |
24 |
Correct |
4 ms |
1792 KB |
Output is correct |
25 |
Correct |
5 ms |
4864 KB |
Output is correct |
26 |
Correct |
6 ms |
5120 KB |
Output is correct |
27 |
Correct |
5 ms |
3712 KB |
Output is correct |
28 |
Correct |
5 ms |
2816 KB |
Output is correct |
29 |
Correct |
5 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
4 ms |
2304 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
4736 KB |
Output is correct |
34 |
Correct |
5 ms |
4352 KB |
Output is correct |
35 |
Correct |
6 ms |
3200 KB |
Output is correct |
36 |
Correct |
4 ms |
2304 KB |
Output is correct |
37 |
Correct |
3 ms |
2048 KB |
Output is correct |
38 |
Correct |
5 ms |
2432 KB |
Output is correct |
39 |
Correct |
5 ms |
2688 KB |
Output is correct |
40 |
Correct |
3 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
2560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
204 ms |
192804 KB |
Output is correct |
3 |
Correct |
161 ms |
191992 KB |
Output is correct |
4 |
Correct |
154 ms |
192932 KB |
Output is correct |
5 |
Correct |
165 ms |
192904 KB |
Output is correct |
6 |
Correct |
132 ms |
192852 KB |
Output is correct |
7 |
Correct |
135 ms |
191964 KB |
Output is correct |
8 |
Correct |
157 ms |
191996 KB |
Output is correct |
9 |
Correct |
153 ms |
191084 KB |
Output is correct |
10 |
Correct |
152 ms |
192900 KB |
Output is correct |
11 |
Correct |
148 ms |
192892 KB |
Output is correct |
12 |
Correct |
44 ms |
52476 KB |
Output is correct |
13 |
Correct |
89 ms |
91592 KB |
Output is correct |
14 |
Correct |
110 ms |
132088 KB |
Output is correct |
15 |
Correct |
158 ms |
192888 KB |
Output is correct |
16 |
Correct |
150 ms |
192860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
768 KB |
Output is correct |
11 |
Correct |
3 ms |
628 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
484 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
640 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
2816 KB |
Output is correct |
19 |
Correct |
5 ms |
2176 KB |
Output is correct |
20 |
Correct |
4 ms |
2688 KB |
Output is correct |
21 |
Correct |
4 ms |
2816 KB |
Output is correct |
22 |
Correct |
3 ms |
2048 KB |
Output is correct |
23 |
Correct |
4 ms |
2432 KB |
Output is correct |
24 |
Correct |
4 ms |
1792 KB |
Output is correct |
25 |
Correct |
5 ms |
4864 KB |
Output is correct |
26 |
Correct |
6 ms |
5120 KB |
Output is correct |
27 |
Correct |
5 ms |
3712 KB |
Output is correct |
28 |
Correct |
5 ms |
2816 KB |
Output is correct |
29 |
Correct |
5 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
4 ms |
2304 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
4736 KB |
Output is correct |
34 |
Correct |
5 ms |
4352 KB |
Output is correct |
35 |
Correct |
6 ms |
3200 KB |
Output is correct |
36 |
Correct |
4 ms |
2304 KB |
Output is correct |
37 |
Correct |
3 ms |
2048 KB |
Output is correct |
38 |
Correct |
5 ms |
2432 KB |
Output is correct |
39 |
Correct |
5 ms |
2688 KB |
Output is correct |
40 |
Correct |
3 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
2560 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
204 ms |
192804 KB |
Output is correct |
44 |
Correct |
161 ms |
191992 KB |
Output is correct |
45 |
Correct |
154 ms |
192932 KB |
Output is correct |
46 |
Correct |
165 ms |
192904 KB |
Output is correct |
47 |
Correct |
132 ms |
192852 KB |
Output is correct |
48 |
Correct |
135 ms |
191964 KB |
Output is correct |
49 |
Correct |
157 ms |
191996 KB |
Output is correct |
50 |
Correct |
153 ms |
191084 KB |
Output is correct |
51 |
Correct |
152 ms |
192900 KB |
Output is correct |
52 |
Correct |
148 ms |
192892 KB |
Output is correct |
53 |
Correct |
44 ms |
52476 KB |
Output is correct |
54 |
Correct |
89 ms |
91592 KB |
Output is correct |
55 |
Correct |
110 ms |
132088 KB |
Output is correct |
56 |
Correct |
158 ms |
192888 KB |
Output is correct |
57 |
Correct |
150 ms |
192860 KB |
Output is correct |
58 |
Correct |
341 ms |
76664 KB |
Output is correct |
59 |
Correct |
384 ms |
97136 KB |
Output is correct |
60 |
Correct |
368 ms |
88568 KB |
Output is correct |
61 |
Correct |
299 ms |
85368 KB |
Output is correct |
62 |
Correct |
159 ms |
188024 KB |
Output is correct |
63 |
Correct |
183 ms |
185336 KB |
Output is correct |
64 |
Correct |
207 ms |
161912 KB |
Output is correct |
65 |
Correct |
285 ms |
141012 KB |
Output is correct |
66 |
Correct |
310 ms |
86588 KB |
Output is correct |
67 |
Correct |
243 ms |
83200 KB |
Output is correct |
68 |
Correct |
235 ms |
89284 KB |
Output is correct |
69 |
Correct |
241 ms |
86008 KB |
Output is correct |
70 |
Correct |
334 ms |
93052 KB |
Output is correct |
71 |
Correct |
330 ms |
84728 KB |
Output is correct |
72 |
Correct |
129 ms |
43768 KB |
Output is correct |
73 |
Correct |
15 ms |
2944 KB |
Output is correct |