#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 405;
int dp[3][MAXN][MAXN][MAXN];
int antes[3][MAXN][MAXN];
vector<int> l[3];
string ORD = "RGY";
const int INF = 0x3f3f3f3f;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> v(n + 1);
for (int i = 1; i <= n; i++) {
char c;
cin >> c;
for (int j = 0; j < 3; j++) {
if (c == ORD[j]) v[i] = j;
}
l[v[i]].push_back(i);
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < ((int)l[i].size()); j++) {
int p = 0;
for (int k = 1; k <= n; k++) {
antes[i][j][k] = antes[i][j][k - 1];
if ((p <= j) && (l[i][p] == k)) {
antes[i][j][k]++;
p++;
}
}
}
}
vector<int> t(3);
for (int i = 0; i < 3; i++) {
t[i] = ((int)l[i].size());
}
memset(dp, -1, sizeof dp);
dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int q1 = 0; q1 <= min(i, t[0]); q1++) {
for (int q2 = 0; q2 <= min(i - q1, t[1]); q2++) {
for (int c = 0; c < 3; c++) {
int q[3] = {q1, q2, i - q1 - q2};
bool flag = 0;
for (int x = 0; x < 3; x++) {
if (q[x] > t[x]) flag = 1;
}
if (flag) continue;
q[c]--;
if (q[c] < 0) continue;
int pegar = l[c][q[c]];
for (int ant = 0; ant < 3; ant++) {
if (ant == c) continue;
int val = dp[ant][i - 1][q[0]][q[1]];
if (val == -1) continue;
int cost = pegar - 1;
if (q[0] > 0) cost -= antes[0][q[0] - 1][pegar];
if (q[1] > 0) cost -= antes[1][q[1] - 1][pegar];
if (q[2] > 0) cost -= antes[2][q[2] - 1][pegar];
if (dp[c][i][q1][q2] == -1) dp[c][i][q1][q2] = (val + cost);
else dp[c][i][q1][q2] = min(dp[c][i][q1][q2], val + cost);
}
}
}
}
}
int ans = INF, a = l[0].size(), b = l[1].size();
for (int i = 0; i < 3; i++) {
if (dp[i][n][a][b] != -1) ans = min(ans, dp[i][n][a][b]);
}
if (ans == INF) cout << "-1\n";
else cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
344 ms |
780368 KB |
Output is correct |
2 |
Correct |
351 ms |
780276 KB |
Output is correct |
3 |
Correct |
355 ms |
780232 KB |
Output is correct |
4 |
Correct |
360 ms |
780424 KB |
Output is correct |
5 |
Correct |
353 ms |
780368 KB |
Output is correct |
6 |
Correct |
360 ms |
780368 KB |
Output is correct |
7 |
Correct |
356 ms |
780296 KB |
Output is correct |
8 |
Correct |
354 ms |
780304 KB |
Output is correct |
9 |
Correct |
356 ms |
780372 KB |
Output is correct |
10 |
Correct |
395 ms |
780284 KB |
Output is correct |
11 |
Correct |
351 ms |
780372 KB |
Output is correct |
12 |
Correct |
369 ms |
780416 KB |
Output is correct |
13 |
Correct |
375 ms |
780304 KB |
Output is correct |
14 |
Correct |
336 ms |
780372 KB |
Output is correct |
15 |
Correct |
332 ms |
780372 KB |
Output is correct |
16 |
Correct |
343 ms |
780300 KB |
Output is correct |
17 |
Correct |
335 ms |
780248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
344 ms |
780368 KB |
Output is correct |
2 |
Correct |
351 ms |
780276 KB |
Output is correct |
3 |
Correct |
355 ms |
780232 KB |
Output is correct |
4 |
Correct |
360 ms |
780424 KB |
Output is correct |
5 |
Correct |
353 ms |
780368 KB |
Output is correct |
6 |
Correct |
360 ms |
780368 KB |
Output is correct |
7 |
Correct |
356 ms |
780296 KB |
Output is correct |
8 |
Correct |
354 ms |
780304 KB |
Output is correct |
9 |
Correct |
356 ms |
780372 KB |
Output is correct |
10 |
Correct |
395 ms |
780284 KB |
Output is correct |
11 |
Correct |
351 ms |
780372 KB |
Output is correct |
12 |
Correct |
369 ms |
780416 KB |
Output is correct |
13 |
Correct |
375 ms |
780304 KB |
Output is correct |
14 |
Correct |
336 ms |
780372 KB |
Output is correct |
15 |
Correct |
332 ms |
780372 KB |
Output is correct |
16 |
Correct |
343 ms |
780300 KB |
Output is correct |
17 |
Correct |
335 ms |
780248 KB |
Output is correct |
18 |
Correct |
332 ms |
780428 KB |
Output is correct |
19 |
Correct |
332 ms |
780328 KB |
Output is correct |
20 |
Correct |
373 ms |
780372 KB |
Output is correct |
21 |
Correct |
376 ms |
780368 KB |
Output is correct |
22 |
Correct |
363 ms |
780368 KB |
Output is correct |
23 |
Correct |
355 ms |
780312 KB |
Output is correct |
24 |
Correct |
368 ms |
780376 KB |
Output is correct |
25 |
Correct |
359 ms |
780412 KB |
Output is correct |
26 |
Correct |
361 ms |
780316 KB |
Output is correct |
27 |
Correct |
365 ms |
780368 KB |
Output is correct |
28 |
Correct |
371 ms |
780368 KB |
Output is correct |
29 |
Correct |
360 ms |
780488 KB |
Output is correct |
30 |
Correct |
364 ms |
780456 KB |
Output is correct |
31 |
Correct |
371 ms |
780372 KB |
Output is correct |
32 |
Correct |
370 ms |
780484 KB |
Output is correct |
33 |
Correct |
375 ms |
780372 KB |
Output is correct |
34 |
Correct |
369 ms |
780372 KB |
Output is correct |
35 |
Correct |
384 ms |
780380 KB |
Output is correct |
36 |
Correct |
357 ms |
780412 KB |
Output is correct |
37 |
Correct |
403 ms |
780396 KB |
Output is correct |
38 |
Correct |
364 ms |
780368 KB |
Output is correct |
39 |
Correct |
359 ms |
780312 KB |
Output is correct |
40 |
Correct |
355 ms |
780356 KB |
Output is correct |
41 |
Correct |
349 ms |
780540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
368 ms |
780400 KB |
Output is correct |
2 |
Correct |
409 ms |
781052 KB |
Output is correct |
3 |
Correct |
387 ms |
781048 KB |
Output is correct |
4 |
Correct |
428 ms |
780880 KB |
Output is correct |
5 |
Correct |
412 ms |
780884 KB |
Output is correct |
6 |
Correct |
417 ms |
780880 KB |
Output is correct |
7 |
Correct |
405 ms |
780880 KB |
Output is correct |
8 |
Correct |
424 ms |
780884 KB |
Output is correct |
9 |
Correct |
414 ms |
781032 KB |
Output is correct |
10 |
Correct |
409 ms |
780940 KB |
Output is correct |
11 |
Correct |
416 ms |
780880 KB |
Output is correct |
12 |
Correct |
359 ms |
780628 KB |
Output is correct |
13 |
Correct |
381 ms |
780628 KB |
Output is correct |
14 |
Correct |
383 ms |
780884 KB |
Output is correct |
15 |
Correct |
409 ms |
780880 KB |
Output is correct |
16 |
Correct |
403 ms |
780964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
344 ms |
780368 KB |
Output is correct |
2 |
Correct |
351 ms |
780276 KB |
Output is correct |
3 |
Correct |
355 ms |
780232 KB |
Output is correct |
4 |
Correct |
360 ms |
780424 KB |
Output is correct |
5 |
Correct |
353 ms |
780368 KB |
Output is correct |
6 |
Correct |
360 ms |
780368 KB |
Output is correct |
7 |
Correct |
356 ms |
780296 KB |
Output is correct |
8 |
Correct |
354 ms |
780304 KB |
Output is correct |
9 |
Correct |
356 ms |
780372 KB |
Output is correct |
10 |
Correct |
395 ms |
780284 KB |
Output is correct |
11 |
Correct |
351 ms |
780372 KB |
Output is correct |
12 |
Correct |
369 ms |
780416 KB |
Output is correct |
13 |
Correct |
375 ms |
780304 KB |
Output is correct |
14 |
Correct |
336 ms |
780372 KB |
Output is correct |
15 |
Correct |
332 ms |
780372 KB |
Output is correct |
16 |
Correct |
343 ms |
780300 KB |
Output is correct |
17 |
Correct |
335 ms |
780248 KB |
Output is correct |
18 |
Correct |
332 ms |
780428 KB |
Output is correct |
19 |
Correct |
332 ms |
780328 KB |
Output is correct |
20 |
Correct |
373 ms |
780372 KB |
Output is correct |
21 |
Correct |
376 ms |
780368 KB |
Output is correct |
22 |
Correct |
363 ms |
780368 KB |
Output is correct |
23 |
Correct |
355 ms |
780312 KB |
Output is correct |
24 |
Correct |
368 ms |
780376 KB |
Output is correct |
25 |
Correct |
359 ms |
780412 KB |
Output is correct |
26 |
Correct |
361 ms |
780316 KB |
Output is correct |
27 |
Correct |
365 ms |
780368 KB |
Output is correct |
28 |
Correct |
371 ms |
780368 KB |
Output is correct |
29 |
Correct |
360 ms |
780488 KB |
Output is correct |
30 |
Correct |
364 ms |
780456 KB |
Output is correct |
31 |
Correct |
371 ms |
780372 KB |
Output is correct |
32 |
Correct |
370 ms |
780484 KB |
Output is correct |
33 |
Correct |
375 ms |
780372 KB |
Output is correct |
34 |
Correct |
369 ms |
780372 KB |
Output is correct |
35 |
Correct |
384 ms |
780380 KB |
Output is correct |
36 |
Correct |
357 ms |
780412 KB |
Output is correct |
37 |
Correct |
403 ms |
780396 KB |
Output is correct |
38 |
Correct |
364 ms |
780368 KB |
Output is correct |
39 |
Correct |
359 ms |
780312 KB |
Output is correct |
40 |
Correct |
355 ms |
780356 KB |
Output is correct |
41 |
Correct |
349 ms |
780540 KB |
Output is correct |
42 |
Correct |
368 ms |
780400 KB |
Output is correct |
43 |
Correct |
409 ms |
781052 KB |
Output is correct |
44 |
Correct |
387 ms |
781048 KB |
Output is correct |
45 |
Correct |
428 ms |
780880 KB |
Output is correct |
46 |
Correct |
412 ms |
780884 KB |
Output is correct |
47 |
Correct |
417 ms |
780880 KB |
Output is correct |
48 |
Correct |
405 ms |
780880 KB |
Output is correct |
49 |
Correct |
424 ms |
780884 KB |
Output is correct |
50 |
Correct |
414 ms |
781032 KB |
Output is correct |
51 |
Correct |
409 ms |
780940 KB |
Output is correct |
52 |
Correct |
416 ms |
780880 KB |
Output is correct |
53 |
Correct |
359 ms |
780628 KB |
Output is correct |
54 |
Correct |
381 ms |
780628 KB |
Output is correct |
55 |
Correct |
383 ms |
780884 KB |
Output is correct |
56 |
Correct |
409 ms |
780880 KB |
Output is correct |
57 |
Correct |
403 ms |
780964 KB |
Output is correct |
58 |
Correct |
466 ms |
781048 KB |
Output is correct |
59 |
Correct |
459 ms |
781216 KB |
Output is correct |
60 |
Correct |
454 ms |
781140 KB |
Output is correct |
61 |
Correct |
456 ms |
780884 KB |
Output is correct |
62 |
Correct |
429 ms |
780884 KB |
Output is correct |
63 |
Correct |
416 ms |
780888 KB |
Output is correct |
64 |
Correct |
421 ms |
780880 KB |
Output is correct |
65 |
Correct |
449 ms |
780884 KB |
Output is correct |
66 |
Correct |
460 ms |
781208 KB |
Output is correct |
67 |
Correct |
453 ms |
780880 KB |
Output is correct |
68 |
Correct |
450 ms |
781140 KB |
Output is correct |
69 |
Correct |
451 ms |
781084 KB |
Output is correct |
70 |
Correct |
458 ms |
780880 KB |
Output is correct |
71 |
Correct |
463 ms |
781304 KB |
Output is correct |
72 |
Correct |
379 ms |
781072 KB |
Output is correct |
73 |
Correct |
365 ms |
780884 KB |
Output is correct |