#include <bits/stdc++.h>
#define iii tuple<int, int, int>
using namespace std;
const int N = 405;
int n;
int cnt[3], dp[3][N][N][N];
char A[N];
vector<iii> vec[3];
int f(char c) {
if(c == 'R') return 0;
if(c == 'G') return 1;
return 2;
}
int cost(int c, iii a, iii b) {
int ret = 0;
if(c != 0) ret += max(0, get<0>(b) - get<0>(a));
if(c != 1) ret += max(0, get<1>(b) - get<1>(a));
if(c != 2) ret += max(0, get<2>(b) - get<2>(a));
return ret;
}
int main() {
memset(dp, -1, sizeof dp);
scanf("%d %s", &n, A + 1);
for(int i = 1; i <= n; i++) {
++cnt[f(A[i])];
vec[f(A[i])].emplace_back(cnt[0], cnt[1], cnt[2]);
}
int R = cnt[0], G = cnt[1], Y = cnt[2];
for(int i = 0; i <= R; i++) for(int j = 0; j <= G; j++) for(int k = 0; k <= Y; k++) {
if(!i && !j && !k) continue;
dp[0][i][j][k] = dp[1][i][j][k] = dp[2][i][j][k] = 1e9;
if(i) dp[0][i][j][k] = min(dp[1][i-1][j][k], dp[2][i-1][j][k]) + cost(0, vec[0][i-1], {i, j, k});
if(j) dp[1][i][j][k] = min(dp[0][i][j-1][k], dp[2][i][j-1][k]) + cost(1, vec[1][j-1], {i, j, k});
if(k) dp[2][i][j][k] = min(dp[0][i][j][k-1], dp[1][i][j][k-1]) + cost(2, vec[2][k-1], {i, j, k});
}
int ans = 1e9;
for(int i = 0; i < 3; i++) ans = min(ans, dp[i][R][G][Y]);
if(ans == 1e9) printf("-1\n");
else printf("%d\n", ans + 1);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %s", &n, A + 1);
~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
387 ms |
780408 KB |
Output is correct |
2 |
Correct |
367 ms |
780280 KB |
Output is correct |
3 |
Correct |
364 ms |
780280 KB |
Output is correct |
4 |
Correct |
362 ms |
780280 KB |
Output is correct |
5 |
Correct |
378 ms |
780384 KB |
Output is correct |
6 |
Correct |
361 ms |
780280 KB |
Output is correct |
7 |
Correct |
371 ms |
780368 KB |
Output is correct |
8 |
Correct |
364 ms |
780280 KB |
Output is correct |
9 |
Correct |
367 ms |
780368 KB |
Output is correct |
10 |
Correct |
361 ms |
780280 KB |
Output is correct |
11 |
Correct |
369 ms |
780484 KB |
Output is correct |
12 |
Correct |
365 ms |
780280 KB |
Output is correct |
13 |
Correct |
372 ms |
780408 KB |
Output is correct |
14 |
Correct |
361 ms |
780280 KB |
Output is correct |
15 |
Correct |
373 ms |
780408 KB |
Output is correct |
16 |
Correct |
360 ms |
780280 KB |
Output is correct |
17 |
Correct |
369 ms |
780408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
387 ms |
780408 KB |
Output is correct |
2 |
Correct |
367 ms |
780280 KB |
Output is correct |
3 |
Correct |
364 ms |
780280 KB |
Output is correct |
4 |
Correct |
362 ms |
780280 KB |
Output is correct |
5 |
Correct |
378 ms |
780384 KB |
Output is correct |
6 |
Correct |
361 ms |
780280 KB |
Output is correct |
7 |
Correct |
371 ms |
780368 KB |
Output is correct |
8 |
Correct |
364 ms |
780280 KB |
Output is correct |
9 |
Correct |
367 ms |
780368 KB |
Output is correct |
10 |
Correct |
361 ms |
780280 KB |
Output is correct |
11 |
Correct |
369 ms |
780484 KB |
Output is correct |
12 |
Correct |
365 ms |
780280 KB |
Output is correct |
13 |
Correct |
372 ms |
780408 KB |
Output is correct |
14 |
Correct |
361 ms |
780280 KB |
Output is correct |
15 |
Correct |
373 ms |
780408 KB |
Output is correct |
16 |
Correct |
360 ms |
780280 KB |
Output is correct |
17 |
Correct |
369 ms |
780408 KB |
Output is correct |
18 |
Correct |
362 ms |
780280 KB |
Output is correct |
19 |
Correct |
373 ms |
780428 KB |
Output is correct |
20 |
Correct |
372 ms |
780280 KB |
Output is correct |
21 |
Correct |
363 ms |
780408 KB |
Output is correct |
22 |
Correct |
364 ms |
780280 KB |
Output is correct |
23 |
Correct |
360 ms |
780280 KB |
Output is correct |
24 |
Correct |
364 ms |
780280 KB |
Output is correct |
25 |
Correct |
377 ms |
780280 KB |
Output is correct |
26 |
Correct |
375 ms |
780316 KB |
Output is correct |
27 |
Correct |
368 ms |
780408 KB |
Output is correct |
28 |
Correct |
383 ms |
780280 KB |
Output is correct |
29 |
Correct |
373 ms |
780408 KB |
Output is correct |
30 |
Correct |
365 ms |
780280 KB |
Output is correct |
31 |
Correct |
379 ms |
780408 KB |
Output is correct |
32 |
Correct |
361 ms |
780280 KB |
Output is correct |
33 |
Correct |
388 ms |
780284 KB |
Output is correct |
34 |
Correct |
366 ms |
780280 KB |
Output is correct |
35 |
Correct |
368 ms |
780280 KB |
Output is correct |
36 |
Correct |
365 ms |
780280 KB |
Output is correct |
37 |
Correct |
363 ms |
780280 KB |
Output is correct |
38 |
Correct |
367 ms |
780280 KB |
Output is correct |
39 |
Correct |
366 ms |
780280 KB |
Output is correct |
40 |
Correct |
369 ms |
780280 KB |
Output is correct |
41 |
Correct |
365 ms |
780280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
386 ms |
780376 KB |
Output is correct |
2 |
Correct |
369 ms |
780280 KB |
Output is correct |
3 |
Correct |
372 ms |
780280 KB |
Output is correct |
4 |
Correct |
371 ms |
780280 KB |
Output is correct |
5 |
Correct |
377 ms |
780408 KB |
Output is correct |
6 |
Correct |
382 ms |
780280 KB |
Output is correct |
7 |
Correct |
372 ms |
780384 KB |
Output is correct |
8 |
Correct |
373 ms |
780280 KB |
Output is correct |
9 |
Correct |
374 ms |
780408 KB |
Output is correct |
10 |
Correct |
365 ms |
780280 KB |
Output is correct |
11 |
Correct |
377 ms |
780492 KB |
Output is correct |
12 |
Correct |
368 ms |
780300 KB |
Output is correct |
13 |
Correct |
380 ms |
780408 KB |
Output is correct |
14 |
Correct |
366 ms |
780280 KB |
Output is correct |
15 |
Correct |
390 ms |
780408 KB |
Output is correct |
16 |
Correct |
376 ms |
780280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
387 ms |
780408 KB |
Output is correct |
2 |
Correct |
367 ms |
780280 KB |
Output is correct |
3 |
Correct |
364 ms |
780280 KB |
Output is correct |
4 |
Correct |
362 ms |
780280 KB |
Output is correct |
5 |
Correct |
378 ms |
780384 KB |
Output is correct |
6 |
Correct |
361 ms |
780280 KB |
Output is correct |
7 |
Correct |
371 ms |
780368 KB |
Output is correct |
8 |
Correct |
364 ms |
780280 KB |
Output is correct |
9 |
Correct |
367 ms |
780368 KB |
Output is correct |
10 |
Correct |
361 ms |
780280 KB |
Output is correct |
11 |
Correct |
369 ms |
780484 KB |
Output is correct |
12 |
Correct |
365 ms |
780280 KB |
Output is correct |
13 |
Correct |
372 ms |
780408 KB |
Output is correct |
14 |
Correct |
361 ms |
780280 KB |
Output is correct |
15 |
Correct |
373 ms |
780408 KB |
Output is correct |
16 |
Correct |
360 ms |
780280 KB |
Output is correct |
17 |
Correct |
369 ms |
780408 KB |
Output is correct |
18 |
Correct |
362 ms |
780280 KB |
Output is correct |
19 |
Correct |
373 ms |
780428 KB |
Output is correct |
20 |
Correct |
372 ms |
780280 KB |
Output is correct |
21 |
Correct |
363 ms |
780408 KB |
Output is correct |
22 |
Correct |
364 ms |
780280 KB |
Output is correct |
23 |
Correct |
360 ms |
780280 KB |
Output is correct |
24 |
Correct |
364 ms |
780280 KB |
Output is correct |
25 |
Correct |
377 ms |
780280 KB |
Output is correct |
26 |
Correct |
375 ms |
780316 KB |
Output is correct |
27 |
Correct |
368 ms |
780408 KB |
Output is correct |
28 |
Correct |
383 ms |
780280 KB |
Output is correct |
29 |
Correct |
373 ms |
780408 KB |
Output is correct |
30 |
Correct |
365 ms |
780280 KB |
Output is correct |
31 |
Correct |
379 ms |
780408 KB |
Output is correct |
32 |
Correct |
361 ms |
780280 KB |
Output is correct |
33 |
Correct |
388 ms |
780284 KB |
Output is correct |
34 |
Correct |
366 ms |
780280 KB |
Output is correct |
35 |
Correct |
368 ms |
780280 KB |
Output is correct |
36 |
Correct |
365 ms |
780280 KB |
Output is correct |
37 |
Correct |
363 ms |
780280 KB |
Output is correct |
38 |
Correct |
367 ms |
780280 KB |
Output is correct |
39 |
Correct |
366 ms |
780280 KB |
Output is correct |
40 |
Correct |
369 ms |
780280 KB |
Output is correct |
41 |
Correct |
365 ms |
780280 KB |
Output is correct |
42 |
Correct |
386 ms |
780376 KB |
Output is correct |
43 |
Correct |
369 ms |
780280 KB |
Output is correct |
44 |
Correct |
372 ms |
780280 KB |
Output is correct |
45 |
Correct |
371 ms |
780280 KB |
Output is correct |
46 |
Correct |
377 ms |
780408 KB |
Output is correct |
47 |
Correct |
382 ms |
780280 KB |
Output is correct |
48 |
Correct |
372 ms |
780384 KB |
Output is correct |
49 |
Correct |
373 ms |
780280 KB |
Output is correct |
50 |
Correct |
374 ms |
780408 KB |
Output is correct |
51 |
Correct |
365 ms |
780280 KB |
Output is correct |
52 |
Correct |
377 ms |
780492 KB |
Output is correct |
53 |
Correct |
368 ms |
780300 KB |
Output is correct |
54 |
Correct |
380 ms |
780408 KB |
Output is correct |
55 |
Correct |
366 ms |
780280 KB |
Output is correct |
56 |
Correct |
390 ms |
780408 KB |
Output is correct |
57 |
Correct |
376 ms |
780280 KB |
Output is correct |
58 |
Correct |
398 ms |
780408 KB |
Output is correct |
59 |
Correct |
384 ms |
780284 KB |
Output is correct |
60 |
Correct |
401 ms |
780408 KB |
Output is correct |
61 |
Correct |
393 ms |
780280 KB |
Output is correct |
62 |
Correct |
397 ms |
780292 KB |
Output is correct |
63 |
Correct |
399 ms |
780280 KB |
Output is correct |
64 |
Correct |
391 ms |
780372 KB |
Output is correct |
65 |
Correct |
381 ms |
780280 KB |
Output is correct |
66 |
Correct |
389 ms |
780320 KB |
Output is correct |
67 |
Correct |
383 ms |
780364 KB |
Output is correct |
68 |
Correct |
390 ms |
780364 KB |
Output is correct |
69 |
Correct |
378 ms |
780280 KB |
Output is correct |
70 |
Correct |
393 ms |
780536 KB |
Output is correct |
71 |
Correct |
383 ms |
780312 KB |
Output is correct |
72 |
Correct |
402 ms |
780328 KB |
Output is correct |
73 |
Correct |
374 ms |
780280 KB |
Output is correct |