#include <bits/stdc++.h>
#define RED 0
#define GRE 1
#define YEL 2
using namespace std;
int N;
int memo[405][405][405][3];
int pos[405][3], pre[405][3];
int pr, py, pg;
int dp(int r, int g, int y, int last) {
if(r == pr && g == pg && y == py) return 0;
if(memo[r][g][y][last] != -1) return memo[r][g][y][last];
int res = 1000000000;
if(r < pr && last != 0) {
int now = pos[r][0];
res = min(res, dp(r + 1, g, y, 0) + max(pre[now][1] - g, 0) + max(pre[now][2] - y, 0));
}
if(g < pg && last != 1) {
int now = pos[g][1];
res = min(res, dp(r, g + 1, y, 1) + max(pre[now][0] - r, 0) + max(pre[now][2] - y, 0));
}
if(y < py && last != 2) {
int now = pos[y][2];
res = min(res, dp(r, g, y + 1, 2) + max(pre[now][0] - r, 0) + max(pre[now][1] - g, 0));
}
return memo[r][g][y][last] = res;
}
int main() {
memset(memo, -1, sizeof(memo));
ios_base::sync_with_stdio(0);
cin >> N;
char ch;
for(int i = 1; i <= N; i++) {
cin >> ch;
if(ch == 'R') {
pre[i][0] = 1;
pos[pr++][0] = i;
}else if(ch == 'G') {
pre[i][1] = 1;
pos[pg++][1] = i;
}else {
pre[i][2] = 1;
pos[py++][2] = i;
}
pre[i][0] += pre[i - 1][0];
pre[i][1] += pre[i - 1][1];
pre[i][2] += pre[i - 1][2];
}
int ans = 1000000000;
if(pr > 0) {
int now = pos[0][0];
ans = min(ans, dp(1, 0, 0, 0) + pre[now][1] + pre[now][2]);
}
if(pg > 0) {
int now = pos[0][1];
ans = min(ans, dp(0, 1, 0, 1) + pre[now][0] + pre[now][2]);
}
if(py > 0) {
int now = pos[0][2];
ans = min(ans, dp(0, 0, 1, 2) + pre[now][0] + pre[now][1]);
}
if(ans != 1000000000) {
cout << ans << "\n";
}else cout << "-1\n";
cin >> N;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
408 ms |
780408 KB |
Output is correct |
2 |
Correct |
399 ms |
780408 KB |
Output is correct |
3 |
Correct |
385 ms |
780408 KB |
Output is correct |
4 |
Correct |
379 ms |
780408 KB |
Output is correct |
5 |
Correct |
389 ms |
780408 KB |
Output is correct |
6 |
Correct |
383 ms |
780280 KB |
Output is correct |
7 |
Correct |
393 ms |
780436 KB |
Output is correct |
8 |
Correct |
380 ms |
780408 KB |
Output is correct |
9 |
Correct |
381 ms |
780280 KB |
Output is correct |
10 |
Correct |
394 ms |
780280 KB |
Output is correct |
11 |
Correct |
437 ms |
780408 KB |
Output is correct |
12 |
Correct |
403 ms |
780408 KB |
Output is correct |
13 |
Correct |
407 ms |
780536 KB |
Output is correct |
14 |
Correct |
410 ms |
780408 KB |
Output is correct |
15 |
Correct |
392 ms |
780412 KB |
Output is correct |
16 |
Correct |
440 ms |
780408 KB |
Output is correct |
17 |
Correct |
425 ms |
780412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
408 ms |
780408 KB |
Output is correct |
2 |
Correct |
399 ms |
780408 KB |
Output is correct |
3 |
Correct |
385 ms |
780408 KB |
Output is correct |
4 |
Correct |
379 ms |
780408 KB |
Output is correct |
5 |
Correct |
389 ms |
780408 KB |
Output is correct |
6 |
Correct |
383 ms |
780280 KB |
Output is correct |
7 |
Correct |
393 ms |
780436 KB |
Output is correct |
8 |
Correct |
380 ms |
780408 KB |
Output is correct |
9 |
Correct |
381 ms |
780280 KB |
Output is correct |
10 |
Correct |
394 ms |
780280 KB |
Output is correct |
11 |
Correct |
437 ms |
780408 KB |
Output is correct |
12 |
Correct |
403 ms |
780408 KB |
Output is correct |
13 |
Correct |
407 ms |
780536 KB |
Output is correct |
14 |
Correct |
410 ms |
780408 KB |
Output is correct |
15 |
Correct |
392 ms |
780412 KB |
Output is correct |
16 |
Correct |
440 ms |
780408 KB |
Output is correct |
17 |
Correct |
425 ms |
780412 KB |
Output is correct |
18 |
Correct |
386 ms |
780280 KB |
Output is correct |
19 |
Correct |
380 ms |
780408 KB |
Output is correct |
20 |
Correct |
385 ms |
780412 KB |
Output is correct |
21 |
Correct |
393 ms |
780408 KB |
Output is correct |
22 |
Correct |
425 ms |
780280 KB |
Output is correct |
23 |
Correct |
414 ms |
780408 KB |
Output is correct |
24 |
Correct |
384 ms |
780408 KB |
Output is correct |
25 |
Correct |
392 ms |
780408 KB |
Output is correct |
26 |
Correct |
393 ms |
780328 KB |
Output is correct |
27 |
Correct |
409 ms |
780384 KB |
Output is correct |
28 |
Correct |
409 ms |
780280 KB |
Output is correct |
29 |
Correct |
406 ms |
780432 KB |
Output is correct |
30 |
Correct |
404 ms |
780408 KB |
Output is correct |
31 |
Correct |
420 ms |
780540 KB |
Output is correct |
32 |
Correct |
401 ms |
780412 KB |
Output is correct |
33 |
Correct |
427 ms |
780280 KB |
Output is correct |
34 |
Correct |
439 ms |
780280 KB |
Output is correct |
35 |
Correct |
415 ms |
780408 KB |
Output is correct |
36 |
Correct |
386 ms |
780384 KB |
Output is correct |
37 |
Correct |
386 ms |
780408 KB |
Output is correct |
38 |
Correct |
428 ms |
780408 KB |
Output is correct |
39 |
Correct |
416 ms |
780540 KB |
Output is correct |
40 |
Correct |
401 ms |
780408 KB |
Output is correct |
41 |
Correct |
394 ms |
780280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
436 ms |
780352 KB |
Output is correct |
2 |
Correct |
383 ms |
780408 KB |
Output is correct |
3 |
Correct |
448 ms |
780408 KB |
Output is correct |
4 |
Correct |
442 ms |
780408 KB |
Output is correct |
5 |
Correct |
400 ms |
780476 KB |
Output is correct |
6 |
Correct |
398 ms |
780408 KB |
Output is correct |
7 |
Correct |
420 ms |
780580 KB |
Output is correct |
8 |
Correct |
405 ms |
780408 KB |
Output is correct |
9 |
Correct |
383 ms |
780408 KB |
Output is correct |
10 |
Correct |
432 ms |
780464 KB |
Output is correct |
11 |
Correct |
381 ms |
780408 KB |
Output is correct |
12 |
Correct |
392 ms |
780408 KB |
Output is correct |
13 |
Correct |
380 ms |
780408 KB |
Output is correct |
14 |
Correct |
408 ms |
780384 KB |
Output is correct |
15 |
Correct |
418 ms |
780408 KB |
Output is correct |
16 |
Correct |
406 ms |
780536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
408 ms |
780408 KB |
Output is correct |
2 |
Correct |
399 ms |
780408 KB |
Output is correct |
3 |
Correct |
385 ms |
780408 KB |
Output is correct |
4 |
Correct |
379 ms |
780408 KB |
Output is correct |
5 |
Correct |
389 ms |
780408 KB |
Output is correct |
6 |
Correct |
383 ms |
780280 KB |
Output is correct |
7 |
Correct |
393 ms |
780436 KB |
Output is correct |
8 |
Correct |
380 ms |
780408 KB |
Output is correct |
9 |
Correct |
381 ms |
780280 KB |
Output is correct |
10 |
Correct |
394 ms |
780280 KB |
Output is correct |
11 |
Correct |
437 ms |
780408 KB |
Output is correct |
12 |
Correct |
403 ms |
780408 KB |
Output is correct |
13 |
Correct |
407 ms |
780536 KB |
Output is correct |
14 |
Correct |
410 ms |
780408 KB |
Output is correct |
15 |
Correct |
392 ms |
780412 KB |
Output is correct |
16 |
Correct |
440 ms |
780408 KB |
Output is correct |
17 |
Correct |
425 ms |
780412 KB |
Output is correct |
18 |
Correct |
386 ms |
780280 KB |
Output is correct |
19 |
Correct |
380 ms |
780408 KB |
Output is correct |
20 |
Correct |
385 ms |
780412 KB |
Output is correct |
21 |
Correct |
393 ms |
780408 KB |
Output is correct |
22 |
Correct |
425 ms |
780280 KB |
Output is correct |
23 |
Correct |
414 ms |
780408 KB |
Output is correct |
24 |
Correct |
384 ms |
780408 KB |
Output is correct |
25 |
Correct |
392 ms |
780408 KB |
Output is correct |
26 |
Correct |
393 ms |
780328 KB |
Output is correct |
27 |
Correct |
409 ms |
780384 KB |
Output is correct |
28 |
Correct |
409 ms |
780280 KB |
Output is correct |
29 |
Correct |
406 ms |
780432 KB |
Output is correct |
30 |
Correct |
404 ms |
780408 KB |
Output is correct |
31 |
Correct |
420 ms |
780540 KB |
Output is correct |
32 |
Correct |
401 ms |
780412 KB |
Output is correct |
33 |
Correct |
427 ms |
780280 KB |
Output is correct |
34 |
Correct |
439 ms |
780280 KB |
Output is correct |
35 |
Correct |
415 ms |
780408 KB |
Output is correct |
36 |
Correct |
386 ms |
780384 KB |
Output is correct |
37 |
Correct |
386 ms |
780408 KB |
Output is correct |
38 |
Correct |
428 ms |
780408 KB |
Output is correct |
39 |
Correct |
416 ms |
780540 KB |
Output is correct |
40 |
Correct |
401 ms |
780408 KB |
Output is correct |
41 |
Correct |
394 ms |
780280 KB |
Output is correct |
42 |
Correct |
436 ms |
780352 KB |
Output is correct |
43 |
Correct |
383 ms |
780408 KB |
Output is correct |
44 |
Correct |
448 ms |
780408 KB |
Output is correct |
45 |
Correct |
442 ms |
780408 KB |
Output is correct |
46 |
Correct |
400 ms |
780476 KB |
Output is correct |
47 |
Correct |
398 ms |
780408 KB |
Output is correct |
48 |
Correct |
420 ms |
780580 KB |
Output is correct |
49 |
Correct |
405 ms |
780408 KB |
Output is correct |
50 |
Correct |
383 ms |
780408 KB |
Output is correct |
51 |
Correct |
432 ms |
780464 KB |
Output is correct |
52 |
Correct |
381 ms |
780408 KB |
Output is correct |
53 |
Correct |
392 ms |
780408 KB |
Output is correct |
54 |
Correct |
380 ms |
780408 KB |
Output is correct |
55 |
Correct |
408 ms |
780384 KB |
Output is correct |
56 |
Correct |
418 ms |
780408 KB |
Output is correct |
57 |
Correct |
406 ms |
780536 KB |
Output is correct |
58 |
Execution timed out |
558 ms |
780536 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |