#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int n,dp[3][402][402][402];
vector<int>v[4];
string s;
int f(int x,int ir,int ig,int iy)
{
if(ir+ig+iy==n)return 0;
if(dp[x][ir][ig][iy]!=-1)return dp[x][ir][ig][iy];
int ans=1e9,ind=ir+ig+iy,id;
for(int i=0;i<3;i++)
{
if(i==x)continue;
if(i==0)
{
if(ir==v[0].size())continue;
int id=v[i][ir],ad=0;
for(int j=0;j<ig;j++)
{
if(v[1][j]>id)ad++;
}
for(int j=0;j<iy;j++)
{
if(v[2][j]>id)ad++;
}
ans=min(ans,f(0,ir+1,ig,iy)+abs((id+ad)-ind));
}
if(i==1)
{
if(ig==v[1].size())continue;
int id=v[i][ig],ad=0;
for(int j=0;j<ir;j++)
{
if(v[0][j]>id)ad++;
}
for(int j=0;j<iy;j++)
{
if(v[2][j]>id)ad++;
}
ans=min(ans,f(1,ir,ig+1,iy)+abs((id+ad)-ind));
}
if(i==2)
{
if(iy==v[2].size())continue;
int id=v[i][iy],ad=0;
for(int j=0;j<ir;j++)
{
if(v[0][j]>id)ad++;
}
for(int j=0;j<ig;j++)
{
if(v[1][j]>id)ad++;
}
ans=min(ans,f(2,ir,ig,iy+1)+abs((id+ad)-ind));
}
}
return dp[x][ir][ig][iy]=ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen("dec.in","r",stdin);
//freopen("dec.out","w",stdout);
memset(dp,-1,sizeof dp);
cin>>n>>s;
for(int i=0;i<n;i++)
{
if(s[i]=='R')v[0].push_back(i);
if(s[i]=='G')v[1].push_back(i);
if(s[i]=='Y')v[2].push_back(i);
}
int ans=f(3,0,0,0);
if(ans>=1e8)cout<<-1;
else cout<<ans;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int f(int, int, int, int)':
joi2019_ho_t3.cpp:17:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ir==v[0].size())continue;
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:31:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ig==v[1].size())continue;
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:45:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(iy==v[2].size())continue;
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:11:30: warning: unused variable 'id' [-Wunused-variable]
int ans=1e9,ind=ir+ig+iy,id;
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
694 ms |
763244 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
694 ms |
763244 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
430 ms |
763208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
694 ms |
763244 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |