#include<bits/stdc++.h>
using namespace std;
const int maxn=405;
int inf=5e6;
int dp1[maxn][maxn][3],dp0[maxn][maxn][3];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
string s;
cin>>n>>s;
vector<int>w1,w2,w0;
vector<int>ted0(n),ted1(n),ted2(n);
for(int i=0;i<n;i++){
if(i!=0){
ted1[i]=ted1[i-1];
ted0[i]=ted0[i-1];
ted2[i]=ted2[i-1];
}
if(s[i]=='Y'){
ted0[i]++;
w0.push_back(i);
continue;
}
if(s[i]=='G'){
ted1[i]++;
w1.push_back(i);
continue;
}
ted2[i]++;
w2.push_back(i);
}
for(int ind=0;ind<n;ind++){
for(int i=0;i<maxn;i++){
for(int j=0;j<maxn;j++){
for(int h=0;h<3;h++){
dp0[i][j][h]=dp1[i][j][h];
}
}
}
for(int i=0;i<maxn;i++){
for(int j=0;j<maxn;j++){
for(int h=0;h<3;h++){
int z=ind+1-i-j;
if(i+j>ind+1||(h==2&&i+j==ind+1)){
dp1[i][j][h]=inf;
continue;
}
if(h==0){
if(i<=0||i>(int)w0.size()){
dp1[i][j][h]=inf;
continue;
}
dp1[i][j][h]=min(dp0[i-1][j][1],dp0[i-1][j][2])+max(ted1[w0[i-1]]-j,0)+max(ted2[w0[i-1]]-z,0);
}
if(h==1){
if(j<=0||j>(int)w1.size()){
dp1[i][j][h]=inf;
continue;
}
dp1[i][j][h]=min(dp0[i][j-1][0],dp0[i][j-1][2])+max(ted0[w1[j-1]]-i,0)+max(ted2[w1[j-1]]-z,0);
//cout<<"ted: "<<ted[w1[j-1]]<<"\n";
}
if(h==2){
if(z<=0||z>(int)w2.size()){
dp1[i][j][h]=inf;
continue;
}
dp1[i][j][h]=min(dp0[i][j][0],dp0[i][j][1])+max(ted1[w2[z-1]]-j,0)+max(ted0[w2[z-1]]-i,0);
}
// cout<<ind<<" "<<i<<" "<<j<<" "<<h<<" "<<dp1[i][j][h]<<"\n";
}
}
}
}
int res=min(dp1[(int)w0.size()][(int)w1.size()][1],min(dp1[(int)w0.size()][(int)w1.size()][0],dp1[(int)w0.size()][(int)w1.size()][2]));
//cout<<dp1[(int)w0.size()][(int)w1.size()][0]<<" "<<dp1[(int)w0.size()][(int)w1.size()][1]<<" "<<dp1[(int)w0.size()][(int)w1.size()][2]<<"\n";
//cout<<dp0[(int)w0.size()][(int)w1.size()-1][0]<<" "<<dp0[(int)w0.size()][(int)w1.size()-1][2]<<"\n";
if(res>=inf){
cout<<-1<<"\n";
}
else{
cout<<res<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4052 KB |
Output is correct |
2 |
Correct |
5 ms |
4052 KB |
Output is correct |
3 |
Correct |
5 ms |
4052 KB |
Output is correct |
4 |
Correct |
13 ms |
4052 KB |
Output is correct |
5 |
Correct |
14 ms |
4168 KB |
Output is correct |
6 |
Correct |
13 ms |
4052 KB |
Output is correct |
7 |
Correct |
14 ms |
4180 KB |
Output is correct |
8 |
Correct |
14 ms |
4172 KB |
Output is correct |
9 |
Correct |
13 ms |
4052 KB |
Output is correct |
10 |
Correct |
14 ms |
4052 KB |
Output is correct |
11 |
Correct |
13 ms |
4172 KB |
Output is correct |
12 |
Correct |
12 ms |
4052 KB |
Output is correct |
13 |
Correct |
12 ms |
4176 KB |
Output is correct |
14 |
Correct |
15 ms |
4168 KB |
Output is correct |
15 |
Correct |
12 ms |
4052 KB |
Output is correct |
16 |
Correct |
16 ms |
4052 KB |
Output is correct |
17 |
Correct |
16 ms |
4168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4052 KB |
Output is correct |
2 |
Correct |
5 ms |
4052 KB |
Output is correct |
3 |
Correct |
5 ms |
4052 KB |
Output is correct |
4 |
Correct |
13 ms |
4052 KB |
Output is correct |
5 |
Correct |
14 ms |
4168 KB |
Output is correct |
6 |
Correct |
13 ms |
4052 KB |
Output is correct |
7 |
Correct |
14 ms |
4180 KB |
Output is correct |
8 |
Correct |
14 ms |
4172 KB |
Output is correct |
9 |
Correct |
13 ms |
4052 KB |
Output is correct |
10 |
Correct |
14 ms |
4052 KB |
Output is correct |
11 |
Correct |
13 ms |
4172 KB |
Output is correct |
12 |
Correct |
12 ms |
4052 KB |
Output is correct |
13 |
Correct |
12 ms |
4176 KB |
Output is correct |
14 |
Correct |
15 ms |
4168 KB |
Output is correct |
15 |
Correct |
12 ms |
4052 KB |
Output is correct |
16 |
Correct |
16 ms |
4052 KB |
Output is correct |
17 |
Correct |
16 ms |
4168 KB |
Output is correct |
18 |
Correct |
46 ms |
4052 KB |
Output is correct |
19 |
Correct |
46 ms |
4168 KB |
Output is correct |
20 |
Correct |
44 ms |
4052 KB |
Output is correct |
21 |
Correct |
48 ms |
4168 KB |
Output is correct |
22 |
Correct |
45 ms |
4052 KB |
Output is correct |
23 |
Correct |
47 ms |
4052 KB |
Output is correct |
24 |
Correct |
46 ms |
4052 KB |
Output is correct |
25 |
Correct |
48 ms |
4052 KB |
Output is correct |
26 |
Correct |
44 ms |
4164 KB |
Output is correct |
27 |
Correct |
46 ms |
4164 KB |
Output is correct |
28 |
Correct |
46 ms |
4052 KB |
Output is correct |
29 |
Correct |
43 ms |
4052 KB |
Output is correct |
30 |
Correct |
46 ms |
4148 KB |
Output is correct |
31 |
Correct |
49 ms |
4168 KB |
Output is correct |
32 |
Correct |
52 ms |
4148 KB |
Output is correct |
33 |
Correct |
51 ms |
4172 KB |
Output is correct |
34 |
Correct |
59 ms |
4164 KB |
Output is correct |
35 |
Correct |
43 ms |
4168 KB |
Output is correct |
36 |
Correct |
43 ms |
4164 KB |
Output is correct |
37 |
Correct |
45 ms |
4148 KB |
Output is correct |
38 |
Correct |
55 ms |
4052 KB |
Output is correct |
39 |
Correct |
43 ms |
4168 KB |
Output is correct |
40 |
Correct |
43 ms |
4052 KB |
Output is correct |
41 |
Correct |
43 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4052 KB |
Output is correct |
2 |
Correct |
348 ms |
4156 KB |
Output is correct |
3 |
Correct |
340 ms |
4160 KB |
Output is correct |
4 |
Correct |
402 ms |
4172 KB |
Output is correct |
5 |
Correct |
362 ms |
4164 KB |
Output is correct |
6 |
Correct |
348 ms |
4168 KB |
Output is correct |
7 |
Correct |
382 ms |
4160 KB |
Output is correct |
8 |
Correct |
339 ms |
4052 KB |
Output is correct |
9 |
Correct |
386 ms |
4180 KB |
Output is correct |
10 |
Correct |
335 ms |
4160 KB |
Output is correct |
11 |
Correct |
332 ms |
4180 KB |
Output is correct |
12 |
Correct |
147 ms |
4052 KB |
Output is correct |
13 |
Correct |
230 ms |
4156 KB |
Output is correct |
14 |
Correct |
257 ms |
4160 KB |
Output is correct |
15 |
Correct |
341 ms |
4052 KB |
Output is correct |
16 |
Correct |
368 ms |
4168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4052 KB |
Output is correct |
2 |
Correct |
5 ms |
4052 KB |
Output is correct |
3 |
Correct |
5 ms |
4052 KB |
Output is correct |
4 |
Correct |
13 ms |
4052 KB |
Output is correct |
5 |
Correct |
14 ms |
4168 KB |
Output is correct |
6 |
Correct |
13 ms |
4052 KB |
Output is correct |
7 |
Correct |
14 ms |
4180 KB |
Output is correct |
8 |
Correct |
14 ms |
4172 KB |
Output is correct |
9 |
Correct |
13 ms |
4052 KB |
Output is correct |
10 |
Correct |
14 ms |
4052 KB |
Output is correct |
11 |
Correct |
13 ms |
4172 KB |
Output is correct |
12 |
Correct |
12 ms |
4052 KB |
Output is correct |
13 |
Correct |
12 ms |
4176 KB |
Output is correct |
14 |
Correct |
15 ms |
4168 KB |
Output is correct |
15 |
Correct |
12 ms |
4052 KB |
Output is correct |
16 |
Correct |
16 ms |
4052 KB |
Output is correct |
17 |
Correct |
16 ms |
4168 KB |
Output is correct |
18 |
Correct |
46 ms |
4052 KB |
Output is correct |
19 |
Correct |
46 ms |
4168 KB |
Output is correct |
20 |
Correct |
44 ms |
4052 KB |
Output is correct |
21 |
Correct |
48 ms |
4168 KB |
Output is correct |
22 |
Correct |
45 ms |
4052 KB |
Output is correct |
23 |
Correct |
47 ms |
4052 KB |
Output is correct |
24 |
Correct |
46 ms |
4052 KB |
Output is correct |
25 |
Correct |
48 ms |
4052 KB |
Output is correct |
26 |
Correct |
44 ms |
4164 KB |
Output is correct |
27 |
Correct |
46 ms |
4164 KB |
Output is correct |
28 |
Correct |
46 ms |
4052 KB |
Output is correct |
29 |
Correct |
43 ms |
4052 KB |
Output is correct |
30 |
Correct |
46 ms |
4148 KB |
Output is correct |
31 |
Correct |
49 ms |
4168 KB |
Output is correct |
32 |
Correct |
52 ms |
4148 KB |
Output is correct |
33 |
Correct |
51 ms |
4172 KB |
Output is correct |
34 |
Correct |
59 ms |
4164 KB |
Output is correct |
35 |
Correct |
43 ms |
4168 KB |
Output is correct |
36 |
Correct |
43 ms |
4164 KB |
Output is correct |
37 |
Correct |
45 ms |
4148 KB |
Output is correct |
38 |
Correct |
55 ms |
4052 KB |
Output is correct |
39 |
Correct |
43 ms |
4168 KB |
Output is correct |
40 |
Correct |
43 ms |
4052 KB |
Output is correct |
41 |
Correct |
43 ms |
4172 KB |
Output is correct |
42 |
Correct |
4 ms |
4052 KB |
Output is correct |
43 |
Correct |
348 ms |
4156 KB |
Output is correct |
44 |
Correct |
340 ms |
4160 KB |
Output is correct |
45 |
Correct |
402 ms |
4172 KB |
Output is correct |
46 |
Correct |
362 ms |
4164 KB |
Output is correct |
47 |
Correct |
348 ms |
4168 KB |
Output is correct |
48 |
Correct |
382 ms |
4160 KB |
Output is correct |
49 |
Correct |
339 ms |
4052 KB |
Output is correct |
50 |
Correct |
386 ms |
4180 KB |
Output is correct |
51 |
Correct |
335 ms |
4160 KB |
Output is correct |
52 |
Correct |
332 ms |
4180 KB |
Output is correct |
53 |
Correct |
147 ms |
4052 KB |
Output is correct |
54 |
Correct |
230 ms |
4156 KB |
Output is correct |
55 |
Correct |
257 ms |
4160 KB |
Output is correct |
56 |
Correct |
341 ms |
4052 KB |
Output is correct |
57 |
Correct |
368 ms |
4168 KB |
Output is correct |
58 |
Correct |
341 ms |
4164 KB |
Output is correct |
59 |
Correct |
354 ms |
4160 KB |
Output is correct |
60 |
Correct |
427 ms |
4160 KB |
Output is correct |
61 |
Correct |
332 ms |
4180 KB |
Output is correct |
62 |
Correct |
367 ms |
4164 KB |
Output is correct |
63 |
Correct |
337 ms |
4156 KB |
Output is correct |
64 |
Correct |
355 ms |
4156 KB |
Output is correct |
65 |
Correct |
348 ms |
4156 KB |
Output is correct |
66 |
Correct |
345 ms |
4164 KB |
Output is correct |
67 |
Correct |
368 ms |
4160 KB |
Output is correct |
68 |
Correct |
354 ms |
4172 KB |
Output is correct |
69 |
Correct |
408 ms |
4156 KB |
Output is correct |
70 |
Correct |
372 ms |
4052 KB |
Output is correct |
71 |
Correct |
402 ms |
4160 KB |
Output is correct |
72 |
Correct |
341 ms |
4052 KB |
Output is correct |
73 |
Correct |
358 ms |
4160 KB |
Output is correct |