#include <bits/stdc++.h>
#define ll int
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;
template <class _T>
bool chmin(_T &x, const _T &y){
bool f=0;
if (x>y){x=y;f=1;}
return f;
}
template <class _T>
bool chmax(_T &x, const _T &y){
bool f=0;
if (x<y){x=y;f=1;}
return f;
}
//void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=1e9;
const ll mod=1e9+7;
const ll N=402;
const ld eps=1e-9;
ll dp[N][N][N][3];
void solve(){
ll i,j,q,k;
ll n;
cin>>n;
ll a[n+5];
str s;
cin>>s;
vector<vll> v(3);
for(i=0;i<n;i++){
ll x;
if(s[i]=='R')x=0;
if(s[i]=='G')x=1;
if(s[i]=='Y')x=2;
a[i+1]=x;
v[x].pb(i+1);
}
ll R=v[0].sz,G=v[1].sz,Y=v[2].sz;
for(i=0;i<=R;i++)
for(j=0;j<=G;j++)
for(q=0;q<=Y;q++)
for(k=0;k<3;k++)
dp[i][j][q][k]=inf;
dp[0][0][0][0]=0;
dp[0][0][0][1]=0;
dp[0][0][0][2]=0;
ll pref[n+5][3];
memset(pref,0,sizeof(pref));
for(i=1;i<=n;i++){
for(j=0;j<3;j++){
pref[i][j]=pref[i-1][j];
if(a[i]==j)pref[i][j]++;
}
}
for(i=0;i<=R;i++){
for(j=0;j<=G;j++){
for(q=0;q<=Y;q++){
for(k=0;k<3;k++){
if(i<R && k!=0){
chmin(dp[i+1][j][q][0],dp[i][j][q][k]+max(0,pref[v[0][i]][1]-j)+max(0,pref[v[0][i]][2]-q));
}
if(j<G && k!=1){
chmin(dp[i][j+1][q][1],dp[i][j][q][k]+max(0,pref[v[1][j]][0]-i)+max(0,pref[v[1][j]][2]-q));
}
if(q<Y && k!=2){
chmin(dp[i][j][q+1][2],dp[i][j][q][k]+max(0,pref[v[2][q]][0]-i)+max(0,pref[v[2][q]][1]-j));
}
}
}
}
}
ll ans=inf;
for(i=0;i<3;i++){
chmin(ans,dp[R][G][Y][i]);
}
if(ans==inf)cout<<-1<<endl;
else cout<<ans<<endl;
}
signed main(){
//start();
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
/*
*/
Compilation message
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:60:12: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
60 | v[x].pb(i+1);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2400 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
4 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
6488 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
8664 KB |
Output is correct |
16 |
Correct |
3 ms |
22996 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2400 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
4 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
6488 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
8664 KB |
Output is correct |
16 |
Correct |
3 ms |
22996 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
7 ms |
45656 KB |
Output is correct |
19 |
Correct |
6 ms |
37212 KB |
Output is correct |
20 |
Correct |
8 ms |
49756 KB |
Output is correct |
21 |
Correct |
5 ms |
39516 KB |
Output is correct |
22 |
Correct |
8 ms |
47448 KB |
Output is correct |
23 |
Correct |
6 ms |
33316 KB |
Output is correct |
24 |
Correct |
3 ms |
21080 KB |
Output is correct |
25 |
Correct |
12 ms |
72284 KB |
Output is correct |
26 |
Correct |
8 ms |
59836 KB |
Output is correct |
27 |
Correct |
8 ms |
51800 KB |
Output is correct |
28 |
Correct |
6 ms |
41564 KB |
Output is correct |
29 |
Correct |
7 ms |
43612 KB |
Output is correct |
30 |
Correct |
5 ms |
33372 KB |
Output is correct |
31 |
Correct |
5 ms |
29116 KB |
Output is correct |
32 |
Correct |
5 ms |
35420 KB |
Output is correct |
33 |
Correct |
8 ms |
49756 KB |
Output is correct |
34 |
Correct |
7 ms |
43612 KB |
Output is correct |
35 |
Correct |
7 ms |
45660 KB |
Output is correct |
36 |
Correct |
6 ms |
39260 KB |
Output is correct |
37 |
Correct |
4 ms |
24924 KB |
Output is correct |
38 |
Correct |
5 ms |
27228 KB |
Output is correct |
39 |
Correct |
7 ms |
37464 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
3 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
63 ms |
305276 KB |
Output is correct |
3 |
Correct |
56 ms |
305232 KB |
Output is correct |
4 |
Correct |
58 ms |
305484 KB |
Output is correct |
5 |
Correct |
57 ms |
305360 KB |
Output is correct |
6 |
Correct |
56 ms |
305488 KB |
Output is correct |
7 |
Correct |
62 ms |
304500 KB |
Output is correct |
8 |
Correct |
57 ms |
304472 KB |
Output is correct |
9 |
Correct |
56 ms |
304256 KB |
Output is correct |
10 |
Correct |
57 ms |
305492 KB |
Output is correct |
11 |
Correct |
57 ms |
305488 KB |
Output is correct |
12 |
Correct |
28 ms |
197468 KB |
Output is correct |
13 |
Correct |
36 ms |
252664 KB |
Output is correct |
14 |
Correct |
45 ms |
269700 KB |
Output is correct |
15 |
Correct |
56 ms |
302296 KB |
Output is correct |
16 |
Correct |
58 ms |
304908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2400 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
4 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
6488 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
8664 KB |
Output is correct |
16 |
Correct |
3 ms |
22996 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
7 ms |
45656 KB |
Output is correct |
19 |
Correct |
6 ms |
37212 KB |
Output is correct |
20 |
Correct |
8 ms |
49756 KB |
Output is correct |
21 |
Correct |
5 ms |
39516 KB |
Output is correct |
22 |
Correct |
8 ms |
47448 KB |
Output is correct |
23 |
Correct |
6 ms |
33316 KB |
Output is correct |
24 |
Correct |
3 ms |
21080 KB |
Output is correct |
25 |
Correct |
12 ms |
72284 KB |
Output is correct |
26 |
Correct |
8 ms |
59836 KB |
Output is correct |
27 |
Correct |
8 ms |
51800 KB |
Output is correct |
28 |
Correct |
6 ms |
41564 KB |
Output is correct |
29 |
Correct |
7 ms |
43612 KB |
Output is correct |
30 |
Correct |
5 ms |
33372 KB |
Output is correct |
31 |
Correct |
5 ms |
29116 KB |
Output is correct |
32 |
Correct |
5 ms |
35420 KB |
Output is correct |
33 |
Correct |
8 ms |
49756 KB |
Output is correct |
34 |
Correct |
7 ms |
43612 KB |
Output is correct |
35 |
Correct |
7 ms |
45660 KB |
Output is correct |
36 |
Correct |
6 ms |
39260 KB |
Output is correct |
37 |
Correct |
4 ms |
24924 KB |
Output is correct |
38 |
Correct |
5 ms |
27228 KB |
Output is correct |
39 |
Correct |
7 ms |
37464 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
3 ms |
19036 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
63 ms |
305276 KB |
Output is correct |
44 |
Correct |
56 ms |
305232 KB |
Output is correct |
45 |
Correct |
58 ms |
305484 KB |
Output is correct |
46 |
Correct |
57 ms |
305360 KB |
Output is correct |
47 |
Correct |
56 ms |
305488 KB |
Output is correct |
48 |
Correct |
62 ms |
304500 KB |
Output is correct |
49 |
Correct |
57 ms |
304472 KB |
Output is correct |
50 |
Correct |
56 ms |
304256 KB |
Output is correct |
51 |
Correct |
57 ms |
305492 KB |
Output is correct |
52 |
Correct |
57 ms |
305488 KB |
Output is correct |
53 |
Correct |
28 ms |
197468 KB |
Output is correct |
54 |
Correct |
36 ms |
252664 KB |
Output is correct |
55 |
Correct |
45 ms |
269700 KB |
Output is correct |
56 |
Correct |
56 ms |
302296 KB |
Output is correct |
57 |
Correct |
58 ms |
304908 KB |
Output is correct |
58 |
Correct |
67 ms |
234524 KB |
Output is correct |
59 |
Correct |
74 ms |
257976 KB |
Output is correct |
60 |
Correct |
77 ms |
256340 KB |
Output is correct |
61 |
Correct |
70 ms |
250740 KB |
Output is correct |
62 |
Correct |
68 ms |
311380 KB |
Output is correct |
63 |
Correct |
66 ms |
301800 KB |
Output is correct |
64 |
Correct |
77 ms |
294212 KB |
Output is correct |
65 |
Correct |
74 ms |
273908 KB |
Output is correct |
66 |
Correct |
69 ms |
252496 KB |
Output is correct |
67 |
Correct |
63 ms |
213944 KB |
Output is correct |
68 |
Correct |
71 ms |
252484 KB |
Output is correct |
69 |
Correct |
71 ms |
252500 KB |
Output is correct |
70 |
Correct |
73 ms |
252244 KB |
Output is correct |
71 |
Correct |
73 ms |
260216 KB |
Output is correct |
72 |
Correct |
53 ms |
272040 KB |
Output is correct |
73 |
Correct |
3 ms |
8792 KB |
Output is correct |