#include <bits/stdc++.h>
#define iii tuple<int, int, int>
using namespace std;
const int N = 405;
int n;
int cnt[3], dp[N][N][N][3];
char s[N];
vector<iii> vec[3];
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()
{
scanf("%d %s",&n,s);
for( int i = 0, f ; i < n ; i++ ) {
if( s[i] == 'R') f = 0;
else if( s[i] == 'G' ) f = 1;
else f = 2;
cnt[f]++, vec[f].emplace_back( cnt[0], cnt[1], cnt[2] );
}
for( int i = 0 ; i <= cnt[0] ; i++ ) for( int j = 0 ; j <= cnt[1] ; j++ ) for( int k = 0 ; k <= cnt[2] ; k++ ) {
if( !i && !j && !k ) continue;
dp[i][j][k][0] = dp[i][j][k][1] = dp[i][j][k][2] = 1e9;
if( i ) dp[i][j][k][0] = min( dp[i-1][j][k][1], dp[i-1][j][k][2] ) + cost( 0, vec[0][i-1], { i, j, k } );
if( j ) dp[i][j][k][1] = min( dp[i][j-1][k][0], dp[i][j-1][k][2] ) + cost( 1, vec[1][j-1], { i, j, k } );
if( k ) dp[i][j][k][2] = min( dp[i][j][k-1][0], dp[i][j][k-1][1] ) + cost( 2, vec[2][k-1], { i, j, k } );
}
int ans = 1e9;
for( int i = 0 ; i < 3 ; i++ ) ans = min( ans, dp[cnt[0]][cnt[1]][cnt[2]][i] );
if( ans == 1e9 ) printf("-1\n");
else printf("%d\n", ans);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:23:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %s",&n,s);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
5 ms |
2304 KB |
Output is correct |
19 |
Correct |
5 ms |
1920 KB |
Output is correct |
20 |
Correct |
5 ms |
2304 KB |
Output is correct |
21 |
Correct |
5 ms |
2432 KB |
Output is correct |
22 |
Correct |
5 ms |
1664 KB |
Output is correct |
23 |
Correct |
5 ms |
2176 KB |
Output is correct |
24 |
Correct |
5 ms |
1664 KB |
Output is correct |
25 |
Correct |
6 ms |
3968 KB |
Output is correct |
26 |
Correct |
6 ms |
4096 KB |
Output is correct |
27 |
Correct |
6 ms |
3072 KB |
Output is correct |
28 |
Correct |
6 ms |
2292 KB |
Output is correct |
29 |
Correct |
5 ms |
2304 KB |
Output is correct |
30 |
Correct |
5 ms |
2304 KB |
Output is correct |
31 |
Correct |
5 ms |
2048 KB |
Output is correct |
32 |
Correct |
6 ms |
2432 KB |
Output is correct |
33 |
Correct |
6 ms |
3840 KB |
Output is correct |
34 |
Correct |
8 ms |
3584 KB |
Output is correct |
35 |
Correct |
6 ms |
2688 KB |
Output is correct |
36 |
Correct |
5 ms |
1920 KB |
Output is correct |
37 |
Correct |
5 ms |
1792 KB |
Output is correct |
38 |
Correct |
6 ms |
2176 KB |
Output is correct |
39 |
Correct |
6 ms |
2560 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
2304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
74 ms |
162936 KB |
Output is correct |
3 |
Correct |
73 ms |
162168 KB |
Output is correct |
4 |
Correct |
75 ms |
162936 KB |
Output is correct |
5 |
Correct |
75 ms |
163064 KB |
Output is correct |
6 |
Correct |
75 ms |
162936 KB |
Output is correct |
7 |
Correct |
74 ms |
162192 KB |
Output is correct |
8 |
Correct |
73 ms |
162160 KB |
Output is correct |
9 |
Correct |
73 ms |
161400 KB |
Output is correct |
10 |
Correct |
76 ms |
162936 KB |
Output is correct |
11 |
Correct |
79 ms |
163124 KB |
Output is correct |
12 |
Correct |
24 ms |
44032 KB |
Output is correct |
13 |
Correct |
39 ms |
77184 KB |
Output is correct |
14 |
Correct |
52 ms |
111352 KB |
Output is correct |
15 |
Correct |
73 ms |
162936 KB |
Output is correct |
16 |
Correct |
73 ms |
162940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
5 ms |
2304 KB |
Output is correct |
19 |
Correct |
5 ms |
1920 KB |
Output is correct |
20 |
Correct |
5 ms |
2304 KB |
Output is correct |
21 |
Correct |
5 ms |
2432 KB |
Output is correct |
22 |
Correct |
5 ms |
1664 KB |
Output is correct |
23 |
Correct |
5 ms |
2176 KB |
Output is correct |
24 |
Correct |
5 ms |
1664 KB |
Output is correct |
25 |
Correct |
6 ms |
3968 KB |
Output is correct |
26 |
Correct |
6 ms |
4096 KB |
Output is correct |
27 |
Correct |
6 ms |
3072 KB |
Output is correct |
28 |
Correct |
6 ms |
2292 KB |
Output is correct |
29 |
Correct |
5 ms |
2304 KB |
Output is correct |
30 |
Correct |
5 ms |
2304 KB |
Output is correct |
31 |
Correct |
5 ms |
2048 KB |
Output is correct |
32 |
Correct |
6 ms |
2432 KB |
Output is correct |
33 |
Correct |
6 ms |
3840 KB |
Output is correct |
34 |
Correct |
8 ms |
3584 KB |
Output is correct |
35 |
Correct |
6 ms |
2688 KB |
Output is correct |
36 |
Correct |
5 ms |
1920 KB |
Output is correct |
37 |
Correct |
5 ms |
1792 KB |
Output is correct |
38 |
Correct |
6 ms |
2176 KB |
Output is correct |
39 |
Correct |
6 ms |
2560 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
2304 KB |
Output is correct |
42 |
Correct |
5 ms |
512 KB |
Output is correct |
43 |
Correct |
74 ms |
162936 KB |
Output is correct |
44 |
Correct |
73 ms |
162168 KB |
Output is correct |
45 |
Correct |
75 ms |
162936 KB |
Output is correct |
46 |
Correct |
75 ms |
163064 KB |
Output is correct |
47 |
Correct |
75 ms |
162936 KB |
Output is correct |
48 |
Correct |
74 ms |
162192 KB |
Output is correct |
49 |
Correct |
73 ms |
162160 KB |
Output is correct |
50 |
Correct |
73 ms |
161400 KB |
Output is correct |
51 |
Correct |
76 ms |
162936 KB |
Output is correct |
52 |
Correct |
79 ms |
163124 KB |
Output is correct |
53 |
Correct |
24 ms |
44032 KB |
Output is correct |
54 |
Correct |
39 ms |
77184 KB |
Output is correct |
55 |
Correct |
52 ms |
111352 KB |
Output is correct |
56 |
Correct |
73 ms |
162936 KB |
Output is correct |
57 |
Correct |
73 ms |
162940 KB |
Output is correct |
58 |
Correct |
51 ms |
76280 KB |
Output is correct |
59 |
Correct |
59 ms |
96632 KB |
Output is correct |
60 |
Correct |
55 ms |
87800 KB |
Output is correct |
61 |
Correct |
55 ms |
84856 KB |
Output is correct |
62 |
Correct |
73 ms |
160632 KB |
Output is correct |
63 |
Correct |
76 ms |
160248 KB |
Output is correct |
64 |
Correct |
76 ms |
150136 KB |
Output is correct |
65 |
Correct |
74 ms |
138616 KB |
Output is correct |
66 |
Correct |
55 ms |
86104 KB |
Output is correct |
67 |
Correct |
53 ms |
82936 KB |
Output is correct |
68 |
Correct |
57 ms |
88696 KB |
Output is correct |
69 |
Correct |
56 ms |
85492 KB |
Output is correct |
70 |
Correct |
59 ms |
92664 KB |
Output is correct |
71 |
Correct |
54 ms |
83960 KB |
Output is correct |
72 |
Correct |
26 ms |
41600 KB |
Output is correct |
73 |
Correct |
7 ms |
2944 KB |
Output is correct |