#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int inf = 1e7;
int n;
char str[401];
int tr[256];
vector<int> pos[3];
int dp[401][401][3];
int cnt[401][3];
int cost(int i, int j, int k, int x) {
int ret = 0;
ret += max(i - cnt[x - 1][0], 0);
ret += max(j - cnt[x - 1][1], 0);
ret += max(k - cnt[x - 1][2], 0);
return ret;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
tr['R'] = 0;
tr['G'] = 1;
tr['Y'] = 2;
cin >> n >> str;
for (int i = 0; i < n; ++i) {
pos[tr[str[i]]].push_back(i + 1);
for (int j = 0; j < 3; ++j) cnt[i + 1][j] = cnt[i][j];
++cnt[i + 1][tr[str[i]]];
}
for (int j = 0; j <= pos[1].size(); ++j) {
for (int k = 0; k <= pos[2].size(); ++k) {
if (j + k == 0) continue;
for (int l = 0; l < 3; ++l) dp[j][k][l] = inf;
}
}
for (int i = 0; i <= pos[0].size(); ++i) {
for (int j = 0; j <= pos[1].size(); ++j) {
for (int k = 0; k <= pos[2].size(); ++k) {
if (i + j + k == 0) continue;
if (i) dp[j][k][0] = min(dp[j][k][1], dp[j][k][2]) + cost(i - 1, j, k, pos[0][i - 1]);
else dp[j][k][0] = inf;
if (j) dp[j][k][1] = min(dp[j - 1][k][0], dp[j - 1][k][2]) + cost(i, j - 1, k, pos[1][j - 1]);
else dp[j][k][1] = inf;
if (k) dp[j][k][2] = min(dp[j][k - 1][0], dp[j][k - 1][1]) + cost(i, j, k - 1, pos[2][k - 1]);
else dp[j][k][2] = inf;
}
}
}
int ans = inf;
for (int i = 0; i < 3; ++i) ans = min(ans, dp[pos[1].size()][pos[2].size()][i]);
printf("%d\n", ans < inf ? ans : -1);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:30:22: warning: array subscript has type 'char' [-Wchar-subscripts]
pos[tr[str[i]]].push_back(i + 1);
^
joi2019_ho_t3.cpp:32:31: warning: array subscript has type 'char' [-Wchar-subscripts]
++cnt[i + 1][tr[str[i]]];
^
joi2019_ho_t3.cpp:34:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j <= pos[1].size(); ++j) {
~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:35:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k <= pos[2].size(); ++k) {
~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:40:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i <= pos[0].size(); ++i) {
~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:41:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j <= pos[1].size(); ++j) {
~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:42:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k <= pos[2].size(); ++k) {
~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
300 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 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 |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
300 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 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 |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
512 KB |
Output is correct |
33 |
Correct |
2 ms |
640 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
2 ms |
384 KB |
Output is correct |
37 |
Correct |
2 ms |
384 KB |
Output is correct |
38 |
Correct |
2 ms |
512 KB |
Output is correct |
39 |
Correct |
2 ms |
384 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
1152 KB |
Output is correct |
4 |
Correct |
4 ms |
1280 KB |
Output is correct |
5 |
Correct |
4 ms |
1152 KB |
Output is correct |
6 |
Correct |
4 ms |
1152 KB |
Output is correct |
7 |
Correct |
4 ms |
1152 KB |
Output is correct |
8 |
Correct |
4 ms |
1196 KB |
Output is correct |
9 |
Correct |
3 ms |
1152 KB |
Output is correct |
10 |
Correct |
3 ms |
1152 KB |
Output is correct |
11 |
Correct |
7 ms |
1152 KB |
Output is correct |
12 |
Correct |
3 ms |
768 KB |
Output is correct |
13 |
Correct |
3 ms |
896 KB |
Output is correct |
14 |
Correct |
2 ms |
1024 KB |
Output is correct |
15 |
Correct |
3 ms |
1152 KB |
Output is correct |
16 |
Correct |
3 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
300 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 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 |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
512 KB |
Output is correct |
33 |
Correct |
2 ms |
640 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
2 ms |
384 KB |
Output is correct |
37 |
Correct |
2 ms |
384 KB |
Output is correct |
38 |
Correct |
2 ms |
512 KB |
Output is correct |
39 |
Correct |
2 ms |
384 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
512 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
3 ms |
1152 KB |
Output is correct |
44 |
Correct |
5 ms |
1152 KB |
Output is correct |
45 |
Correct |
4 ms |
1280 KB |
Output is correct |
46 |
Correct |
4 ms |
1152 KB |
Output is correct |
47 |
Correct |
4 ms |
1152 KB |
Output is correct |
48 |
Correct |
4 ms |
1152 KB |
Output is correct |
49 |
Correct |
4 ms |
1196 KB |
Output is correct |
50 |
Correct |
3 ms |
1152 KB |
Output is correct |
51 |
Correct |
3 ms |
1152 KB |
Output is correct |
52 |
Correct |
7 ms |
1152 KB |
Output is correct |
53 |
Correct |
3 ms |
768 KB |
Output is correct |
54 |
Correct |
3 ms |
896 KB |
Output is correct |
55 |
Correct |
2 ms |
1024 KB |
Output is correct |
56 |
Correct |
3 ms |
1152 KB |
Output is correct |
57 |
Correct |
3 ms |
1152 KB |
Output is correct |
58 |
Correct |
40 ms |
984 KB |
Output is correct |
59 |
Correct |
33 ms |
1024 KB |
Output is correct |
60 |
Correct |
40 ms |
1024 KB |
Output is correct |
61 |
Correct |
32 ms |
1016 KB |
Output is correct |
62 |
Correct |
7 ms |
1024 KB |
Output is correct |
63 |
Correct |
9 ms |
1152 KB |
Output is correct |
64 |
Correct |
18 ms |
1152 KB |
Output is correct |
65 |
Correct |
22 ms |
1252 KB |
Output is correct |
66 |
Correct |
33 ms |
896 KB |
Output is correct |
67 |
Correct |
31 ms |
1104 KB |
Output is correct |
68 |
Correct |
40 ms |
1116 KB |
Output is correct |
69 |
Correct |
36 ms |
896 KB |
Output is correct |
70 |
Correct |
35 ms |
1056 KB |
Output is correct |
71 |
Correct |
31 ms |
896 KB |
Output is correct |
72 |
Correct |
10 ms |
512 KB |
Output is correct |
73 |
Correct |
4 ms |
896 KB |
Output is correct |