#include <bits/stdc++.h>
using namespace std;
#define ll short
#define ii pair<ll,ll>
#define iii tuple<ll,ll,ll>
const short maxn=205;
const short inf=10000;
short dp[maxn][maxn][maxn][4];
short cost[maxn][maxn][maxn][4];
vector<short> r,g,y;
short suff[405];
short solve(short a, short b, short c, short flag){
if (a<0 || b<0 || c<0) return inf;
if (a==0 && b==0 && c==0) return 0;
if (dp[a][b][c][flag]!=-1) return dp[a][b][c][flag];
if (flag==1){
dp[a][b][c][flag] = min(solve(a-1,b,c,2),solve(a-1,b,c,3));
}
if (flag==2){
dp[a][b][c][flag] = min(solve(a,b-1,c,1),solve(a,b-1,c,3));
}
if (flag==3){
dp[a][b][c][flag] = min(solve(a,b,c-1,1),solve(a,b,c-1,2));
}
dp[a][b][c][flag]+=cost[a][b][c][flag];
return dp[a][b][c][flag];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
short n;
cin>>n;
string s;
cin>>s;
for (short i=1;i<=n;i++){
if (s[i-1]=='R') r.emplace_back(i);
if (s[i-1]=='Y') y.emplace_back(i);
if (s[i-1]=='G') g.emplace_back(i);
}
if (2*max({r.size(),g.size(),y.size()})>n+(n&1)){
cout<<-1;
return 0;
}
for (int i=0;i<=r.size();i++){
for (int j=0;j<=g.size();j++){
//create suffix sum array
memset(suff,0,sizeof(suff));
for (int k=0;k<i;k++) suff[r[k]]=1;
for (int k=0;k<j;k++) suff[g[k]]=1;
for (int k=400;k>=0;k--) suff[k]+=suff[k+1];
for (int k=0;k<y.size();k++){
cost[i][j][k+1][3]=abs((suff[y[k]]+y[k])-(i+j+k+1));
}
}
}
for (int i=0;i<=g.size();i++){
for (int j=0;j<=y.size();j++){
//create suffix sum array
memset(suff,0,sizeof(suff));
for (int k=0;k<i;k++) suff[g[k]]=1;
for (int k=0;k<j;k++) suff[y[k]]=1;
for (int k=400;k>=0;k--) suff[k]+=suff[k+1];
for (int k=0;k<r.size();k++){
cost[k+1][i][j][1]=abs((suff[r[k]]+r[k])-(i+j+k+1));
}
}
}
for (int i=0;i<=y.size();i++){
for (int j=0;j<=r.size();j++){
//create suffix sum array
memset(suff,0,sizeof(suff));
for (int k=0;k<i;k++) suff[y[k]]=1;
for (int k=0;k<j;k++) suff[r[k]]=1;
for (int k=400;k>=0;k--) suff[k]+=suff[k+1];
for (int k=0;k<g.size();k++){
cost[j][k+1][i][2]=abs((suff[g[k]]+g[k])-(i+j+k+1));
}
}
}
memset(dp,-1,sizeof(dp));
cout<<min({solve(r.size(),g.size(),y.size(),1),
solve(r.size(),g.size(),y.size(),2),
solve(r.size(),g.size(),y.size(),3)});
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:43:41: warning: comparison of integer expressions of different signedness: 'long unsigned int' and 'int' [-Wsign-compare]
43 | if (2*max({r.size(),g.size(),y.size()})>n+(n&1)){
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
joi2019_ho_t3.cpp:47:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i=0;i<=r.size();i++){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:48:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int j=0;j<=g.size();j++){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:54:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int k=0;k<y.size();k++){
| ~^~~~~~~~~
joi2019_ho_t3.cpp:59:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i=0;i<=g.size();i++){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:60:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int j=0;j<=y.size();j++){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:66:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int k=0;k<r.size();k++){
| ~^~~~~~~~~
joi2019_ho_t3.cpp:71:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (int i=0;i<=y.size();i++){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:72:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int j=0;j<=r.size();j++){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:78:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (int k=0;k<g.size();k++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
71512 KB |
Output is correct |
2 |
Correct |
10 ms |
71476 KB |
Output is correct |
3 |
Correct |
9 ms |
69468 KB |
Output is correct |
4 |
Correct |
10 ms |
71488 KB |
Output is correct |
5 |
Correct |
10 ms |
71516 KB |
Output is correct |
6 |
Correct |
11 ms |
71516 KB |
Output is correct |
7 |
Correct |
10 ms |
73560 KB |
Output is correct |
8 |
Correct |
10 ms |
71516 KB |
Output is correct |
9 |
Correct |
10 ms |
71516 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
10 ms |
71512 KB |
Output is correct |
12 |
Correct |
11 ms |
71516 KB |
Output is correct |
13 |
Correct |
1 ms |
500 KB |
Output is correct |
14 |
Correct |
9 ms |
71512 KB |
Output is correct |
15 |
Correct |
9 ms |
71512 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
71512 KB |
Output is correct |
2 |
Correct |
10 ms |
71476 KB |
Output is correct |
3 |
Correct |
9 ms |
69468 KB |
Output is correct |
4 |
Correct |
10 ms |
71488 KB |
Output is correct |
5 |
Correct |
10 ms |
71516 KB |
Output is correct |
6 |
Correct |
11 ms |
71516 KB |
Output is correct |
7 |
Correct |
10 ms |
73560 KB |
Output is correct |
8 |
Correct |
10 ms |
71516 KB |
Output is correct |
9 |
Correct |
10 ms |
71516 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
10 ms |
71512 KB |
Output is correct |
12 |
Correct |
11 ms |
71516 KB |
Output is correct |
13 |
Correct |
1 ms |
500 KB |
Output is correct |
14 |
Correct |
9 ms |
71512 KB |
Output is correct |
15 |
Correct |
9 ms |
71512 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
12 ms |
77656 KB |
Output is correct |
19 |
Correct |
12 ms |
75612 KB |
Output is correct |
20 |
Correct |
13 ms |
77524 KB |
Output is correct |
21 |
Correct |
13 ms |
77652 KB |
Output is correct |
22 |
Correct |
11 ms |
77660 KB |
Output is correct |
23 |
Correct |
12 ms |
75476 KB |
Output is correct |
24 |
Correct |
12 ms |
73564 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
11 ms |
79708 KB |
Output is correct |
28 |
Correct |
12 ms |
77660 KB |
Output is correct |
29 |
Correct |
14 ms |
77660 KB |
Output is correct |
30 |
Correct |
11 ms |
75612 KB |
Output is correct |
31 |
Correct |
12 ms |
75520 KB |
Output is correct |
32 |
Correct |
12 ms |
75612 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
14 ms |
77660 KB |
Output is correct |
36 |
Correct |
12 ms |
77656 KB |
Output is correct |
37 |
Correct |
11 ms |
75612 KB |
Output is correct |
38 |
Correct |
12 ms |
75608 KB |
Output is correct |
39 |
Correct |
13 ms |
75804 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
71524 KB |
Output is correct |
2 |
Incorrect |
57 ms |
135252 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
71512 KB |
Output is correct |
2 |
Correct |
10 ms |
71476 KB |
Output is correct |
3 |
Correct |
9 ms |
69468 KB |
Output is correct |
4 |
Correct |
10 ms |
71488 KB |
Output is correct |
5 |
Correct |
10 ms |
71516 KB |
Output is correct |
6 |
Correct |
11 ms |
71516 KB |
Output is correct |
7 |
Correct |
10 ms |
73560 KB |
Output is correct |
8 |
Correct |
10 ms |
71516 KB |
Output is correct |
9 |
Correct |
10 ms |
71516 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
10 ms |
71512 KB |
Output is correct |
12 |
Correct |
11 ms |
71516 KB |
Output is correct |
13 |
Correct |
1 ms |
500 KB |
Output is correct |
14 |
Correct |
9 ms |
71512 KB |
Output is correct |
15 |
Correct |
9 ms |
71512 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
12 ms |
77656 KB |
Output is correct |
19 |
Correct |
12 ms |
75612 KB |
Output is correct |
20 |
Correct |
13 ms |
77524 KB |
Output is correct |
21 |
Correct |
13 ms |
77652 KB |
Output is correct |
22 |
Correct |
11 ms |
77660 KB |
Output is correct |
23 |
Correct |
12 ms |
75476 KB |
Output is correct |
24 |
Correct |
12 ms |
73564 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
11 ms |
79708 KB |
Output is correct |
28 |
Correct |
12 ms |
77660 KB |
Output is correct |
29 |
Correct |
14 ms |
77660 KB |
Output is correct |
30 |
Correct |
11 ms |
75612 KB |
Output is correct |
31 |
Correct |
12 ms |
75520 KB |
Output is correct |
32 |
Correct |
12 ms |
75612 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
14 ms |
77660 KB |
Output is correct |
36 |
Correct |
12 ms |
77656 KB |
Output is correct |
37 |
Correct |
11 ms |
75612 KB |
Output is correct |
38 |
Correct |
12 ms |
75608 KB |
Output is correct |
39 |
Correct |
13 ms |
75804 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
11 ms |
71524 KB |
Output is correct |
43 |
Incorrect |
57 ms |
135252 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |