#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[j]);
}
}
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[j]);
}
}
}
}
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<rc;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<gc;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 |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
400 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
400 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
856 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
856 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
12632 KB |
Output is correct |
3 |
Correct |
9 ms |
12380 KB |
Output is correct |
4 |
Correct |
8 ms |
12564 KB |
Output is correct |
5 |
Correct |
8 ms |
12524 KB |
Output is correct |
6 |
Correct |
8 ms |
12888 KB |
Output is correct |
7 |
Correct |
8 ms |
12380 KB |
Output is correct |
8 |
Correct |
8 ms |
12380 KB |
Output is correct |
9 |
Correct |
8 ms |
12380 KB |
Output is correct |
10 |
Correct |
9 ms |
12632 KB |
Output is correct |
11 |
Correct |
8 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
3676 KB |
Output is correct |
13 |
Correct |
4 ms |
5980 KB |
Output is correct |
14 |
Correct |
6 ms |
8756 KB |
Output is correct |
15 |
Correct |
8 ms |
12636 KB |
Output is correct |
16 |
Correct |
8 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
400 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
856 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
856 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
0 ms |
604 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
9 ms |
12632 KB |
Output is correct |
44 |
Correct |
9 ms |
12380 KB |
Output is correct |
45 |
Correct |
8 ms |
12564 KB |
Output is correct |
46 |
Correct |
8 ms |
12524 KB |
Output is correct |
47 |
Correct |
8 ms |
12888 KB |
Output is correct |
48 |
Correct |
8 ms |
12380 KB |
Output is correct |
49 |
Correct |
8 ms |
12380 KB |
Output is correct |
50 |
Correct |
8 ms |
12380 KB |
Output is correct |
51 |
Correct |
9 ms |
12632 KB |
Output is correct |
52 |
Correct |
8 ms |
12636 KB |
Output is correct |
53 |
Correct |
2 ms |
3676 KB |
Output is correct |
54 |
Correct |
4 ms |
5980 KB |
Output is correct |
55 |
Correct |
6 ms |
8756 KB |
Output is correct |
56 |
Correct |
8 ms |
12636 KB |
Output is correct |
57 |
Correct |
8 ms |
12636 KB |
Output is correct |
58 |
Correct |
72 ms |
67924 KB |
Output is correct |
59 |
Correct |
77 ms |
68036 KB |
Output is correct |
60 |
Correct |
83 ms |
67676 KB |
Output is correct |
61 |
Correct |
74 ms |
68144 KB |
Output is correct |
62 |
Correct |
12 ms |
16220 KB |
Output is correct |
63 |
Correct |
16 ms |
19720 KB |
Output is correct |
64 |
Correct |
38 ms |
39516 KB |
Output is correct |
65 |
Correct |
54 ms |
51540 KB |
Output is correct |
66 |
Correct |
71 ms |
67160 KB |
Output is correct |
67 |
Correct |
81 ms |
67412 KB |
Output is correct |
68 |
Correct |
83 ms |
67920 KB |
Output is correct |
69 |
Correct |
72 ms |
68436 KB |
Output is correct |
70 |
Correct |
73 ms |
68436 KB |
Output is correct |
71 |
Correct |
71 ms |
66900 KB |
Output is correct |
72 |
Correct |
21 ms |
25948 KB |
Output is correct |
73 |
Correct |
11 ms |
15196 KB |
Output is correct |