#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
//Don't open the standings during contests.
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=405;
const int inf=1e12;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
string s;
cin>>s;
s='X'+s;
vector<int>v(n+1);
vector<vector<int>>pre(3,vector<int>(n+1,0));
vector<int>pos[3];
pos[0].pb(-1);
pos[1].pb(-1);
pos[2].pb(-1);
for(int i=1;i<=n;i++){
if(s[i]=='R'){
v[i]=0;
}
else if(s[i]=='G'){
v[i]=1;
}
else {
v[i]=2;
}
pre[v[i]][i]=1;
for(int j=0;j<3;j++){
pre[j][i]+=pre[j][i-1];
}
pos[v[i]].pb(i);
}
vector<vector<vector<int>>>dp(n+1,vector<vector<int>>(n+1,vector<int>(3,1e12)));
vector<vector<vector<int>>>dp2(n+1,vector<vector<int>>(n+1,vector<int>(3,1e12)));
dp[0][0][0]=dp[0][0][1]=dp[0][0][2]=0;
for(int i=1;i<=n;i++){
for(int j=0;j<=min(i,pre[0][n]);j++){
for(int k=0;k<=min(i-j,pre[1][n]);k++){
if(i-j-k<0 or i-j-k>pre[2][n])continue;
int r=i-j-k;
if(j)dp2[j][k][0]=min(max(pre[1][pos[0][j]]-k,0LL)+max(pre[2][pos[0][j]]-r,0LL)+min(dp[j-1][k][1],dp[j-1][k][2]),inf);
if(k)dp2[j][k][1]=min(max(pre[0][pos[1][k]]-j,0LL)+max(pre[2][pos[1][k]]-r,0LL)+min(dp[j][k-1][0],dp[j][k-1][2]),inf);
if(r)dp2[j][k][2]=min(max(pre[0][pos[2][r]]-j,0LL)+max(pre[1][pos[2][r]]-k,0LL)+min(dp[j][k][0],dp[j][k][1]),inf);
//for(int h=0;h<3;h++)cout<<i<<' '<<j<<' '<<k<<' '<<r<<' '<<h<<' '<<dp2[j][k][h]<<'\n';
}
}
for(int j=0;j<=n;j++){
for(int k=0;k<=n;k++){
for(int g=0;g<3;g++){
dp[j][k][g]=dp2[j][k][g];
dp2[j][k][g]=inf;
}
}
}
}
int ans=1e12;
for(int i=0;i<3;i++){
chmin(ans,dp[sz(pos[0])-1][sz(pos[1])-1][i]);
}
if(ans>=1e12){
cout<<-1<<'\n';
}
else{
cout<<ans<<'\n';
}
}
/*
input:
dp[i][r][g][t]=dp[i-1][r-1][g]
*/
Compilation message
joi2019_ho_t3.cpp: In function 'void setIO(std::string)':
joi2019_ho_t3.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
2 ms |
856 KB |
Output is correct |
21 |
Correct |
2 ms |
856 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 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 |
692 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 |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
1104 KB |
Output is correct |
35 |
Correct |
1 ms |
604 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 |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
311 ms |
18124 KB |
Output is correct |
3 |
Correct |
302 ms |
18028 KB |
Output is correct |
4 |
Correct |
314 ms |
18012 KB |
Output is correct |
5 |
Correct |
310 ms |
18260 KB |
Output is correct |
6 |
Correct |
305 ms |
18112 KB |
Output is correct |
7 |
Correct |
294 ms |
18040 KB |
Output is correct |
8 |
Correct |
308 ms |
18164 KB |
Output is correct |
9 |
Correct |
286 ms |
17752 KB |
Output is correct |
10 |
Correct |
314 ms |
18136 KB |
Output is correct |
11 |
Correct |
310 ms |
18012 KB |
Output is correct |
12 |
Correct |
26 ms |
4956 KB |
Output is correct |
13 |
Correct |
61 ms |
8764 KB |
Output is correct |
14 |
Correct |
125 ms |
12628 KB |
Output is correct |
15 |
Correct |
303 ms |
18264 KB |
Output is correct |
16 |
Correct |
310 ms |
18120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
2 ms |
856 KB |
Output is correct |
21 |
Correct |
2 ms |
856 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 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 |
692 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 |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
1104 KB |
Output is correct |
35 |
Correct |
1 ms |
604 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 |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
704 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
311 ms |
18124 KB |
Output is correct |
44 |
Correct |
302 ms |
18028 KB |
Output is correct |
45 |
Correct |
314 ms |
18012 KB |
Output is correct |
46 |
Correct |
310 ms |
18260 KB |
Output is correct |
47 |
Correct |
305 ms |
18112 KB |
Output is correct |
48 |
Correct |
294 ms |
18040 KB |
Output is correct |
49 |
Correct |
308 ms |
18164 KB |
Output is correct |
50 |
Correct |
286 ms |
17752 KB |
Output is correct |
51 |
Correct |
314 ms |
18136 KB |
Output is correct |
52 |
Correct |
310 ms |
18012 KB |
Output is correct |
53 |
Correct |
26 ms |
4956 KB |
Output is correct |
54 |
Correct |
61 ms |
8764 KB |
Output is correct |
55 |
Correct |
125 ms |
12628 KB |
Output is correct |
56 |
Correct |
303 ms |
18264 KB |
Output is correct |
57 |
Correct |
310 ms |
18120 KB |
Output is correct |
58 |
Correct |
313 ms |
18008 KB |
Output is correct |
59 |
Correct |
320 ms |
18132 KB |
Output is correct |
60 |
Correct |
304 ms |
17752 KB |
Output is correct |
61 |
Correct |
314 ms |
18008 KB |
Output is correct |
62 |
Correct |
295 ms |
18012 KB |
Output is correct |
63 |
Correct |
299 ms |
18120 KB |
Output is correct |
64 |
Correct |
310 ms |
18268 KB |
Output is correct |
65 |
Correct |
305 ms |
18024 KB |
Output is correct |
66 |
Correct |
306 ms |
17868 KB |
Output is correct |
67 |
Correct |
314 ms |
18012 KB |
Output is correct |
68 |
Correct |
315 ms |
18040 KB |
Output is correct |
69 |
Correct |
337 ms |
18120 KB |
Output is correct |
70 |
Correct |
335 ms |
18012 KB |
Output is correct |
71 |
Correct |
321 ms |
18040 KB |
Output is correct |
72 |
Correct |
300 ms |
18264 KB |
Output is correct |
73 |
Correct |
290 ms |
18468 KB |
Output is correct |