#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define x first
#define y second
using namespace std;
using ll = long long;
#define int uint16_t
void umin(int &a, int b) {
a = min(a, b);
}
void umax(int &a, int b) {
a = max(a, b);
}
const int N = 401;
const int INF = UINT16_MAX;
int n;
string str;
int a[N];
int pr[N];
int pg[N];
int py[N];
int wr[N];
int wg[N];
int wy[N];
int dp[N][N][N][3];
// R, G, Y, Last
signed main() {
#ifdef LC
assert(freopen("input.txt", "r", stdin));
#endif
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> str;
for (int i = 0; i < n; ++i) {
pr[i + 1] = pr[i];
pg[i + 1] = pg[i];
py[i + 1] = py[i];
if (str[i] == 'R') {
a[i] = 0;
wr[pr[i]] = i;
++pr[i + 1];
} else if (str[i] == 'G') {
a[i] = 1;
wg[pg[i]] = i;
++pg[i + 1];
} else {
assert(str[i] == 'Y');
a[i] = 2;
wy[py[i]] = i;
++py[i + 1];
}
}
fill_n(dp[0][0][0], 3 * N * N * N, INF);
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (int r = 0; r <= pr[n]; ++r) {
for (int g = 0; g <= pg[n]; ++g) {
for (int y = 0; y <= py[n]; ++y) {
for (int l = 0; l < 3; ++l) {
int v = dp[r][g][y][l];
if (v >= INF) {
continue;
}
if (l != 0 && r < pr[n]) {
int i = wr[r];
umin(dp[r + 1][g][y][0], v + abs(pg[i] - g) + abs(py[i] - y));
}
if (l != 1 && g < pg[n]) {
int i = wg[g];
umin(dp[r][g + 1][y][1], v + abs(pr[i] - r) + abs(py[i] - y));
}
if (l != 2 && y < py[n]) {
int i = wy[y];
umin(dp[r][g][y + 1][2], v + abs(pr[i] - r) + abs(pg[i] - g));
}
}
}
}
}
int r = pr[n], g = pg[n], y = py[n];
int ans = min(min(dp[r][g][y][0], dp[r][g][y][1]), dp[r][g][y][2]);
if (ans >= INF) {
cout << "-1\n";
return 0;
}
assert(ans % 2 == 0);
cout << ans / 2 << "\n";
return 0;
}
/**
RRGYY
->
RYRGY
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
320 ms |
378872 KB |
Output is correct |
2 |
Correct |
314 ms |
378872 KB |
Output is correct |
3 |
Correct |
313 ms |
378872 KB |
Output is correct |
4 |
Correct |
314 ms |
379000 KB |
Output is correct |
5 |
Correct |
315 ms |
378872 KB |
Output is correct |
6 |
Correct |
328 ms |
379012 KB |
Output is correct |
7 |
Correct |
321 ms |
379000 KB |
Output is correct |
8 |
Correct |
314 ms |
378880 KB |
Output is correct |
9 |
Correct |
342 ms |
378992 KB |
Output is correct |
10 |
Correct |
314 ms |
378872 KB |
Output is correct |
11 |
Correct |
335 ms |
379000 KB |
Output is correct |
12 |
Correct |
317 ms |
378976 KB |
Output is correct |
13 |
Correct |
317 ms |
378888 KB |
Output is correct |
14 |
Correct |
314 ms |
378872 KB |
Output is correct |
15 |
Correct |
323 ms |
379000 KB |
Output is correct |
16 |
Correct |
326 ms |
379000 KB |
Output is correct |
17 |
Correct |
324 ms |
379004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
320 ms |
378872 KB |
Output is correct |
2 |
Correct |
314 ms |
378872 KB |
Output is correct |
3 |
Correct |
313 ms |
378872 KB |
Output is correct |
4 |
Correct |
314 ms |
379000 KB |
Output is correct |
5 |
Correct |
315 ms |
378872 KB |
Output is correct |
6 |
Correct |
328 ms |
379012 KB |
Output is correct |
7 |
Correct |
321 ms |
379000 KB |
Output is correct |
8 |
Correct |
314 ms |
378880 KB |
Output is correct |
9 |
Correct |
342 ms |
378992 KB |
Output is correct |
10 |
Correct |
314 ms |
378872 KB |
Output is correct |
11 |
Correct |
335 ms |
379000 KB |
Output is correct |
12 |
Correct |
317 ms |
378976 KB |
Output is correct |
13 |
Correct |
317 ms |
378888 KB |
Output is correct |
14 |
Correct |
314 ms |
378872 KB |
Output is correct |
15 |
Correct |
323 ms |
379000 KB |
Output is correct |
16 |
Correct |
326 ms |
379000 KB |
Output is correct |
17 |
Correct |
324 ms |
379004 KB |
Output is correct |
18 |
Correct |
306 ms |
378872 KB |
Output is correct |
19 |
Correct |
320 ms |
379000 KB |
Output is correct |
20 |
Correct |
317 ms |
378872 KB |
Output is correct |
21 |
Correct |
323 ms |
378872 KB |
Output is correct |
22 |
Correct |
312 ms |
379000 KB |
Output is correct |
23 |
Correct |
315 ms |
378872 KB |
Output is correct |
24 |
Correct |
320 ms |
379128 KB |
Output is correct |
25 |
Correct |
331 ms |
379052 KB |
Output is correct |
26 |
Correct |
339 ms |
378872 KB |
Output is correct |
27 |
Correct |
313 ms |
378872 KB |
Output is correct |
28 |
Correct |
318 ms |
378872 KB |
Output is correct |
29 |
Correct |
316 ms |
379004 KB |
Output is correct |
30 |
Correct |
315 ms |
378872 KB |
Output is correct |
31 |
Correct |
327 ms |
379000 KB |
Output is correct |
32 |
Correct |
324 ms |
378872 KB |
Output is correct |
33 |
Correct |
319 ms |
378876 KB |
Output is correct |
34 |
Correct |
318 ms |
378872 KB |
Output is correct |
35 |
Correct |
327 ms |
379044 KB |
Output is correct |
36 |
Correct |
309 ms |
378844 KB |
Output is correct |
37 |
Correct |
321 ms |
379000 KB |
Output is correct |
38 |
Correct |
315 ms |
378872 KB |
Output is correct |
39 |
Correct |
310 ms |
378872 KB |
Output is correct |
40 |
Correct |
314 ms |
379000 KB |
Output is correct |
41 |
Correct |
312 ms |
379000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
313 ms |
379000 KB |
Output is correct |
2 |
Correct |
311 ms |
378872 KB |
Output is correct |
3 |
Correct |
325 ms |
379032 KB |
Output is correct |
4 |
Correct |
320 ms |
379000 KB |
Output is correct |
5 |
Correct |
328 ms |
378872 KB |
Output is correct |
6 |
Correct |
317 ms |
379000 KB |
Output is correct |
7 |
Correct |
316 ms |
378872 KB |
Output is correct |
8 |
Correct |
326 ms |
379000 KB |
Output is correct |
9 |
Correct |
330 ms |
378872 KB |
Output is correct |
10 |
Correct |
328 ms |
378872 KB |
Output is correct |
11 |
Correct |
330 ms |
378872 KB |
Output is correct |
12 |
Correct |
326 ms |
378872 KB |
Output is correct |
13 |
Correct |
323 ms |
379128 KB |
Output is correct |
14 |
Correct |
314 ms |
378872 KB |
Output is correct |
15 |
Correct |
328 ms |
379000 KB |
Output is correct |
16 |
Correct |
314 ms |
378872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
320 ms |
378872 KB |
Output is correct |
2 |
Correct |
314 ms |
378872 KB |
Output is correct |
3 |
Correct |
313 ms |
378872 KB |
Output is correct |
4 |
Correct |
314 ms |
379000 KB |
Output is correct |
5 |
Correct |
315 ms |
378872 KB |
Output is correct |
6 |
Correct |
328 ms |
379012 KB |
Output is correct |
7 |
Correct |
321 ms |
379000 KB |
Output is correct |
8 |
Correct |
314 ms |
378880 KB |
Output is correct |
9 |
Correct |
342 ms |
378992 KB |
Output is correct |
10 |
Correct |
314 ms |
378872 KB |
Output is correct |
11 |
Correct |
335 ms |
379000 KB |
Output is correct |
12 |
Correct |
317 ms |
378976 KB |
Output is correct |
13 |
Correct |
317 ms |
378888 KB |
Output is correct |
14 |
Correct |
314 ms |
378872 KB |
Output is correct |
15 |
Correct |
323 ms |
379000 KB |
Output is correct |
16 |
Correct |
326 ms |
379000 KB |
Output is correct |
17 |
Correct |
324 ms |
379004 KB |
Output is correct |
18 |
Correct |
306 ms |
378872 KB |
Output is correct |
19 |
Correct |
320 ms |
379000 KB |
Output is correct |
20 |
Correct |
317 ms |
378872 KB |
Output is correct |
21 |
Correct |
323 ms |
378872 KB |
Output is correct |
22 |
Correct |
312 ms |
379000 KB |
Output is correct |
23 |
Correct |
315 ms |
378872 KB |
Output is correct |
24 |
Correct |
320 ms |
379128 KB |
Output is correct |
25 |
Correct |
331 ms |
379052 KB |
Output is correct |
26 |
Correct |
339 ms |
378872 KB |
Output is correct |
27 |
Correct |
313 ms |
378872 KB |
Output is correct |
28 |
Correct |
318 ms |
378872 KB |
Output is correct |
29 |
Correct |
316 ms |
379004 KB |
Output is correct |
30 |
Correct |
315 ms |
378872 KB |
Output is correct |
31 |
Correct |
327 ms |
379000 KB |
Output is correct |
32 |
Correct |
324 ms |
378872 KB |
Output is correct |
33 |
Correct |
319 ms |
378876 KB |
Output is correct |
34 |
Correct |
318 ms |
378872 KB |
Output is correct |
35 |
Correct |
327 ms |
379044 KB |
Output is correct |
36 |
Correct |
309 ms |
378844 KB |
Output is correct |
37 |
Correct |
321 ms |
379000 KB |
Output is correct |
38 |
Correct |
315 ms |
378872 KB |
Output is correct |
39 |
Correct |
310 ms |
378872 KB |
Output is correct |
40 |
Correct |
314 ms |
379000 KB |
Output is correct |
41 |
Correct |
312 ms |
379000 KB |
Output is correct |
42 |
Correct |
313 ms |
379000 KB |
Output is correct |
43 |
Correct |
311 ms |
378872 KB |
Output is correct |
44 |
Correct |
325 ms |
379032 KB |
Output is correct |
45 |
Correct |
320 ms |
379000 KB |
Output is correct |
46 |
Correct |
328 ms |
378872 KB |
Output is correct |
47 |
Correct |
317 ms |
379000 KB |
Output is correct |
48 |
Correct |
316 ms |
378872 KB |
Output is correct |
49 |
Correct |
326 ms |
379000 KB |
Output is correct |
50 |
Correct |
330 ms |
378872 KB |
Output is correct |
51 |
Correct |
328 ms |
378872 KB |
Output is correct |
52 |
Correct |
330 ms |
378872 KB |
Output is correct |
53 |
Correct |
326 ms |
378872 KB |
Output is correct |
54 |
Correct |
323 ms |
379128 KB |
Output is correct |
55 |
Correct |
314 ms |
378872 KB |
Output is correct |
56 |
Correct |
328 ms |
379000 KB |
Output is correct |
57 |
Correct |
314 ms |
378872 KB |
Output is correct |
58 |
Correct |
361 ms |
378948 KB |
Output is correct |
59 |
Correct |
359 ms |
378872 KB |
Output is correct |
60 |
Correct |
364 ms |
379000 KB |
Output is correct |
61 |
Correct |
357 ms |
379000 KB |
Output is correct |
62 |
Correct |
332 ms |
378984 KB |
Output is correct |
63 |
Correct |
322 ms |
378872 KB |
Output is correct |
64 |
Correct |
345 ms |
379000 KB |
Output is correct |
65 |
Correct |
333 ms |
378872 KB |
Output is correct |
66 |
Correct |
368 ms |
378872 KB |
Output is correct |
67 |
Correct |
374 ms |
379128 KB |
Output is correct |
68 |
Correct |
369 ms |
378856 KB |
Output is correct |
69 |
Correct |
351 ms |
379128 KB |
Output is correct |
70 |
Correct |
381 ms |
378872 KB |
Output is correct |
71 |
Correct |
349 ms |
378932 KB |
Output is correct |
72 |
Correct |
326 ms |
378872 KB |
Output is correct |
73 |
Correct |
323 ms |
379000 KB |
Output is correct |