#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
#define int long long
using namespace std;
const long long oo = 1000000000000000000;
long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos;
/*
ViHHiPuh
(( `'-""``""-'` ))
)-__-_.._-__-(
/ --- (o _ o) --- \
\ .-* ( .0. ) *-. /
_'-. ,_ '=' _, .-'_
/ `;#'#'# - #'#'#;` \
\_)) -----'#'----- ((_/
# --------- #
'# ------- ------ #'
/..-'# ------- #'-.\
_\...-\'# -- #'/-.../_
((____)- '#' -(____))
cout << fixed << setprecision(6) << x;
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
freopen ( "sum.in", "r", stdin )
*/
int dp[ 401 ][ 401 ][ 401 ][ 3 ];
vector <pair<int, pair<int, int> > > v, vv, v1;
int f( int a, int b ) {
return min( a, b );
}
main () {
int n;
cin >> n;
string s;
cin >> s;
for ( int i = 0; i < s.size(); i ++ ) {
if ( s[ i ] == 'R' ) {
sum ++;
v.push_back( { i, make_pair( vv.size(), v1.size() ) } );
}
if ( s[ i ] == 'Y' ) {
num ++;
v1.push_back( { i, make_pair( v.size(), vv.size() ) } );
}
if ( s[ i ] == 'G' ) {
mx ++;
vv.push_back( { i, make_pair( v.size(), v1.size() ) } );
}
}
if ( max( mx, max( num, sum ) ) > ( n + 1 ) / 2 ) {
cout << "-1";
return 0;
}
for ( int i = 0; i <= sum; i ++ ) {
for ( int j = 0; j <= mx; j ++ ) {
for ( int k = 0; k <= num; k ++ ) {
if ( i + j + k == 0 )
continue;
dp[ i ][ j ][ k ][ 1 ] = dp[ i ][ j ][ k ][ 2 ] = dp[ i ][ j ][ k ][ 3 ] = oo;
}
}
}
for ( int a = 0; a <= sum; a ++ ) {
for ( int b = 0; b <= mx; b ++ ) {
for ( int c = 0; c <= num; c ++ ) {
if ( a ) {
dp[ a ][ b ][ c ][ 1 ] = min( dp[ a - 1 ][ b ][ c ][ 2 ], dp[ a - 1 ][ b ][ c ][ 3 ] );
mn = v[ a - 1 ].first;
mn -= a - 1;
mn -= f( v[ a - 1 ].second.first, b );
mn -= f( v[ a - 1 ].second.second, c );
dp[ a ][ b ][ c ][ 1 ] += mn;
}
if ( b ) {
dp[ a ][ b ][ c ][ 2 ] = min( dp[ a ][ b - 1 ][ c ][ 1 ], dp[ a ][ b - 1 ][ c ][ 3 ] );
mn = vv[ b - 1 ].first;
mn -= b - 1;
mn -= f( vv[ b - 1 ].second.first, a );
mn -= f( vv[ b - 1 ].second.second, c );
dp[ a ][ b ][ c ][ 2 ] += mn;
}
if ( c ) {
dp[ a ][ b ][ c ][ 3 ] = min( dp[ a ][ b ][ c - 1 ][ 1 ], dp[ a ][ b ][ c - 1 ][ 2 ] );
mn = v1[ c - 1 ].first;
mn -= c - 1;
mn -= f( v1[ c - 1 ].second.first, a );
mn -= f( v1[ c - 1 ].second.second, b );
dp[ a ][ b ][ c ][ 3 ] += mn;
}
// cout << a << " " << b << " " << c << " - " << dp[ a ][ b ][ c ][ 1 ] << " " << dp[ a ][ b ][ c ][ 2 ] << " " << dp[ a ][ b ][ c ][ 3 ] << "\n";
}
}
}
cout << min( dp[ sum ][ mx ][ num ][ 1 ], min( dp[ sum ][ mx ][ num ][ 2 ], dp[ sum ][ mx ][ num ][ 3 ] ) );
}
Compilation message
joi2019_ho_t3.cpp:53:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
53 | main () {
| ^~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:58:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for ( int i = 0; i < s.size(); i ++ ) {
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:81:76: warning: array subscript 3 is above array bounds of 'long long int [3]' [-Warray-bounds]
81 | dp[ i ][ j ][ k ][ 1 ] = dp[ i ][ j ][ k ][ 2 ] = dp[ i ][ j ][ k ][ 3 ] = oo;
| ~~~~~~~~~~~~~~~~~~~~~^
joi2019_ho_t3.cpp:89:89: warning: array subscript 3 is above array bounds of 'long long int [3]' [-Warray-bounds]
89 | dp[ a ][ b ][ c ][ 1 ] = min( dp[ a - 1 ][ b ][ c ][ 2 ], dp[ a - 1 ][ b ][ c ][ 3 ] );
| ~~~~~~~~~~~~~~~~~~~~~~~~~^
joi2019_ho_t3.cpp:97:89: warning: array subscript 3 is above array bounds of 'long long int [3]' [-Warray-bounds]
97 | dp[ a ][ b ][ c ][ 2 ] = min( dp[ a ][ b - 1 ][ c ][ 1 ], dp[ a ][ b - 1 ][ c ][ 3 ] );
| ~~~~~~~~~~~~~~~~~~~~~~~~~^
joi2019_ho_t3.cpp:105:27: warning: array subscript 3 is above array bounds of 'long long int [3]' [-Warray-bounds]
105 | dp[ a ][ b ][ c ][ 3 ] = min( dp[ a ][ b ][ c - 1 ][ 1 ], dp[ a ][ b ][ c - 1 ][ 2 ] );
| ~~~~~~~~~~~~~~~~~~~~~^
joi2019_ho_t3.cpp:110:29: warning: array subscript 3 is above array bounds of 'long long int [3]' [-Warray-bounds]
110 | dp[ a ][ b ][ c ][ 3 ] += mn;
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
joi2019_ho_t3.cpp:116:104: warning: array subscript 3 is above array bounds of 'long long int [3]' [-Warray-bounds]
116 | cout << min( dp[ sum ][ mx ][ num ][ 1 ], min( dp[ sum ][ mx ][ num ][ 2 ], dp[ sum ][ mx ][ num ][ 3 ] ) );
| ~~~~~~~~~~~~~~~~~~~~~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
280 KB |
Output is correct |
3 |
Correct |
1 ms |
280 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
412 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
556 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
296 KB |
Output is correct |
17 |
Correct |
1 ms |
280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
280 KB |
Output is correct |
3 |
Correct |
1 ms |
280 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
412 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
556 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
296 KB |
Output is correct |
17 |
Correct |
1 ms |
280 KB |
Output is correct |
18 |
Correct |
2 ms |
2380 KB |
Output is correct |
19 |
Correct |
2 ms |
1956 KB |
Output is correct |
20 |
Correct |
2 ms |
2356 KB |
Output is correct |
21 |
Correct |
2 ms |
2508 KB |
Output is correct |
22 |
Correct |
1 ms |
1740 KB |
Output is correct |
23 |
Correct |
1 ms |
2252 KB |
Output is correct |
24 |
Correct |
2 ms |
1612 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
2 ms |
3148 KB |
Output is correct |
28 |
Correct |
2 ms |
2344 KB |
Output is correct |
29 |
Correct |
1 ms |
2352 KB |
Output is correct |
30 |
Correct |
1 ms |
2380 KB |
Output is correct |
31 |
Correct |
1 ms |
2124 KB |
Output is correct |
32 |
Correct |
1 ms |
2480 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
2764 KB |
Output is correct |
36 |
Correct |
1 ms |
1996 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
2 ms |
2212 KB |
Output is correct |
39 |
Correct |
2 ms |
2348 KB |
Output is correct |
40 |
Correct |
1 ms |
300 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
66 ms |
163900 KB |
Output is correct |
3 |
Correct |
67 ms |
163092 KB |
Output is correct |
4 |
Correct |
68 ms |
164020 KB |
Output is correct |
5 |
Correct |
78 ms |
164000 KB |
Output is correct |
6 |
Correct |
68 ms |
163976 KB |
Output is correct |
7 |
Correct |
61 ms |
163140 KB |
Output is correct |
8 |
Correct |
59 ms |
163116 KB |
Output is correct |
9 |
Correct |
60 ms |
162344 KB |
Output is correct |
10 |
Correct |
59 ms |
163976 KB |
Output is correct |
11 |
Correct |
63 ms |
163968 KB |
Output is correct |
12 |
Correct |
21 ms |
44236 KB |
Output is correct |
13 |
Correct |
33 ms |
77612 KB |
Output is correct |
14 |
Correct |
50 ms |
112016 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
280 KB |
Output is correct |
3 |
Correct |
1 ms |
280 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
412 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
556 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
296 KB |
Output is correct |
17 |
Correct |
1 ms |
280 KB |
Output is correct |
18 |
Correct |
2 ms |
2380 KB |
Output is correct |
19 |
Correct |
2 ms |
1956 KB |
Output is correct |
20 |
Correct |
2 ms |
2356 KB |
Output is correct |
21 |
Correct |
2 ms |
2508 KB |
Output is correct |
22 |
Correct |
1 ms |
1740 KB |
Output is correct |
23 |
Correct |
1 ms |
2252 KB |
Output is correct |
24 |
Correct |
2 ms |
1612 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
2 ms |
3148 KB |
Output is correct |
28 |
Correct |
2 ms |
2344 KB |
Output is correct |
29 |
Correct |
1 ms |
2352 KB |
Output is correct |
30 |
Correct |
1 ms |
2380 KB |
Output is correct |
31 |
Correct |
1 ms |
2124 KB |
Output is correct |
32 |
Correct |
1 ms |
2480 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
2764 KB |
Output is correct |
36 |
Correct |
1 ms |
1996 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
2 ms |
2212 KB |
Output is correct |
39 |
Correct |
2 ms |
2348 KB |
Output is correct |
40 |
Correct |
1 ms |
300 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
66 ms |
163900 KB |
Output is correct |
44 |
Correct |
67 ms |
163092 KB |
Output is correct |
45 |
Correct |
68 ms |
164020 KB |
Output is correct |
46 |
Correct |
78 ms |
164000 KB |
Output is correct |
47 |
Correct |
68 ms |
163976 KB |
Output is correct |
48 |
Correct |
61 ms |
163140 KB |
Output is correct |
49 |
Correct |
59 ms |
163116 KB |
Output is correct |
50 |
Correct |
60 ms |
162344 KB |
Output is correct |
51 |
Correct |
59 ms |
163976 KB |
Output is correct |
52 |
Correct |
63 ms |
163968 KB |
Output is correct |
53 |
Correct |
21 ms |
44236 KB |
Output is correct |
54 |
Correct |
33 ms |
77612 KB |
Output is correct |
55 |
Correct |
50 ms |
112016 KB |
Output is correct |
56 |
Correct |
1 ms |
332 KB |
Output is correct |
57 |
Correct |
1 ms |
300 KB |
Output is correct |
58 |
Correct |
69 ms |
120628 KB |
Output is correct |
59 |
Correct |
98 ms |
137852 KB |
Output is correct |
60 |
Correct |
103 ms |
130004 KB |
Output is correct |
61 |
Correct |
74 ms |
128112 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
69 ms |
164836 KB |
Output is correct |
64 |
Correct |
75 ms |
164488 KB |
Output is correct |
65 |
Correct |
98 ms |
159032 KB |
Output is correct |
66 |
Correct |
90 ms |
128356 KB |
Output is correct |
67 |
Correct |
94 ms |
125528 KB |
Output is correct |
68 |
Correct |
131 ms |
131312 KB |
Output is correct |
69 |
Correct |
72 ms |
129052 KB |
Output is correct |
70 |
Correct |
83 ms |
134788 KB |
Output is correct |
71 |
Correct |
71 ms |
126316 KB |
Output is correct |
72 |
Correct |
0 ms |
332 KB |
Output is correct |
73 |
Correct |
1 ms |
204 KB |
Output is correct |