#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const int N = 405;
int pre[N][3];
inline void ckmin(int &x , int y){
x = min(x,y);
}
int eval(int tar , int c0 , int c1 , int c2){
if(tar == 0)return 0;
int temptar = tar;
tar -= min(c0 , pre[temptar-1][0]);
tar -= min(c1 , pre[temptar-1][1]);
tar -= min(c2 , pre[temptar-1][2]);
return tar;
}
void solve(){
int n;
cin >> n;
string str;
cin >> str;
vector < int > ind[3];
for(int i = 0;i<n;i++){
if(str[i] == 'R'){
ind[0].push_back(i);
pre[i][0] = 1;
}
else if(str[i] == 'G'){
ind[1].push_back(i);
pre[i][1] = 1;
}
else if(str[i] == 'Y'){
ind[2].push_back(i);
pre[i][2] = 1;
}
else assert(false);
}
for(int i = 0;i<3;i++){
for(int j = 1;j<n;j++){
pre[j][i] += pre[j-1][i];
}
}
int dp[ind[0].size()+2][ind[1].size()+2][ind[2].size()+2][3];
for(int i = 0;i<=ind[0].size();i++){
for(int j = 0;j<=ind[1].size();j++){
for(int k = 0;k<=ind[2].size();k++){
dp[i][j][k][0] = dp[i][j][k][1] = dp[i][j][k][2] = inf;
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for(int cnt0 = 0;cnt0 <= (int)ind[0].size();cnt0++){
for(int cnt1 = 0;cnt1 <= (int)ind[1].size();cnt1++){
for(int cnt2 = 0;cnt2 <= (int)ind[2].size();cnt2++){
if(cnt0 != (int)ind[0].size()){
ckmin(dp[cnt0+1][cnt1][cnt2][0] , dp[cnt0][cnt1][cnt2][1] + eval(ind[0][cnt0] , cnt0,cnt1,cnt2));
ckmin(dp[cnt0+1][cnt1][cnt2][0] , dp[cnt0][cnt1][cnt2][2] + eval(ind[0][cnt0] , cnt0,cnt1,cnt2));
}
if(cnt1 != (int)ind[1].size()){
ckmin(dp[cnt0][cnt1+1][cnt2][1] , dp[cnt0][cnt1][cnt2][0] + eval(ind[1][cnt1] , cnt0,cnt1,cnt2));
ckmin(dp[cnt0][cnt1+1][cnt2][1] , dp[cnt0][cnt1][cnt2][2] + eval(ind[1][cnt1] , cnt0,cnt1,cnt2));
}
if(cnt2 != (int)ind[2].size()){
ckmin(dp[cnt0][cnt1][cnt2+1][2] , dp[cnt0][cnt1][cnt2][0] + eval(ind[2][cnt2] , cnt0,cnt1,cnt2));
ckmin(dp[cnt0][cnt1][cnt2+1][2] , dp[cnt0][cnt1][cnt2][1] + eval(ind[2][cnt2] , cnt0,cnt1,cnt2));
}
}
}
}
int ans = inf;
for(int i = 0;i<3;i++){
ans = min(ans , dp[ind[0].size()][ind[1].size()][ind[2].size()][i]);
}
cout << (ans == inf ? -1 : ans) << endl;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase = 1;//cin >> testcase;
while(testcase--)solve();
}
Compilation message
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:44:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i = 0;i<=ind[0].size();i++){
| ~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:45:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int j = 0;j<=ind[1].size();j++){
| ~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:46:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int k = 0;k<=ind[2].size();k++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
500 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
500 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
496 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1368 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1220 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
1112 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
500 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
496 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
1372 KB |
Output is correct |
44 |
Correct |
1 ms |
1228 KB |
Output is correct |
45 |
Correct |
1 ms |
1372 KB |
Output is correct |
46 |
Correct |
1 ms |
1372 KB |
Output is correct |
47 |
Correct |
1 ms |
1372 KB |
Output is correct |
48 |
Correct |
1 ms |
1368 KB |
Output is correct |
49 |
Correct |
1 ms |
1372 KB |
Output is correct |
50 |
Correct |
1 ms |
1372 KB |
Output is correct |
51 |
Correct |
1 ms |
1220 KB |
Output is correct |
52 |
Correct |
2 ms |
1372 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
860 KB |
Output is correct |
55 |
Correct |
1 ms |
1112 KB |
Output is correct |
56 |
Correct |
1 ms |
1372 KB |
Output is correct |
57 |
Correct |
1 ms |
1372 KB |
Output is correct |
58 |
Correct |
45 ms |
29060 KB |
Output is correct |
59 |
Correct |
48 ms |
29016 KB |
Output is correct |
60 |
Correct |
45 ms |
29004 KB |
Output is correct |
61 |
Correct |
46 ms |
29320 KB |
Output is correct |
62 |
Correct |
5 ms |
3268 KB |
Output is correct |
63 |
Correct |
7 ms |
4956 KB |
Output is correct |
64 |
Correct |
27 ms |
14688 KB |
Output is correct |
65 |
Correct |
40 ms |
21084 KB |
Output is correct |
66 |
Correct |
44 ms |
28760 KB |
Output is correct |
67 |
Correct |
58 ms |
28884 KB |
Output is correct |
68 |
Correct |
45 ms |
29280 KB |
Output is correct |
69 |
Correct |
46 ms |
29532 KB |
Output is correct |
70 |
Correct |
47 ms |
29276 KB |
Output is correct |
71 |
Correct |
46 ms |
28764 KB |
Output is correct |
72 |
Correct |
13 ms |
7768 KB |
Output is correct |
73 |
Correct |
3 ms |
2648 KB |
Output is correct |