#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int G = 'G';
const int R = 'R';
const int Y = 'Y';
int dp[3][404][202][202], pre[101][404], pos[101][404];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i++) {
pre[G][i] = pre[G][i - 1];
pre[R][i] = pre[R][i - 1];
pre[Y][i] = pre[Y][i - 1];
char c; cin >> c;
pos[c][++pre[c][i]] = i;
}
if (pre[R][n] < pre[Y][n])
swap(pre[R], pre[Y]), swap(pos[R], pos[Y]);
if (pre[G][n] < pre[R][n])
swap(pre[G], pre[R]), swap(pos[G], pos[R]);
for (int type = 0; type < 3; type++)
for (int i = 0; i <= pre[G][n]; i++)
for (int j = 0; j <= pre[R][n]; j++)
for (int k = 0; k <= pre[Y][n]; k++)
dp[type][i][j][k] = INT_MAX;
dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0;
function<void(int,int,int,int)> refine = [&] (int i, int j, int k, int type) {
if (type == 0 && i > 0) {
int startPos = pos[G][i], cost = max(0, pre[R][startPos] - j) + max(0, pre[Y][startPos] - k);
if (dp[1][i - 1][j][k] != INT_MAX)
dp[0][i][j][k] = min(dp[0][i][j][k], dp[1][i - 1][j][k] + cost);
if (dp[2][i - 1][j][k] != INT_MAX)
dp[0][i][j][k] = min(dp[0][i][j][k], dp[2][i - 1][j][k] + cost);
}
else if (type == 1 && j > 0) {
int startPos = pos[R][j], cost = max(0, pre[G][startPos] - i) + max(0, pre[Y][startPos] - k);
if (dp[0][i][j - 1][k] != INT_MAX)
dp[1][i][j][k] = min(dp[1][i][j][k], dp[0][i][j - 1][k] + cost);
if (dp[2][i][j - 1][k] != INT_MAX)
dp[1][i][j][k] = min(dp[1][i][j][k], dp[2][i][j - 1][k] + cost);
}
else if (type == 2 && k > 0) {
int startPos = pos[Y][k], cost = max(0, pre[G][startPos] - i) + max(0, pre[R][startPos] - j);
if (dp[0][i][j][k - 1] != INT_MAX)
dp[2][i][j][k] = min(dp[2][i][j][k], dp[0][i][j][k - 1] + cost);
if (dp[1][i][j][k - 1] != INT_MAX)
dp[2][i][j][k] = min(dp[2][i][j][k], dp[1][i][j][k - 1] + cost);
}
};
for (int i = 0; i <= pre[G][n]; i++) {
for (int j = 0; j <= pre[R][n]; j++) {
for (int k = 0; k <= pre[Y][n]; k++) {
if (!i && !j && !k) continue;
for (int type = 0; type < 3; type++) refine(i, j, k, type);
}
}
}
int ans = INT_MAX;
for (int i = 0; i < 3; i++) ans = min(ans, dp[i][pre[G][n]][pre[R][n]][pre[Y][n]]);
cout << (ans == INT_MAX ? -1 : ans);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:31:13: warning: array subscript has type 'char' [-Wchar-subscripts]
31 | pos[c][++pre[c][i]] = i;
| ^
joi2019_ho_t3.cpp:31:22: warning: array subscript has type 'char' [-Wchar-subscripts]
31 | pos[c][++pre[c][i]] = i;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
1656 KB |
Output is correct |
19 |
Correct |
1 ms |
1628 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 ms |
2132 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1884 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2904 KB |
Output is correct |
27 |
Correct |
1 ms |
2140 KB |
Output is correct |
28 |
Correct |
1 ms |
1628 KB |
Output is correct |
29 |
Correct |
1 ms |
1628 KB |
Output is correct |
30 |
Correct |
1 ms |
1884 KB |
Output is correct |
31 |
Correct |
1 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
1884 KB |
Output is correct |
36 |
Correct |
1 ms |
1628 KB |
Output is correct |
37 |
Correct |
1 ms |
1884 KB |
Output is correct |
38 |
Correct |
1 ms |
1884 KB |
Output is correct |
39 |
Correct |
1 ms |
1628 KB |
Output is correct |
40 |
Correct |
1 ms |
2908 KB |
Output is correct |
41 |
Correct |
1 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
34 ms |
96704 KB |
Output is correct |
3 |
Correct |
34 ms |
96776 KB |
Output is correct |
4 |
Correct |
34 ms |
96740 KB |
Output is correct |
5 |
Correct |
33 ms |
96596 KB |
Output is correct |
6 |
Correct |
36 ms |
96596 KB |
Output is correct |
7 |
Correct |
34 ms |
96604 KB |
Output is correct |
8 |
Correct |
34 ms |
96592 KB |
Output is correct |
9 |
Correct |
33 ms |
96080 KB |
Output is correct |
10 |
Correct |
35 ms |
96604 KB |
Output is correct |
11 |
Correct |
36 ms |
96592 KB |
Output is correct |
12 |
Correct |
11 ms |
26972 KB |
Output is correct |
13 |
Correct |
15 ms |
47032 KB |
Output is correct |
14 |
Correct |
26 ms |
67420 KB |
Output is correct |
15 |
Correct |
41 ms |
97076 KB |
Output is correct |
16 |
Correct |
34 ms |
97104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
1656 KB |
Output is correct |
19 |
Correct |
1 ms |
1628 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 ms |
2132 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1884 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2904 KB |
Output is correct |
27 |
Correct |
1 ms |
2140 KB |
Output is correct |
28 |
Correct |
1 ms |
1628 KB |
Output is correct |
29 |
Correct |
1 ms |
1628 KB |
Output is correct |
30 |
Correct |
1 ms |
1884 KB |
Output is correct |
31 |
Correct |
1 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
1884 KB |
Output is correct |
36 |
Correct |
1 ms |
1628 KB |
Output is correct |
37 |
Correct |
1 ms |
1884 KB |
Output is correct |
38 |
Correct |
1 ms |
1884 KB |
Output is correct |
39 |
Correct |
1 ms |
1628 KB |
Output is correct |
40 |
Correct |
1 ms |
2908 KB |
Output is correct |
41 |
Correct |
1 ms |
1884 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
34 ms |
96704 KB |
Output is correct |
44 |
Correct |
34 ms |
96776 KB |
Output is correct |
45 |
Correct |
34 ms |
96740 KB |
Output is correct |
46 |
Correct |
33 ms |
96596 KB |
Output is correct |
47 |
Correct |
36 ms |
96596 KB |
Output is correct |
48 |
Correct |
34 ms |
96604 KB |
Output is correct |
49 |
Correct |
34 ms |
96592 KB |
Output is correct |
50 |
Correct |
33 ms |
96080 KB |
Output is correct |
51 |
Correct |
35 ms |
96604 KB |
Output is correct |
52 |
Correct |
36 ms |
96592 KB |
Output is correct |
53 |
Correct |
11 ms |
26972 KB |
Output is correct |
54 |
Correct |
15 ms |
47032 KB |
Output is correct |
55 |
Correct |
26 ms |
67420 KB |
Output is correct |
56 |
Correct |
41 ms |
97076 KB |
Output is correct |
57 |
Correct |
34 ms |
97104 KB |
Output is correct |
58 |
Correct |
66 ms |
47964 KB |
Output is correct |
59 |
Correct |
63 ms |
49756 KB |
Output is correct |
60 |
Correct |
59 ms |
45400 KB |
Output is correct |
61 |
Correct |
63 ms |
44636 KB |
Output is correct |
62 |
Correct |
42 ms |
95824 KB |
Output is correct |
63 |
Correct |
47 ms |
94300 KB |
Output is correct |
64 |
Correct |
57 ms |
82520 KB |
Output is correct |
65 |
Correct |
64 ms |
72028 KB |
Output is correct |
66 |
Correct |
62 ms |
44632 KB |
Output is correct |
67 |
Correct |
59 ms |
51804 KB |
Output is correct |
68 |
Correct |
62 ms |
45904 KB |
Output is correct |
69 |
Correct |
60 ms |
44120 KB |
Output is correct |
70 |
Correct |
60 ms |
47708 KB |
Output is correct |
71 |
Correct |
59 ms |
43612 KB |
Output is correct |
72 |
Correct |
20 ms |
23900 KB |
Output is correct |
73 |
Correct |
35 ms |
77028 KB |
Output is correct |