#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++;
}
}
}
}
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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
329 ms |
780340 KB |
Output is correct |
2 |
Correct |
338 ms |
780372 KB |
Output is correct |
3 |
Correct |
334 ms |
780372 KB |
Output is correct |
4 |
Correct |
321 ms |
780380 KB |
Output is correct |
5 |
Correct |
382 ms |
780368 KB |
Output is correct |
6 |
Correct |
349 ms |
780368 KB |
Output is correct |
7 |
Correct |
376 ms |
780368 KB |
Output is correct |
8 |
Correct |
363 ms |
780624 KB |
Output is correct |
9 |
Correct |
358 ms |
780368 KB |
Output is correct |
10 |
Correct |
361 ms |
780468 KB |
Output is correct |
11 |
Correct |
352 ms |
780372 KB |
Output is correct |
12 |
Correct |
363 ms |
780496 KB |
Output is correct |
13 |
Correct |
356 ms |
780452 KB |
Output is correct |
14 |
Correct |
349 ms |
780372 KB |
Output is correct |
15 |
Correct |
360 ms |
780368 KB |
Output is correct |
16 |
Correct |
367 ms |
780372 KB |
Output is correct |
17 |
Correct |
362 ms |
780224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
329 ms |
780340 KB |
Output is correct |
2 |
Correct |
338 ms |
780372 KB |
Output is correct |
3 |
Correct |
334 ms |
780372 KB |
Output is correct |
4 |
Correct |
321 ms |
780380 KB |
Output is correct |
5 |
Correct |
382 ms |
780368 KB |
Output is correct |
6 |
Correct |
349 ms |
780368 KB |
Output is correct |
7 |
Correct |
376 ms |
780368 KB |
Output is correct |
8 |
Correct |
363 ms |
780624 KB |
Output is correct |
9 |
Correct |
358 ms |
780368 KB |
Output is correct |
10 |
Correct |
361 ms |
780468 KB |
Output is correct |
11 |
Correct |
352 ms |
780372 KB |
Output is correct |
12 |
Correct |
363 ms |
780496 KB |
Output is correct |
13 |
Correct |
356 ms |
780452 KB |
Output is correct |
14 |
Correct |
349 ms |
780372 KB |
Output is correct |
15 |
Correct |
360 ms |
780368 KB |
Output is correct |
16 |
Correct |
367 ms |
780372 KB |
Output is correct |
17 |
Correct |
362 ms |
780224 KB |
Output is correct |
18 |
Correct |
359 ms |
780552 KB |
Output is correct |
19 |
Correct |
366 ms |
780372 KB |
Output is correct |
20 |
Correct |
357 ms |
780468 KB |
Output is correct |
21 |
Correct |
359 ms |
780376 KB |
Output is correct |
22 |
Correct |
352 ms |
780372 KB |
Output is correct |
23 |
Correct |
369 ms |
780304 KB |
Output is correct |
24 |
Correct |
361 ms |
780368 KB |
Output is correct |
25 |
Correct |
379 ms |
780368 KB |
Output is correct |
26 |
Correct |
347 ms |
780484 KB |
Output is correct |
27 |
Correct |
359 ms |
780456 KB |
Output is correct |
28 |
Correct |
350 ms |
780368 KB |
Output is correct |
29 |
Correct |
364 ms |
780460 KB |
Output is correct |
30 |
Correct |
363 ms |
780372 KB |
Output is correct |
31 |
Correct |
375 ms |
780352 KB |
Output is correct |
32 |
Correct |
362 ms |
780372 KB |
Output is correct |
33 |
Correct |
371 ms |
780372 KB |
Output is correct |
34 |
Correct |
356 ms |
780368 KB |
Output is correct |
35 |
Correct |
367 ms |
780372 KB |
Output is correct |
36 |
Correct |
359 ms |
780368 KB |
Output is correct |
37 |
Correct |
362 ms |
780624 KB |
Output is correct |
38 |
Correct |
355 ms |
780368 KB |
Output is correct |
39 |
Correct |
370 ms |
780496 KB |
Output is correct |
40 |
Correct |
344 ms |
780352 KB |
Output is correct |
41 |
Correct |
350 ms |
780368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
352 ms |
780400 KB |
Output is correct |
2 |
Correct |
464 ms |
780988 KB |
Output is correct |
3 |
Correct |
485 ms |
780884 KB |
Output is correct |
4 |
Correct |
473 ms |
780884 KB |
Output is correct |
5 |
Correct |
471 ms |
780948 KB |
Output is correct |
6 |
Correct |
490 ms |
780884 KB |
Output is correct |
7 |
Correct |
487 ms |
780880 KB |
Output is correct |
8 |
Correct |
468 ms |
780884 KB |
Output is correct |
9 |
Correct |
468 ms |
780884 KB |
Output is correct |
10 |
Correct |
477 ms |
780836 KB |
Output is correct |
11 |
Correct |
481 ms |
780884 KB |
Output is correct |
12 |
Correct |
366 ms |
780624 KB |
Output is correct |
13 |
Correct |
388 ms |
780884 KB |
Output is correct |
14 |
Correct |
427 ms |
780880 KB |
Output is correct |
15 |
Correct |
443 ms |
780900 KB |
Output is correct |
16 |
Correct |
450 ms |
781048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
329 ms |
780340 KB |
Output is correct |
2 |
Correct |
338 ms |
780372 KB |
Output is correct |
3 |
Correct |
334 ms |
780372 KB |
Output is correct |
4 |
Correct |
321 ms |
780380 KB |
Output is correct |
5 |
Correct |
382 ms |
780368 KB |
Output is correct |
6 |
Correct |
349 ms |
780368 KB |
Output is correct |
7 |
Correct |
376 ms |
780368 KB |
Output is correct |
8 |
Correct |
363 ms |
780624 KB |
Output is correct |
9 |
Correct |
358 ms |
780368 KB |
Output is correct |
10 |
Correct |
361 ms |
780468 KB |
Output is correct |
11 |
Correct |
352 ms |
780372 KB |
Output is correct |
12 |
Correct |
363 ms |
780496 KB |
Output is correct |
13 |
Correct |
356 ms |
780452 KB |
Output is correct |
14 |
Correct |
349 ms |
780372 KB |
Output is correct |
15 |
Correct |
360 ms |
780368 KB |
Output is correct |
16 |
Correct |
367 ms |
780372 KB |
Output is correct |
17 |
Correct |
362 ms |
780224 KB |
Output is correct |
18 |
Correct |
359 ms |
780552 KB |
Output is correct |
19 |
Correct |
366 ms |
780372 KB |
Output is correct |
20 |
Correct |
357 ms |
780468 KB |
Output is correct |
21 |
Correct |
359 ms |
780376 KB |
Output is correct |
22 |
Correct |
352 ms |
780372 KB |
Output is correct |
23 |
Correct |
369 ms |
780304 KB |
Output is correct |
24 |
Correct |
361 ms |
780368 KB |
Output is correct |
25 |
Correct |
379 ms |
780368 KB |
Output is correct |
26 |
Correct |
347 ms |
780484 KB |
Output is correct |
27 |
Correct |
359 ms |
780456 KB |
Output is correct |
28 |
Correct |
350 ms |
780368 KB |
Output is correct |
29 |
Correct |
364 ms |
780460 KB |
Output is correct |
30 |
Correct |
363 ms |
780372 KB |
Output is correct |
31 |
Correct |
375 ms |
780352 KB |
Output is correct |
32 |
Correct |
362 ms |
780372 KB |
Output is correct |
33 |
Correct |
371 ms |
780372 KB |
Output is correct |
34 |
Correct |
356 ms |
780368 KB |
Output is correct |
35 |
Correct |
367 ms |
780372 KB |
Output is correct |
36 |
Correct |
359 ms |
780368 KB |
Output is correct |
37 |
Correct |
362 ms |
780624 KB |
Output is correct |
38 |
Correct |
355 ms |
780368 KB |
Output is correct |
39 |
Correct |
370 ms |
780496 KB |
Output is correct |
40 |
Correct |
344 ms |
780352 KB |
Output is correct |
41 |
Correct |
350 ms |
780368 KB |
Output is correct |
42 |
Correct |
352 ms |
780400 KB |
Output is correct |
43 |
Correct |
464 ms |
780988 KB |
Output is correct |
44 |
Correct |
485 ms |
780884 KB |
Output is correct |
45 |
Correct |
473 ms |
780884 KB |
Output is correct |
46 |
Correct |
471 ms |
780948 KB |
Output is correct |
47 |
Correct |
490 ms |
780884 KB |
Output is correct |
48 |
Correct |
487 ms |
780880 KB |
Output is correct |
49 |
Correct |
468 ms |
780884 KB |
Output is correct |
50 |
Correct |
468 ms |
780884 KB |
Output is correct |
51 |
Correct |
477 ms |
780836 KB |
Output is correct |
52 |
Correct |
481 ms |
780884 KB |
Output is correct |
53 |
Correct |
366 ms |
780624 KB |
Output is correct |
54 |
Correct |
388 ms |
780884 KB |
Output is correct |
55 |
Correct |
427 ms |
780880 KB |
Output is correct |
56 |
Correct |
443 ms |
780900 KB |
Output is correct |
57 |
Correct |
450 ms |
781048 KB |
Output is correct |
58 |
Execution timed out |
506 ms |
780884 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |