#include <bits/stdc++.h>
using namespace std;
void setmin(int& a, int b) {
if (a > b) {
a = b;
}
}
const int INF = 1e9;
const int MAXN = 405;
int N;
char S[MAXN];
vector<int> locs[3];
int g[3][MAXN][3];
int dp[2][MAXN][MAXN][3];
map<char, int> mp = {
{'R', 0},
{'G', 1},
{'Y', 2}
};
int get_cost(int a, int b, int c, int x, int p) {
return max(0, a - g[x][p][0]) +
max(0, b - g[x][p][1]) +
max(0, c - g[x][p][2]);
}
int main() {
scanf("%d", &N);
scanf("%s", S);
for (int i = 0; i < N; i++) {
locs[mp[S[i]]].push_back(i);
}
for (int a = 0; a < 3; a++) {
int tmp[3] = {};
for (int i = 0; i < int(locs[a].size()); i++) {
for (int b = 0; b < 3; b++) {
while (tmp[b] < int(locs[b].size()) && locs[b][tmp[b]] < locs[a][i]) {
tmp[b]++;
}
g[a][i][b] = tmp[b];
}
}
}
int cur = 0, nxt = 1;
dp[cur][0][0][0] = 0;
dp[cur][0][0][1] = dp[cur][0][0][2] = INF;
for (int n = 0; n < N; n++, swap(cur, nxt)) {
for (int a = 0; a <= n + 1; a++) {
for (int b = 0; a + b <= n + 1; b++) {
fill_n(dp[nxt][a][b], 3, INF);
}
}
for (int a = 0; a <= n; a++) {
for (int b = 0; a + b <= n; b++) {
int c = n - a - b;
for (int x = 0; x < 3; x++) {
if (dp[cur][a][b][x] == INF) continue;
if ((a + b == 0 || x != 0) && a < int(locs[0].size())) {
setmin(dp[nxt][a + 1][b][0], dp[cur][a][b][x] + get_cost(a, b, c, 0, a));
}
if (x != 1 && b < int(locs[1].size())) {
setmin(dp[nxt][a][b + 1][1], dp[cur][a][b][x] + get_cost(a, b, c, 1, b));
}
if (x != 2 && c < int(locs[2].size())) {
setmin(dp[nxt][a][b][2], dp[cur][a][b][x] + get_cost(a, b, c, 2, c));
}
}
}
}
}
int ans = INF;
for (int c = 0; c < 3; c++) {
setmin(ans, dp[cur][locs[0].size()][locs[1].size()][c]);
}
if (ans == INF) ans = -1;
printf("%d\n", ans);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:35:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", S);
~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
380 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
380 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
3 ms |
888 KB |
Output is correct |
19 |
Correct |
3 ms |
888 KB |
Output is correct |
20 |
Correct |
3 ms |
888 KB |
Output is correct |
21 |
Correct |
3 ms |
892 KB |
Output is correct |
22 |
Correct |
3 ms |
888 KB |
Output is correct |
23 |
Correct |
5 ms |
888 KB |
Output is correct |
24 |
Correct |
3 ms |
888 KB |
Output is correct |
25 |
Correct |
3 ms |
888 KB |
Output is correct |
26 |
Correct |
3 ms |
888 KB |
Output is correct |
27 |
Correct |
3 ms |
888 KB |
Output is correct |
28 |
Correct |
3 ms |
888 KB |
Output is correct |
29 |
Correct |
3 ms |
888 KB |
Output is correct |
30 |
Correct |
3 ms |
888 KB |
Output is correct |
31 |
Correct |
3 ms |
888 KB |
Output is correct |
32 |
Correct |
3 ms |
888 KB |
Output is correct |
33 |
Correct |
3 ms |
888 KB |
Output is correct |
34 |
Correct |
3 ms |
888 KB |
Output is correct |
35 |
Correct |
3 ms |
888 KB |
Output is correct |
36 |
Correct |
3 ms |
860 KB |
Output is correct |
37 |
Correct |
3 ms |
1020 KB |
Output is correct |
38 |
Correct |
3 ms |
888 KB |
Output is correct |
39 |
Correct |
3 ms |
888 KB |
Output is correct |
40 |
Correct |
3 ms |
888 KB |
Output is correct |
41 |
Correct |
3 ms |
888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
58 ms |
4088 KB |
Output is correct |
3 |
Correct |
58 ms |
4088 KB |
Output is correct |
4 |
Correct |
59 ms |
4120 KB |
Output is correct |
5 |
Correct |
59 ms |
4088 KB |
Output is correct |
6 |
Correct |
58 ms |
3960 KB |
Output is correct |
7 |
Correct |
58 ms |
4068 KB |
Output is correct |
8 |
Correct |
58 ms |
4088 KB |
Output is correct |
9 |
Correct |
58 ms |
4092 KB |
Output is correct |
10 |
Correct |
58 ms |
4040 KB |
Output is correct |
11 |
Correct |
58 ms |
4088 KB |
Output is correct |
12 |
Correct |
11 ms |
2300 KB |
Output is correct |
13 |
Correct |
22 ms |
2936 KB |
Output is correct |
14 |
Correct |
35 ms |
3448 KB |
Output is correct |
15 |
Correct |
58 ms |
4088 KB |
Output is correct |
16 |
Correct |
58 ms |
4064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
380 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
3 ms |
888 KB |
Output is correct |
19 |
Correct |
3 ms |
888 KB |
Output is correct |
20 |
Correct |
3 ms |
888 KB |
Output is correct |
21 |
Correct |
3 ms |
892 KB |
Output is correct |
22 |
Correct |
3 ms |
888 KB |
Output is correct |
23 |
Correct |
5 ms |
888 KB |
Output is correct |
24 |
Correct |
3 ms |
888 KB |
Output is correct |
25 |
Correct |
3 ms |
888 KB |
Output is correct |
26 |
Correct |
3 ms |
888 KB |
Output is correct |
27 |
Correct |
3 ms |
888 KB |
Output is correct |
28 |
Correct |
3 ms |
888 KB |
Output is correct |
29 |
Correct |
3 ms |
888 KB |
Output is correct |
30 |
Correct |
3 ms |
888 KB |
Output is correct |
31 |
Correct |
3 ms |
888 KB |
Output is correct |
32 |
Correct |
3 ms |
888 KB |
Output is correct |
33 |
Correct |
3 ms |
888 KB |
Output is correct |
34 |
Correct |
3 ms |
888 KB |
Output is correct |
35 |
Correct |
3 ms |
888 KB |
Output is correct |
36 |
Correct |
3 ms |
860 KB |
Output is correct |
37 |
Correct |
3 ms |
1020 KB |
Output is correct |
38 |
Correct |
3 ms |
888 KB |
Output is correct |
39 |
Correct |
3 ms |
888 KB |
Output is correct |
40 |
Correct |
3 ms |
888 KB |
Output is correct |
41 |
Correct |
3 ms |
888 KB |
Output is correct |
42 |
Correct |
2 ms |
380 KB |
Output is correct |
43 |
Correct |
58 ms |
4088 KB |
Output is correct |
44 |
Correct |
58 ms |
4088 KB |
Output is correct |
45 |
Correct |
59 ms |
4120 KB |
Output is correct |
46 |
Correct |
59 ms |
4088 KB |
Output is correct |
47 |
Correct |
58 ms |
3960 KB |
Output is correct |
48 |
Correct |
58 ms |
4068 KB |
Output is correct |
49 |
Correct |
58 ms |
4088 KB |
Output is correct |
50 |
Correct |
58 ms |
4092 KB |
Output is correct |
51 |
Correct |
58 ms |
4040 KB |
Output is correct |
52 |
Correct |
58 ms |
4088 KB |
Output is correct |
53 |
Correct |
11 ms |
2300 KB |
Output is correct |
54 |
Correct |
22 ms |
2936 KB |
Output is correct |
55 |
Correct |
35 ms |
3448 KB |
Output is correct |
56 |
Correct |
58 ms |
4088 KB |
Output is correct |
57 |
Correct |
58 ms |
4064 KB |
Output is correct |
58 |
Correct |
88 ms |
4088 KB |
Output is correct |
59 |
Correct |
88 ms |
4216 KB |
Output is correct |
60 |
Correct |
88 ms |
4088 KB |
Output is correct |
61 |
Correct |
89 ms |
4092 KB |
Output is correct |
62 |
Correct |
59 ms |
4088 KB |
Output is correct |
63 |
Correct |
59 ms |
4088 KB |
Output is correct |
64 |
Correct |
64 ms |
4136 KB |
Output is correct |
65 |
Correct |
70 ms |
4088 KB |
Output is correct |
66 |
Correct |
86 ms |
4088 KB |
Output is correct |
67 |
Correct |
85 ms |
4088 KB |
Output is correct |
68 |
Correct |
87 ms |
4092 KB |
Output is correct |
69 |
Correct |
87 ms |
4088 KB |
Output is correct |
70 |
Correct |
88 ms |
4088 KB |
Output is correct |
71 |
Correct |
85 ms |
4088 KB |
Output is correct |
72 |
Correct |
59 ms |
4144 KB |
Output is correct |
73 |
Correct |
58 ms |
4088 KB |
Output is correct |