#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() {
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++;
}
}
}
}
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 <= i; q1++) {
for (int q2 = 0; q2 <= (i - q1); 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] > ((int)l[x].size())) flag = 1;
}
if (flag) continue;
q[c]--;
if (q[c] < 0) continue;
int pegar = l[c][q[c]];
//if ((i == 1) && (c == 0)) cout << pegar << " " << q[0] << "\n";
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);
}
//cout << ORD[c] << " " << i << " " << q1 << " " << q2 << " " << i-q1-q2 << " ";
//cout << dp[c][i][q1][q2] << "\n";
}
}
}
}
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 |
354 ms |
780368 KB |
Output is correct |
2 |
Correct |
358 ms |
780420 KB |
Output is correct |
3 |
Correct |
358 ms |
780372 KB |
Output is correct |
4 |
Correct |
349 ms |
780344 KB |
Output is correct |
5 |
Correct |
345 ms |
780368 KB |
Output is correct |
6 |
Correct |
349 ms |
780260 KB |
Output is correct |
7 |
Correct |
344 ms |
780216 KB |
Output is correct |
8 |
Correct |
353 ms |
780432 KB |
Output is correct |
9 |
Correct |
351 ms |
780556 KB |
Output is correct |
10 |
Correct |
347 ms |
780360 KB |
Output is correct |
11 |
Correct |
374 ms |
780372 KB |
Output is correct |
12 |
Correct |
346 ms |
780372 KB |
Output is correct |
13 |
Correct |
368 ms |
780372 KB |
Output is correct |
14 |
Correct |
351 ms |
780372 KB |
Output is correct |
15 |
Correct |
355 ms |
780368 KB |
Output is correct |
16 |
Correct |
359 ms |
780368 KB |
Output is correct |
17 |
Correct |
354 ms |
780372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
354 ms |
780368 KB |
Output is correct |
2 |
Correct |
358 ms |
780420 KB |
Output is correct |
3 |
Correct |
358 ms |
780372 KB |
Output is correct |
4 |
Correct |
349 ms |
780344 KB |
Output is correct |
5 |
Correct |
345 ms |
780368 KB |
Output is correct |
6 |
Correct |
349 ms |
780260 KB |
Output is correct |
7 |
Correct |
344 ms |
780216 KB |
Output is correct |
8 |
Correct |
353 ms |
780432 KB |
Output is correct |
9 |
Correct |
351 ms |
780556 KB |
Output is correct |
10 |
Correct |
347 ms |
780360 KB |
Output is correct |
11 |
Correct |
374 ms |
780372 KB |
Output is correct |
12 |
Correct |
346 ms |
780372 KB |
Output is correct |
13 |
Correct |
368 ms |
780372 KB |
Output is correct |
14 |
Correct |
351 ms |
780372 KB |
Output is correct |
15 |
Correct |
355 ms |
780368 KB |
Output is correct |
16 |
Correct |
359 ms |
780368 KB |
Output is correct |
17 |
Correct |
354 ms |
780372 KB |
Output is correct |
18 |
Correct |
361 ms |
780324 KB |
Output is correct |
19 |
Correct |
351 ms |
780368 KB |
Output is correct |
20 |
Correct |
348 ms |
780368 KB |
Output is correct |
21 |
Correct |
362 ms |
780452 KB |
Output is correct |
22 |
Correct |
353 ms |
780368 KB |
Output is correct |
23 |
Correct |
348 ms |
780336 KB |
Output is correct |
24 |
Correct |
355 ms |
780368 KB |
Output is correct |
25 |
Correct |
350 ms |
780276 KB |
Output is correct |
26 |
Correct |
350 ms |
780392 KB |
Output is correct |
27 |
Correct |
361 ms |
780292 KB |
Output is correct |
28 |
Correct |
346 ms |
780288 KB |
Output is correct |
29 |
Correct |
343 ms |
780404 KB |
Output is correct |
30 |
Correct |
348 ms |
780368 KB |
Output is correct |
31 |
Correct |
351 ms |
780444 KB |
Output is correct |
32 |
Correct |
362 ms |
780372 KB |
Output is correct |
33 |
Correct |
339 ms |
780368 KB |
Output is correct |
34 |
Correct |
354 ms |
780424 KB |
Output is correct |
35 |
Correct |
348 ms |
780372 KB |
Output is correct |
36 |
Correct |
358 ms |
780368 KB |
Output is correct |
37 |
Correct |
356 ms |
780272 KB |
Output is correct |
38 |
Correct |
332 ms |
780368 KB |
Output is correct |
39 |
Correct |
323 ms |
780368 KB |
Output is correct |
40 |
Correct |
328 ms |
780336 KB |
Output is correct |
41 |
Correct |
320 ms |
780372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
331 ms |
780204 KB |
Output is correct |
2 |
Correct |
463 ms |
781140 KB |
Output is correct |
3 |
Correct |
474 ms |
780884 KB |
Output is correct |
4 |
Correct |
470 ms |
780936 KB |
Output is correct |
5 |
Correct |
471 ms |
781000 KB |
Output is correct |
6 |
Correct |
467 ms |
780900 KB |
Output is correct |
7 |
Correct |
475 ms |
781152 KB |
Output is correct |
8 |
Correct |
491 ms |
780964 KB |
Output is correct |
9 |
Correct |
495 ms |
780944 KB |
Output is correct |
10 |
Correct |
478 ms |
780908 KB |
Output is correct |
11 |
Correct |
467 ms |
780852 KB |
Output is correct |
12 |
Correct |
391 ms |
780620 KB |
Output is correct |
13 |
Correct |
407 ms |
780628 KB |
Output is correct |
14 |
Correct |
427 ms |
780804 KB |
Output is correct |
15 |
Correct |
495 ms |
780880 KB |
Output is correct |
16 |
Correct |
463 ms |
781140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
354 ms |
780368 KB |
Output is correct |
2 |
Correct |
358 ms |
780420 KB |
Output is correct |
3 |
Correct |
358 ms |
780372 KB |
Output is correct |
4 |
Correct |
349 ms |
780344 KB |
Output is correct |
5 |
Correct |
345 ms |
780368 KB |
Output is correct |
6 |
Correct |
349 ms |
780260 KB |
Output is correct |
7 |
Correct |
344 ms |
780216 KB |
Output is correct |
8 |
Correct |
353 ms |
780432 KB |
Output is correct |
9 |
Correct |
351 ms |
780556 KB |
Output is correct |
10 |
Correct |
347 ms |
780360 KB |
Output is correct |
11 |
Correct |
374 ms |
780372 KB |
Output is correct |
12 |
Correct |
346 ms |
780372 KB |
Output is correct |
13 |
Correct |
368 ms |
780372 KB |
Output is correct |
14 |
Correct |
351 ms |
780372 KB |
Output is correct |
15 |
Correct |
355 ms |
780368 KB |
Output is correct |
16 |
Correct |
359 ms |
780368 KB |
Output is correct |
17 |
Correct |
354 ms |
780372 KB |
Output is correct |
18 |
Correct |
361 ms |
780324 KB |
Output is correct |
19 |
Correct |
351 ms |
780368 KB |
Output is correct |
20 |
Correct |
348 ms |
780368 KB |
Output is correct |
21 |
Correct |
362 ms |
780452 KB |
Output is correct |
22 |
Correct |
353 ms |
780368 KB |
Output is correct |
23 |
Correct |
348 ms |
780336 KB |
Output is correct |
24 |
Correct |
355 ms |
780368 KB |
Output is correct |
25 |
Correct |
350 ms |
780276 KB |
Output is correct |
26 |
Correct |
350 ms |
780392 KB |
Output is correct |
27 |
Correct |
361 ms |
780292 KB |
Output is correct |
28 |
Correct |
346 ms |
780288 KB |
Output is correct |
29 |
Correct |
343 ms |
780404 KB |
Output is correct |
30 |
Correct |
348 ms |
780368 KB |
Output is correct |
31 |
Correct |
351 ms |
780444 KB |
Output is correct |
32 |
Correct |
362 ms |
780372 KB |
Output is correct |
33 |
Correct |
339 ms |
780368 KB |
Output is correct |
34 |
Correct |
354 ms |
780424 KB |
Output is correct |
35 |
Correct |
348 ms |
780372 KB |
Output is correct |
36 |
Correct |
358 ms |
780368 KB |
Output is correct |
37 |
Correct |
356 ms |
780272 KB |
Output is correct |
38 |
Correct |
332 ms |
780368 KB |
Output is correct |
39 |
Correct |
323 ms |
780368 KB |
Output is correct |
40 |
Correct |
328 ms |
780336 KB |
Output is correct |
41 |
Correct |
320 ms |
780372 KB |
Output is correct |
42 |
Correct |
331 ms |
780204 KB |
Output is correct |
43 |
Correct |
463 ms |
781140 KB |
Output is correct |
44 |
Correct |
474 ms |
780884 KB |
Output is correct |
45 |
Correct |
470 ms |
780936 KB |
Output is correct |
46 |
Correct |
471 ms |
781000 KB |
Output is correct |
47 |
Correct |
467 ms |
780900 KB |
Output is correct |
48 |
Correct |
475 ms |
781152 KB |
Output is correct |
49 |
Correct |
491 ms |
780964 KB |
Output is correct |
50 |
Correct |
495 ms |
780944 KB |
Output is correct |
51 |
Correct |
478 ms |
780908 KB |
Output is correct |
52 |
Correct |
467 ms |
780852 KB |
Output is correct |
53 |
Correct |
391 ms |
780620 KB |
Output is correct |
54 |
Correct |
407 ms |
780628 KB |
Output is correct |
55 |
Correct |
427 ms |
780804 KB |
Output is correct |
56 |
Correct |
495 ms |
780880 KB |
Output is correct |
57 |
Correct |
463 ms |
781140 KB |
Output is correct |
58 |
Execution timed out |
546 ms |
781068 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |