#ifndef Local
#pragma GCC optimize("O3,unroll-loops")
const int lim=2e5+5;
#else
const int lim=3e3+3;
#endif
#include "bits/stdc++.h"
using namespace std;
#define int int64_t
#define pb push_back
//const int mod=1e9+7;
const int mod=(1ll<<61)-1;
using pii=pair<int,int>;
inline void solve(){
int n;
cin>>n;
string s;
cin>>s;
int a[n];
for(int i=0;i<n;i++){
if(s[i]=='R')a[i]=0;
else if(s[i]=='G')a[i]=1;
else a[i]=2;
}
vector<int>r,g,b;
for(int i=0;i<n;i++){
if(!a[i])r.pb(i);
else if(a[i]==1)g.pb(i);
else b.pb(i);
}
int rc=r.size(),gc=g.size(),bc=b.size();
int pc[n][n][3][3];
memset(pc,0,sizeof(pc));
if(rc){
for(int i=0;i<rc;i++){
if(gc){
pc[i][0][0][1]=r[i]<g[0];
for(int j=1;j<gc;j++){
pc[i][j][0][1]=pc[i][j-1][0][1]+(r[i]<g[j]);
}
}
if(bc){
pc[i][0][0][2]=r[i]<b[0];
for(int j=1;j<bc;j++){
pc[i][j][0][2]=pc[i][j-1][0][2]+(r[i]<b[j]);
}
}
}
}
if(gc){
for(int i=0;i<gc;i++){
if(rc){
pc[i][0][1][0]=g[i]<r[0];
for(int j=1;j<rc;j++){
pc[i][j][1][0]=pc[i][j-1][1][0]+(g[i]<r[0]);
}
}
if(bc){
pc[i][0][1][2]=g[i]<b[0];
for(int j=1;j<bc;j++){
pc[i][j][1][2]=pc[i][j-1][1][2]+(g[i]<b[0]);
}
}
}
}
if(bc){
for(int i=0;i<bc;i++){
if(rc){
pc[i][0][2][0]=b[i]<r[0];
for(int j=1;j<gc;j++){
pc[i][j][2][0]=pc[i][j-1][2][0]+(b[i]<r[j]);
}
}
if(gc){
pc[i][0][2][1]=b[i]<g[0];
for(int j=1;j<bc;j++){
pc[i][j][2][1]=pc[i][j-1][2][1]+(b[i]<g[j]);
}
}
}
}
int dp[rc+1][gc+1][bc+1][3];
for(int i=0;i<=rc;i++){
for(int j=0;j<=gc;j++){
for(int k=0;k<=bc;k++){
for(int l=0;l<3;l++){
dp[i][j][k][l]=INT_MAX;
}
}
}
}
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
for(int i=0;i<=rc;i++){
for(int j=0;j<=gc;j++){
for(int k=0;k<=bc;k++){
int val1=i<rc?
(r[i]-(i+j+k)
+(j?pc[i][j-1][0][1]:0)
+(k?pc[i][k-1][0][2]:0)):INT_MAX;
int val2=j<gc?
(g[j]-(i+j+k)
+(i?pc[j][i-1][1][0]:0)
+(k?pc[j][k-1][1][2]:0)):INT_MAX;
int val3=k<bc?
(b[k]-(i+j+k)
+(i?pc[k][i-1][2][0]:0)
+(j?pc[k][j-1][2][1]:0)):INT_MAX;
if(0<=val1&&i+1<=rc){
dp[i+1][j][k][0]=min(
dp[i+1][j][k][0],
min(dp[i][j][k][1],dp[i][j][k][2])+val1
);
}
if(0<=val2&&j+1<=gc){
dp[i][j+1][k][1]=min(
dp[i][j+1][k][1],
min(dp[i][j][k][0],dp[i][j][k][2])+val2
);
}
if(0<=val3&&k+1<=bc){
dp[i][j][k+1][2]=min(
dp[i][j][k+1][2],
min(dp[i][j][k][0],dp[i][j][k][1])+val3
);
}
}
}
}
int ans=INT_MAX;
for(int i=0;i<3;i++){
ans=min(ans,dp[rc][gc][bc][i]);
}
if(ans==INT_MAX)cout<<"-1\n";
else cout<<ans<<"\n";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
#ifdef Local
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#else
#endif
int t=1;
//cin>>t;
while (t--)
{
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
7 ms |
12632 KB |
Output is correct |
3 |
Correct |
8 ms |
12380 KB |
Output is correct |
4 |
Incorrect |
8 ms |
12636 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |