/*
Editor: Abdelrahman Hossam
Nickname: Blobo2_Blobo2
IOI next year isA :)
*/
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-funroll-all-loops,-fpeel-loops,-funswitch-loops")
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define endl "\n"
#define all(v) v.begin(),v.end()
#define gen(arr,n,nxt) generate(arr,arr+n,nxt)
#define Blobo2_el_7akim_elly_3ayz_yro7_IOI_w_3ayz_yakol_jilaty ios_base::sync_with_stdio(false);cin.tie(0);
#define EPS 0.00000001
const int mo=1e9+7,INF=1e18;
int nxt(){int x;cin>>x;return x;}
int dp[401][401][401][3],n;
vector<int>vy,vr,vg;
int tr[400],tg[400],ty[400];
int solve(int r=0,int g=0,int y=0,int last=0){
//cout<<r<<' '<<g<<' '<<y<<' '<<last<<endl;
int idx = r+g+y;
if(idx==n)return 0;
int &ret = dp[r][g][y][last];
if(ret != -1)return ret;
ret = 1e9;
if(idx == 0){
idx = vg[g];
if(g+1<=tg[n-1])
ret = min(ret,solve(r,g+1,y,1) + max(0,tr[idx] - r)+ max(0,ty[idx] - y) );
idx = vy[y];
if(y+1<=ty[n-1])
ret = min(ret,solve(r,g,y+1,2) + max(0,tr[idx] - r)+ max(0,tg[idx] - g) );
idx = vr[r];
if(r+1<=tr[n-1])
ret = min(ret,solve(r+1,g,y,0) + max(0,tg[idx] - g)+ max(0,ty[idx] - y) );
}
else if(last == 0){
idx = vg[g];
if(g+1<=tg[n-1])
ret = min(ret,solve(r,g+1,y,1) + max(0,tr[idx] - r)+ max(0,ty[idx] - y));
idx = vy[y];
if(y+1<=ty[n-1])
ret = min(ret,solve(r,g,y+1,2) + max(0,tr[idx] - r)+ max(0,tg[idx] - g) );
}
else if(last == 1){
idx = vr[r];
if(r+1<=tr[n-1])
ret = min(ret,solve(r+1,g,y,0) + max(0,tg[idx] - g)+ max(0,ty[idx] - y) );
idx = vy[y];
if(y+1<=ty[n-1])
ret = min(ret,solve(r,g,y+1,2) + max(0,tr[idx] - r)+ max(0,tg[idx] - g) );
}
else if(last == 2){
idx = vg[g];
if(g+1<=tg[n-1])
ret = min(ret,solve(r,g+1,y,1) + max(0,tr[idx] - r)+ max(0,ty[idx] - y) );
idx = vr[r];
if(r+1<=tr[n-1])
ret = min(ret,solve(r+1,g,y,0) + max(0,tg[idx] - g)+ max(0,ty[idx] - y) );
}
return ret;
}
signed main(){
Blobo2_el_7akim_elly_3ayz_yro7_IOI_w_3ayz_yakol_jilaty
n=nxt();
string s;
cin>>s;
int arr[3];
arr[0]=count(all(s),'R');
arr[1]=count(all(s),'Y');
arr[2]=count(all(s),'G');
sort(arr,arr+3);
int mn =0;
if(arr[0]>arr[1]+1)mn += arr[0] - (arr[1]+1);
if(arr[2]>arr[0]+arr[1]+1)mn+= arr[2] - (arr[0]+arr[1]+1);
if(mn){
cout<<-1<<endl;
return 0;
}
for(int i=0;i<n;i++){
if(s[i] == 'R')
vr.push_back(i),tr[i]++;
if(s[i] == 'G')
vg.push_back(i),tg[i]++;
if(s[i] == 'Y')
vy.push_back(i),ty[i]++;
if(!i)continue;
tr[i]+=tr[i-1];
tg[i]+=tg[i-1];
ty[i]+=ty[i-1];
}
memset(dp,-1,sizeof dp);
cout<<solve();
return 0;
}
Compilation message
joi2019_ho_t3.cpp:21:24: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
21 | const int mo=1e9+7,INF=1e18;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
354 ms |
757304 KB |
Output is correct |
2 |
Correct |
291 ms |
757336 KB |
Output is correct |
3 |
Correct |
290 ms |
757296 KB |
Output is correct |
4 |
Correct |
292 ms |
757396 KB |
Output is correct |
5 |
Correct |
306 ms |
757412 KB |
Output is correct |
6 |
Correct |
304 ms |
757340 KB |
Output is correct |
7 |
Correct |
294 ms |
757316 KB |
Output is correct |
8 |
Correct |
341 ms |
757396 KB |
Output is correct |
9 |
Correct |
305 ms |
757388 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
296 ms |
757336 KB |
Output is correct |
12 |
Correct |
301 ms |
757316 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
308 ms |
757412 KB |
Output is correct |
15 |
Correct |
324 ms |
757308 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
354 ms |
757304 KB |
Output is correct |
2 |
Correct |
291 ms |
757336 KB |
Output is correct |
3 |
Correct |
290 ms |
757296 KB |
Output is correct |
4 |
Correct |
292 ms |
757396 KB |
Output is correct |
5 |
Correct |
306 ms |
757412 KB |
Output is correct |
6 |
Correct |
304 ms |
757340 KB |
Output is correct |
7 |
Correct |
294 ms |
757316 KB |
Output is correct |
8 |
Correct |
341 ms |
757396 KB |
Output is correct |
9 |
Correct |
305 ms |
757388 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
296 ms |
757336 KB |
Output is correct |
12 |
Correct |
301 ms |
757316 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
308 ms |
757412 KB |
Output is correct |
15 |
Correct |
324 ms |
757308 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
308 ms |
757412 KB |
Output is correct |
19 |
Correct |
349 ms |
757392 KB |
Output is correct |
20 |
Correct |
314 ms |
757300 KB |
Output is correct |
21 |
Correct |
315 ms |
757384 KB |
Output is correct |
22 |
Correct |
319 ms |
757308 KB |
Output is correct |
23 |
Correct |
308 ms |
757316 KB |
Output is correct |
24 |
Correct |
290 ms |
757316 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
216 KB |
Output is correct |
27 |
Correct |
300 ms |
757408 KB |
Output is correct |
28 |
Correct |
309 ms |
757396 KB |
Output is correct |
29 |
Correct |
308 ms |
757316 KB |
Output is correct |
30 |
Correct |
300 ms |
757316 KB |
Output is correct |
31 |
Correct |
328 ms |
757416 KB |
Output is correct |
32 |
Correct |
295 ms |
757380 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
295 ms |
757416 KB |
Output is correct |
36 |
Correct |
293 ms |
757320 KB |
Output is correct |
37 |
Correct |
298 ms |
757576 KB |
Output is correct |
38 |
Correct |
300 ms |
757408 KB |
Output is correct |
39 |
Correct |
308 ms |
757408 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
302 ms |
757356 KB |
Output is correct |
2 |
Correct |
331 ms |
757396 KB |
Output is correct |
3 |
Correct |
307 ms |
757336 KB |
Output is correct |
4 |
Correct |
310 ms |
757436 KB |
Output is correct |
5 |
Correct |
302 ms |
757484 KB |
Output is correct |
6 |
Correct |
329 ms |
757444 KB |
Output is correct |
7 |
Correct |
305 ms |
757340 KB |
Output is correct |
8 |
Correct |
295 ms |
757444 KB |
Output is correct |
9 |
Correct |
299 ms |
757444 KB |
Output is correct |
10 |
Correct |
297 ms |
757444 KB |
Output is correct |
11 |
Correct |
311 ms |
757448 KB |
Output is correct |
12 |
Correct |
291 ms |
757432 KB |
Output is correct |
13 |
Correct |
293 ms |
757344 KB |
Output is correct |
14 |
Correct |
312 ms |
757444 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
354 ms |
757304 KB |
Output is correct |
2 |
Correct |
291 ms |
757336 KB |
Output is correct |
3 |
Correct |
290 ms |
757296 KB |
Output is correct |
4 |
Correct |
292 ms |
757396 KB |
Output is correct |
5 |
Correct |
306 ms |
757412 KB |
Output is correct |
6 |
Correct |
304 ms |
757340 KB |
Output is correct |
7 |
Correct |
294 ms |
757316 KB |
Output is correct |
8 |
Correct |
341 ms |
757396 KB |
Output is correct |
9 |
Correct |
305 ms |
757388 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
296 ms |
757336 KB |
Output is correct |
12 |
Correct |
301 ms |
757316 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
308 ms |
757412 KB |
Output is correct |
15 |
Correct |
324 ms |
757308 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
308 ms |
757412 KB |
Output is correct |
19 |
Correct |
349 ms |
757392 KB |
Output is correct |
20 |
Correct |
314 ms |
757300 KB |
Output is correct |
21 |
Correct |
315 ms |
757384 KB |
Output is correct |
22 |
Correct |
319 ms |
757308 KB |
Output is correct |
23 |
Correct |
308 ms |
757316 KB |
Output is correct |
24 |
Correct |
290 ms |
757316 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
216 KB |
Output is correct |
27 |
Correct |
300 ms |
757408 KB |
Output is correct |
28 |
Correct |
309 ms |
757396 KB |
Output is correct |
29 |
Correct |
308 ms |
757316 KB |
Output is correct |
30 |
Correct |
300 ms |
757316 KB |
Output is correct |
31 |
Correct |
328 ms |
757416 KB |
Output is correct |
32 |
Correct |
295 ms |
757380 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
295 ms |
757416 KB |
Output is correct |
36 |
Correct |
293 ms |
757320 KB |
Output is correct |
37 |
Correct |
298 ms |
757576 KB |
Output is correct |
38 |
Correct |
300 ms |
757408 KB |
Output is correct |
39 |
Correct |
308 ms |
757408 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
302 ms |
757356 KB |
Output is correct |
43 |
Correct |
331 ms |
757396 KB |
Output is correct |
44 |
Correct |
307 ms |
757336 KB |
Output is correct |
45 |
Correct |
310 ms |
757436 KB |
Output is correct |
46 |
Correct |
302 ms |
757484 KB |
Output is correct |
47 |
Correct |
329 ms |
757444 KB |
Output is correct |
48 |
Correct |
305 ms |
757340 KB |
Output is correct |
49 |
Correct |
295 ms |
757444 KB |
Output is correct |
50 |
Correct |
299 ms |
757444 KB |
Output is correct |
51 |
Correct |
297 ms |
757444 KB |
Output is correct |
52 |
Correct |
311 ms |
757448 KB |
Output is correct |
53 |
Correct |
291 ms |
757432 KB |
Output is correct |
54 |
Correct |
293 ms |
757344 KB |
Output is correct |
55 |
Correct |
312 ms |
757444 KB |
Output is correct |
56 |
Correct |
0 ms |
204 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
419 ms |
757444 KB |
Output is correct |
59 |
Correct |
415 ms |
757348 KB |
Output is correct |
60 |
Correct |
454 ms |
757452 KB |
Output is correct |
61 |
Correct |
459 ms |
757564 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
319 ms |
757432 KB |
Output is correct |
64 |
Correct |
326 ms |
757388 KB |
Output is correct |
65 |
Correct |
338 ms |
757444 KB |
Output is correct |
66 |
Correct |
433 ms |
757584 KB |
Output is correct |
67 |
Correct |
415 ms |
757416 KB |
Output is correct |
68 |
Correct |
452 ms |
757644 KB |
Output is correct |
69 |
Correct |
461 ms |
757576 KB |
Output is correct |
70 |
Correct |
437 ms |
757444 KB |
Output is correct |
71 |
Correct |
444 ms |
757444 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
1 ms |
204 KB |
Output is correct |