#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int n,dp[4][302][302][302];
vector<int>v[4];
string s;
int f(int x,int ir,int ig,int iy)
{
if(ir+ig+iy==n)return 0;
if(dp[x][ir][ig][iy]!=-1)return dp[x][ir][ig][iy];
int ans=1e9,ind=ir+ig+iy,id;
for(int i=0;i<3;i++)
{
if(i==x)continue;
if(i==0)
{
if(ir==v[0].size())continue;
int id=v[i][ir],ad=0;
ad-=lower_bound(v[1].begin(),v[1].begin()+ig,id)-v[1].begin();
ad+=ig;
ad-=lower_bound(v[2].begin(),v[2].begin()+iy,id)-v[2].begin();
ad+=iy;
ans=min(ans,f(0,ir+1,ig,iy)+abs((id+ad)-ind));
}
if(i==1)
{
if(ig==v[1].size())continue;
int id=v[i][ig],ad=0;
ad-=lower_bound(v[0].begin(),v[0].begin()+ir,id)-v[0].begin();
ad+=ir;
ad-=lower_bound(v[2].begin(),v[2].begin()+iy,id)-v[2].begin();
ad+=iy;
ans=min(ans,f(1,ir,ig+1,iy)+abs((id+ad)-ind));
}
if(i==2)
{
if(iy==v[2].size())continue;
int id=v[i][iy],ad=0;
ad-=lower_bound(v[1].begin(),v[1].begin()+ig,id)-v[1].begin();
ad+=ig;
ad-=lower_bound(v[0].begin(),v[0].begin()+ir,id)-v[0].begin();
ad+=ir;
ans=min(ans,f(2,ir,ig,iy+1)+abs((id+ad)-ind));
}
}
return dp[x][ir][ig][iy]=ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen("dec.in","r",stdin);
//freopen("dec.out","w",stdout);
memset(dp,-1,sizeof dp);
cin>>n>>s;
for(int i=0;i<n;i++)
{
if(s[i]=='R')v[0].push_back(i);
if(s[i]=='G')v[1].push_back(i);
if(s[i]=='Y')v[2].push_back(i);
}
int mx=max(v[0].size(),max(v[1].size(),v[2].size()));
if(mx-1>n-mx)
{
cout<<-1;
return 0;
}
int ans=f(3,0,0,0);
cout<<ans;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int f(int, int, int, int)':
joi2019_ho_t3.cpp:17:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ir==v[0].size())continue;
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:27:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ig==v[1].size())continue;
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:37:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(iy==v[2].size())continue;
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:11:30: warning: unused variable 'id' [-Wunused-variable]
int ans=1e9,ind=ir+ig+iy,id;
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
431480 KB |
Output is correct |
2 |
Correct |
257 ms |
431576 KB |
Output is correct |
3 |
Correct |
258 ms |
431468 KB |
Output is correct |
4 |
Correct |
271 ms |
431480 KB |
Output is correct |
5 |
Correct |
256 ms |
431544 KB |
Output is correct |
6 |
Correct |
257 ms |
431536 KB |
Output is correct |
7 |
Correct |
255 ms |
431480 KB |
Output is correct |
8 |
Correct |
262 ms |
431608 KB |
Output is correct |
9 |
Correct |
258 ms |
431572 KB |
Output is correct |
10 |
Correct |
258 ms |
431480 KB |
Output is correct |
11 |
Correct |
245 ms |
431636 KB |
Output is correct |
12 |
Correct |
255 ms |
431584 KB |
Output is correct |
13 |
Correct |
254 ms |
431608 KB |
Output is correct |
14 |
Correct |
259 ms |
431568 KB |
Output is correct |
15 |
Correct |
265 ms |
431480 KB |
Output is correct |
16 |
Correct |
258 ms |
431584 KB |
Output is correct |
17 |
Correct |
248 ms |
431492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
431480 KB |
Output is correct |
2 |
Correct |
257 ms |
431576 KB |
Output is correct |
3 |
Correct |
258 ms |
431468 KB |
Output is correct |
4 |
Correct |
271 ms |
431480 KB |
Output is correct |
5 |
Correct |
256 ms |
431544 KB |
Output is correct |
6 |
Correct |
257 ms |
431536 KB |
Output is correct |
7 |
Correct |
255 ms |
431480 KB |
Output is correct |
8 |
Correct |
262 ms |
431608 KB |
Output is correct |
9 |
Correct |
258 ms |
431572 KB |
Output is correct |
10 |
Correct |
258 ms |
431480 KB |
Output is correct |
11 |
Correct |
245 ms |
431636 KB |
Output is correct |
12 |
Correct |
255 ms |
431584 KB |
Output is correct |
13 |
Correct |
254 ms |
431608 KB |
Output is correct |
14 |
Correct |
259 ms |
431568 KB |
Output is correct |
15 |
Correct |
265 ms |
431480 KB |
Output is correct |
16 |
Correct |
258 ms |
431584 KB |
Output is correct |
17 |
Correct |
248 ms |
431492 KB |
Output is correct |
18 |
Correct |
260 ms |
431480 KB |
Output is correct |
19 |
Correct |
246 ms |
431584 KB |
Output is correct |
20 |
Correct |
262 ms |
431608 KB |
Output is correct |
21 |
Correct |
246 ms |
431480 KB |
Output is correct |
22 |
Correct |
255 ms |
431648 KB |
Output is correct |
23 |
Correct |
255 ms |
431608 KB |
Output is correct |
24 |
Correct |
258 ms |
431480 KB |
Output is correct |
25 |
Correct |
252 ms |
431608 KB |
Output is correct |
26 |
Correct |
251 ms |
431480 KB |
Output is correct |
27 |
Correct |
249 ms |
431608 KB |
Output is correct |
28 |
Correct |
249 ms |
431480 KB |
Output is correct |
29 |
Correct |
251 ms |
431608 KB |
Output is correct |
30 |
Correct |
245 ms |
431648 KB |
Output is correct |
31 |
Correct |
247 ms |
431480 KB |
Output is correct |
32 |
Correct |
251 ms |
431480 KB |
Output is correct |
33 |
Correct |
250 ms |
431480 KB |
Output is correct |
34 |
Correct |
253 ms |
431480 KB |
Output is correct |
35 |
Correct |
255 ms |
431480 KB |
Output is correct |
36 |
Correct |
243 ms |
431608 KB |
Output is correct |
37 |
Correct |
245 ms |
431480 KB |
Output is correct |
38 |
Correct |
252 ms |
431736 KB |
Output is correct |
39 |
Correct |
247 ms |
431608 KB |
Output is correct |
40 |
Correct |
251 ms |
431480 KB |
Output is correct |
41 |
Correct |
254 ms |
431608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
262 ms |
431480 KB |
Output is correct |
2 |
Correct |
243 ms |
431612 KB |
Output is correct |
3 |
Correct |
248 ms |
431608 KB |
Output is correct |
4 |
Correct |
247 ms |
431608 KB |
Output is correct |
5 |
Correct |
253 ms |
431608 KB |
Output is correct |
6 |
Correct |
243 ms |
431608 KB |
Output is correct |
7 |
Correct |
239 ms |
431608 KB |
Output is correct |
8 |
Correct |
253 ms |
431612 KB |
Output is correct |
9 |
Correct |
247 ms |
431608 KB |
Output is correct |
10 |
Correct |
263 ms |
431608 KB |
Output is correct |
11 |
Correct |
251 ms |
431736 KB |
Output is correct |
12 |
Correct |
244 ms |
431612 KB |
Output is correct |
13 |
Correct |
247 ms |
431608 KB |
Output is correct |
14 |
Correct |
246 ms |
431608 KB |
Output is correct |
15 |
Correct |
257 ms |
431480 KB |
Output is correct |
16 |
Correct |
247 ms |
431480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
431480 KB |
Output is correct |
2 |
Correct |
257 ms |
431576 KB |
Output is correct |
3 |
Correct |
258 ms |
431468 KB |
Output is correct |
4 |
Correct |
271 ms |
431480 KB |
Output is correct |
5 |
Correct |
256 ms |
431544 KB |
Output is correct |
6 |
Correct |
257 ms |
431536 KB |
Output is correct |
7 |
Correct |
255 ms |
431480 KB |
Output is correct |
8 |
Correct |
262 ms |
431608 KB |
Output is correct |
9 |
Correct |
258 ms |
431572 KB |
Output is correct |
10 |
Correct |
258 ms |
431480 KB |
Output is correct |
11 |
Correct |
245 ms |
431636 KB |
Output is correct |
12 |
Correct |
255 ms |
431584 KB |
Output is correct |
13 |
Correct |
254 ms |
431608 KB |
Output is correct |
14 |
Correct |
259 ms |
431568 KB |
Output is correct |
15 |
Correct |
265 ms |
431480 KB |
Output is correct |
16 |
Correct |
258 ms |
431584 KB |
Output is correct |
17 |
Correct |
248 ms |
431492 KB |
Output is correct |
18 |
Correct |
260 ms |
431480 KB |
Output is correct |
19 |
Correct |
246 ms |
431584 KB |
Output is correct |
20 |
Correct |
262 ms |
431608 KB |
Output is correct |
21 |
Correct |
246 ms |
431480 KB |
Output is correct |
22 |
Correct |
255 ms |
431648 KB |
Output is correct |
23 |
Correct |
255 ms |
431608 KB |
Output is correct |
24 |
Correct |
258 ms |
431480 KB |
Output is correct |
25 |
Correct |
252 ms |
431608 KB |
Output is correct |
26 |
Correct |
251 ms |
431480 KB |
Output is correct |
27 |
Correct |
249 ms |
431608 KB |
Output is correct |
28 |
Correct |
249 ms |
431480 KB |
Output is correct |
29 |
Correct |
251 ms |
431608 KB |
Output is correct |
30 |
Correct |
245 ms |
431648 KB |
Output is correct |
31 |
Correct |
247 ms |
431480 KB |
Output is correct |
32 |
Correct |
251 ms |
431480 KB |
Output is correct |
33 |
Correct |
250 ms |
431480 KB |
Output is correct |
34 |
Correct |
253 ms |
431480 KB |
Output is correct |
35 |
Correct |
255 ms |
431480 KB |
Output is correct |
36 |
Correct |
243 ms |
431608 KB |
Output is correct |
37 |
Correct |
245 ms |
431480 KB |
Output is correct |
38 |
Correct |
252 ms |
431736 KB |
Output is correct |
39 |
Correct |
247 ms |
431608 KB |
Output is correct |
40 |
Correct |
251 ms |
431480 KB |
Output is correct |
41 |
Correct |
254 ms |
431608 KB |
Output is correct |
42 |
Correct |
262 ms |
431480 KB |
Output is correct |
43 |
Correct |
243 ms |
431612 KB |
Output is correct |
44 |
Correct |
248 ms |
431608 KB |
Output is correct |
45 |
Correct |
247 ms |
431608 KB |
Output is correct |
46 |
Correct |
253 ms |
431608 KB |
Output is correct |
47 |
Correct |
243 ms |
431608 KB |
Output is correct |
48 |
Correct |
239 ms |
431608 KB |
Output is correct |
49 |
Correct |
253 ms |
431612 KB |
Output is correct |
50 |
Correct |
247 ms |
431608 KB |
Output is correct |
51 |
Correct |
263 ms |
431608 KB |
Output is correct |
52 |
Correct |
251 ms |
431736 KB |
Output is correct |
53 |
Correct |
244 ms |
431612 KB |
Output is correct |
54 |
Correct |
247 ms |
431608 KB |
Output is correct |
55 |
Correct |
246 ms |
431608 KB |
Output is correct |
56 |
Correct |
257 ms |
431480 KB |
Output is correct |
57 |
Correct |
247 ms |
431480 KB |
Output is correct |
58 |
Execution timed out |
699 ms |
431736 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |