#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<ll,ll>
#define iii tuple<ll,ll,ll>
const int maxn=405;
const int inf=1123456789;
int dp[maxn][maxn][maxn][4];
int cost[maxn][maxn][maxn][4];
vector<int> r,g,y;
int suff[410];
int solve(int a, int b, int c, int 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);
int n;
cin>>n;
string s;
cin>>s;
for (int 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=405;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=405;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=405;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<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<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<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<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<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<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<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<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<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 |
272 ms |
1043792 KB |
Output is correct |
2 |
Correct |
145 ms |
1043856 KB |
Output is correct |
3 |
Correct |
147 ms |
1041744 KB |
Output is correct |
4 |
Correct |
146 ms |
1047888 KB |
Output is correct |
5 |
Runtime error |
146 ms |
1048576 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
1043792 KB |
Output is correct |
2 |
Correct |
145 ms |
1043856 KB |
Output is correct |
3 |
Correct |
147 ms |
1041744 KB |
Output is correct |
4 |
Correct |
146 ms |
1047888 KB |
Output is correct |
5 |
Runtime error |
146 ms |
1048576 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
149 ms |
1045840 KB |
Output is correct |
2 |
Runtime error |
189 ms |
1048576 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
1043792 KB |
Output is correct |
2 |
Correct |
145 ms |
1043856 KB |
Output is correct |
3 |
Correct |
147 ms |
1041744 KB |
Output is correct |
4 |
Correct |
146 ms |
1047888 KB |
Output is correct |
5 |
Runtime error |
146 ms |
1048576 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |