#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define eps 1e-8
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
// change if necessary
#define MAXN 410
using namespace std;
int n;
string s;
bool vis[MAXN][MAXN][MAXN][3];
int dp[MAXN][MAXN][MAXN][3];
int pref[MAXN][3];
vector<int> idx[3];
int ckmin(int &a, int b) {
return a = min(a, b);
}
// so far have r, g, y for each color
// and last char is last
int solve(int r, int g, int y, int last) {
if (r < 0 || g < 0 || y < 0) return INF;
if (vis[r][g][y][last]) return dp[r][g][y][last];
vis[r][g][y][last] = true;
dp[r][g][y][last] = INF;
if (r == 0 && g == 0 && y == 0) return dp[r][g][y][last] = 0;
int pr = r;
int pg = g;
int py = y;
if (last == 0) pr--;
else if (last == 1) pg--;
else py--;
if (pr < 0 || pg < 0 || py < 0) return dp[r][g][y][last];
int to_move = pr + pg + py;
if (last == 0) {
int cur = idx[0][r - 1];
to_move = cur - to_move;
to_move += max(0, g - pref[cur][1]);
to_move += max(0, y - pref[cur][2]);
} else if (last == 1) {
int cur = idx[1][g - 1];
to_move = cur - to_move;
to_move += max(0, r - pref[cur][0]);
to_move += max(0, y - pref[cur][2]);
} else {
int cur = idx[2][y - 1];
to_move = cur - to_move;
to_move += max(0, r - pref[cur][0]);
to_move += max(0, g - pref[cur][1]);
}
for (int i = 0; i < 3; i++) {
if (i == last) continue;
ckmin(dp[r][g][y][last], solve(pr, pg, py, i) + to_move);
}
return dp[r][g][y][last];
}
int main() {
/* cin.tie(0)->sync_with_stdio(0); */
cin >> n;
cin >> s;
for (int i = 0; i < n; i++) {
if (i) {
pref[i][0] = pref[i - 1][0];
pref[i][1] = pref[i - 1][1];
pref[i][2] = pref[i - 1][2];
}
switch (s[i]) {
case 'R':
pref[i][0]++;
idx[0].push_back(i);
break;
case 'G':
pref[i][1]++;
idx[1].push_back(i);
break;
case 'Y':
pref[i][2]++;
idx[2].push_back(i);
break;
}
}
int r = pref[n - 1][0];
int g = pref[n - 1][1];
int y = pref[n - 1][2];
int ans = min({solve(r, g, y, 0), solve(r, g, y, 1), solve(r, g, y, 2)});
if (ans == INF) {
cout << "-1\n";
} else {
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
3 ms |
2924 KB |
Output is correct |
19 |
Correct |
2 ms |
2412 KB |
Output is correct |
20 |
Correct |
2 ms |
2796 KB |
Output is correct |
21 |
Correct |
2 ms |
3052 KB |
Output is correct |
22 |
Correct |
2 ms |
2156 KB |
Output is correct |
23 |
Correct |
2 ms |
2668 KB |
Output is correct |
24 |
Correct |
2 ms |
2028 KB |
Output is correct |
25 |
Correct |
1 ms |
1132 KB |
Output is correct |
26 |
Correct |
1 ms |
1260 KB |
Output is correct |
27 |
Correct |
3 ms |
3052 KB |
Output is correct |
28 |
Correct |
2 ms |
2924 KB |
Output is correct |
29 |
Correct |
3 ms |
2796 KB |
Output is correct |
30 |
Correct |
3 ms |
2816 KB |
Output is correct |
31 |
Correct |
2 ms |
2540 KB |
Output is correct |
32 |
Correct |
2 ms |
2924 KB |
Output is correct |
33 |
Correct |
1 ms |
1388 KB |
Output is correct |
34 |
Correct |
1 ms |
1772 KB |
Output is correct |
35 |
Correct |
2 ms |
2924 KB |
Output is correct |
36 |
Correct |
2 ms |
2412 KB |
Output is correct |
37 |
Correct |
2 ms |
2156 KB |
Output is correct |
38 |
Correct |
2 ms |
2540 KB |
Output is correct |
39 |
Correct |
2 ms |
2924 KB |
Output is correct |
40 |
Correct |
1 ms |
492 KB |
Output is correct |
41 |
Correct |
1 ms |
1004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
4 ms |
4972 KB |
Output is correct |
3 |
Correct |
3 ms |
4972 KB |
Output is correct |
4 |
Correct |
3 ms |
4972 KB |
Output is correct |
5 |
Correct |
3 ms |
4972 KB |
Output is correct |
6 |
Correct |
4 ms |
4972 KB |
Output is correct |
7 |
Correct |
3 ms |
4972 KB |
Output is correct |
8 |
Correct |
3 ms |
4972 KB |
Output is correct |
9 |
Correct |
3 ms |
4972 KB |
Output is correct |
10 |
Correct |
3 ms |
4972 KB |
Output is correct |
11 |
Correct |
4 ms |
4972 KB |
Output is correct |
12 |
Correct |
2 ms |
2796 KB |
Output is correct |
13 |
Correct |
3 ms |
3564 KB |
Output is correct |
14 |
Correct |
3 ms |
4204 KB |
Output is correct |
15 |
Correct |
3 ms |
4972 KB |
Output is correct |
16 |
Correct |
3 ms |
4972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
3 ms |
2924 KB |
Output is correct |
19 |
Correct |
2 ms |
2412 KB |
Output is correct |
20 |
Correct |
2 ms |
2796 KB |
Output is correct |
21 |
Correct |
2 ms |
3052 KB |
Output is correct |
22 |
Correct |
2 ms |
2156 KB |
Output is correct |
23 |
Correct |
2 ms |
2668 KB |
Output is correct |
24 |
Correct |
2 ms |
2028 KB |
Output is correct |
25 |
Correct |
1 ms |
1132 KB |
Output is correct |
26 |
Correct |
1 ms |
1260 KB |
Output is correct |
27 |
Correct |
3 ms |
3052 KB |
Output is correct |
28 |
Correct |
2 ms |
2924 KB |
Output is correct |
29 |
Correct |
3 ms |
2796 KB |
Output is correct |
30 |
Correct |
3 ms |
2816 KB |
Output is correct |
31 |
Correct |
2 ms |
2540 KB |
Output is correct |
32 |
Correct |
2 ms |
2924 KB |
Output is correct |
33 |
Correct |
1 ms |
1388 KB |
Output is correct |
34 |
Correct |
1 ms |
1772 KB |
Output is correct |
35 |
Correct |
2 ms |
2924 KB |
Output is correct |
36 |
Correct |
2 ms |
2412 KB |
Output is correct |
37 |
Correct |
2 ms |
2156 KB |
Output is correct |
38 |
Correct |
2 ms |
2540 KB |
Output is correct |
39 |
Correct |
2 ms |
2924 KB |
Output is correct |
40 |
Correct |
1 ms |
492 KB |
Output is correct |
41 |
Correct |
1 ms |
1004 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
4 ms |
4972 KB |
Output is correct |
44 |
Correct |
3 ms |
4972 KB |
Output is correct |
45 |
Correct |
3 ms |
4972 KB |
Output is correct |
46 |
Correct |
3 ms |
4972 KB |
Output is correct |
47 |
Correct |
4 ms |
4972 KB |
Output is correct |
48 |
Correct |
3 ms |
4972 KB |
Output is correct |
49 |
Correct |
3 ms |
4972 KB |
Output is correct |
50 |
Correct |
3 ms |
4972 KB |
Output is correct |
51 |
Correct |
3 ms |
4972 KB |
Output is correct |
52 |
Correct |
4 ms |
4972 KB |
Output is correct |
53 |
Correct |
2 ms |
2796 KB |
Output is correct |
54 |
Correct |
3 ms |
3564 KB |
Output is correct |
55 |
Correct |
3 ms |
4204 KB |
Output is correct |
56 |
Correct |
3 ms |
4972 KB |
Output is correct |
57 |
Correct |
3 ms |
4972 KB |
Output is correct |
58 |
Correct |
249 ms |
93872 KB |
Output is correct |
59 |
Correct |
253 ms |
115052 KB |
Output is correct |
60 |
Correct |
234 ms |
107264 KB |
Output is correct |
61 |
Correct |
253 ms |
104172 KB |
Output is correct |
62 |
Correct |
8 ms |
12268 KB |
Output is correct |
63 |
Correct |
15 ms |
21100 KB |
Output is correct |
64 |
Correct |
59 ms |
65516 KB |
Output is correct |
65 |
Correct |
116 ms |
92644 KB |
Output is correct |
66 |
Correct |
261 ms |
105580 KB |
Output is correct |
67 |
Correct |
230 ms |
100844 KB |
Output is correct |
68 |
Correct |
274 ms |
108652 KB |
Output is correct |
69 |
Correct |
249 ms |
104940 KB |
Output is correct |
70 |
Correct |
262 ms |
112364 KB |
Output is correct |
71 |
Correct |
238 ms |
102124 KB |
Output is correct |
72 |
Correct |
15 ms |
14316 KB |
Output is correct |
73 |
Correct |
2 ms |
3180 KB |
Output is correct |