//IN THE NAME OF GOD
#include<bits/stdc++.h>
//#pragma GCC optimize("O2,unroll-loops")
//#pragma GCC target("sse4")
#define endl '\n'
#define F first
#define S second
#define pii pair<int,int>
#define mp make_pair
#define all(x) x.begin(),x.end()
#define pb push_back
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define file_io freopen("input.in" , "r" , stdin) ; freopen("output.out" , "w" , stdout);
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double dll;
const int N = 207, inf=1e9;
int A[N*2], dp[3][N][N][N],sum[3][N*2], te[3], po[3][N*2];
int32_t main(){
fast_io;
int n; cin>>n;
string s; cin>>s; s='#'+s;
for(int i=1; i<=n; i++){
if(s[i]=='R'){
A[i]=0;
te[0]++;
po[0][te[0]]=i;
}
else if(s[i]=='G'){
A[i]=1;
te[1]++;
po[1][te[1]]=i;
}
else{
A[i]=2;
te[2]++;
po[2][te[2]]=i;
}
}
if(te[0]>(te[1]+te[2]+1)||te[1]>(te[0]+te[2]+1)||te[2]>(te[0]+te[1]+1)) return cout<<-1<<endl,0;
for(int i=0; i<3; i++){
for(int j=1; j<=n; j++) sum[i][j]=sum[i][j-1]+(A[j]==i);
}
for(int id=0; id<3; id++) for(int a=0; a<N; a++) for(int b=0; b<N; b++) for(int c=0; c<N; c++) dp[id][a][b][c]=inf;
for(int i=0; i<3; i++) dp[i][0][0][0]=0;
for(int a=0; a<=te[0]; a++){
for(int b=0; b<=te[1]; b++){
for(int c=0; c<=te[2]; c++){
for(int now=0; now<3; now++){
for(int lst=0; lst<3; lst++){
if(lst==now||(a==te[0]&&now==0)||(b==te[1]&&now==1)||(c==te[2]&&now==2)) continue;
int po0,po1,po2,fa,mn=dp[lst][a][b][c];
if(lst==0){
po0=po[0][a];
po1=(now==1?po[1][b+1]:po[2][c+1]);
po2=(now==1?po[2][c]:po[1][b]);
fa=(now==1?2:1);
}
else if(lst==1){
po0=po[1][b];
po1=(now==0?po[0][a+1]:po[2][c+1]);
po2=(now==0?po[2][c]:po[0][a]);
fa=(now==0?2:0);
}
else{
po0=po[2][c];
po1=(now==0?po[0][a+1]:po[1][b+1]);
po2=(now==0?po[1][b]:po[0][a]);
fa=(now==0?1:0);
}
mn=mn+max(0,sum[lst][po1]-sum[lst][po0])+max(0,sum[fa][po1]-sum[fa][po2]);
dp[now][a+(now==0)][b+(now==1)][c+(now==2)]=min(dp[now][a+(now==0)][b+(now==1)][c+(now==2)],mn);
}
}
}
}
}
int ans=inf;
for(int i=0; i<3; i++) ans=min(ans,dp[i][te[0]][te[1]][te[2]]);
cout<<(ans==inf?-1:ans)<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
104532 KB |
Output is correct |
2 |
Correct |
24 ms |
104436 KB |
Output is correct |
3 |
Correct |
23 ms |
104612 KB |
Output is correct |
4 |
Correct |
23 ms |
104540 KB |
Output is correct |
5 |
Correct |
23 ms |
104428 KB |
Output is correct |
6 |
Correct |
23 ms |
104592 KB |
Output is correct |
7 |
Correct |
23 ms |
104360 KB |
Output is correct |
8 |
Correct |
24 ms |
104536 KB |
Output is correct |
9 |
Correct |
25 ms |
104540 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
23 ms |
104604 KB |
Output is correct |
12 |
Correct |
24 ms |
104540 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
23 ms |
104540 KB |
Output is correct |
15 |
Correct |
24 ms |
104540 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
104532 KB |
Output is correct |
2 |
Correct |
24 ms |
104436 KB |
Output is correct |
3 |
Correct |
23 ms |
104612 KB |
Output is correct |
4 |
Correct |
23 ms |
104540 KB |
Output is correct |
5 |
Correct |
23 ms |
104428 KB |
Output is correct |
6 |
Correct |
23 ms |
104592 KB |
Output is correct |
7 |
Correct |
23 ms |
104360 KB |
Output is correct |
8 |
Correct |
24 ms |
104536 KB |
Output is correct |
9 |
Correct |
25 ms |
104540 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
23 ms |
104604 KB |
Output is correct |
12 |
Correct |
24 ms |
104540 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
23 ms |
104540 KB |
Output is correct |
15 |
Correct |
24 ms |
104540 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
23 ms |
104540 KB |
Output is correct |
19 |
Correct |
24 ms |
104532 KB |
Output is correct |
20 |
Correct |
27 ms |
104532 KB |
Output is correct |
21 |
Correct |
23 ms |
104540 KB |
Output is correct |
22 |
Correct |
24 ms |
104528 KB |
Output is correct |
23 |
Correct |
23 ms |
104532 KB |
Output is correct |
24 |
Correct |
24 ms |
104540 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
24 ms |
104720 KB |
Output is correct |
28 |
Correct |
24 ms |
104572 KB |
Output is correct |
29 |
Correct |
24 ms |
104532 KB |
Output is correct |
30 |
Correct |
24 ms |
104536 KB |
Output is correct |
31 |
Correct |
24 ms |
104536 KB |
Output is correct |
32 |
Correct |
24 ms |
104456 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
23 ms |
104456 KB |
Output is correct |
36 |
Correct |
23 ms |
104540 KB |
Output is correct |
37 |
Correct |
22 ms |
104532 KB |
Output is correct |
38 |
Correct |
23 ms |
104532 KB |
Output is correct |
39 |
Correct |
23 ms |
104532 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
104544 KB |
Output is correct |
2 |
Correct |
29 ms |
104540 KB |
Output is correct |
3 |
Correct |
34 ms |
104588 KB |
Output is correct |
4 |
Correct |
29 ms |
104540 KB |
Output is correct |
5 |
Correct |
33 ms |
104540 KB |
Output is correct |
6 |
Correct |
29 ms |
104580 KB |
Output is correct |
7 |
Correct |
30 ms |
104780 KB |
Output is correct |
8 |
Correct |
31 ms |
104488 KB |
Output is correct |
9 |
Correct |
29 ms |
104540 KB |
Output is correct |
10 |
Correct |
35 ms |
104436 KB |
Output is correct |
11 |
Correct |
32 ms |
104532 KB |
Output is correct |
12 |
Correct |
25 ms |
104416 KB |
Output is correct |
13 |
Correct |
28 ms |
104540 KB |
Output is correct |
14 |
Correct |
28 ms |
104540 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
104532 KB |
Output is correct |
2 |
Correct |
24 ms |
104436 KB |
Output is correct |
3 |
Correct |
23 ms |
104612 KB |
Output is correct |
4 |
Correct |
23 ms |
104540 KB |
Output is correct |
5 |
Correct |
23 ms |
104428 KB |
Output is correct |
6 |
Correct |
23 ms |
104592 KB |
Output is correct |
7 |
Correct |
23 ms |
104360 KB |
Output is correct |
8 |
Correct |
24 ms |
104536 KB |
Output is correct |
9 |
Correct |
25 ms |
104540 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
23 ms |
104604 KB |
Output is correct |
12 |
Correct |
24 ms |
104540 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
23 ms |
104540 KB |
Output is correct |
15 |
Correct |
24 ms |
104540 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
23 ms |
104540 KB |
Output is correct |
19 |
Correct |
24 ms |
104532 KB |
Output is correct |
20 |
Correct |
27 ms |
104532 KB |
Output is correct |
21 |
Correct |
23 ms |
104540 KB |
Output is correct |
22 |
Correct |
24 ms |
104528 KB |
Output is correct |
23 |
Correct |
23 ms |
104532 KB |
Output is correct |
24 |
Correct |
24 ms |
104540 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
24 ms |
104720 KB |
Output is correct |
28 |
Correct |
24 ms |
104572 KB |
Output is correct |
29 |
Correct |
24 ms |
104532 KB |
Output is correct |
30 |
Correct |
24 ms |
104536 KB |
Output is correct |
31 |
Correct |
24 ms |
104536 KB |
Output is correct |
32 |
Correct |
24 ms |
104456 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
23 ms |
104456 KB |
Output is correct |
36 |
Correct |
23 ms |
104540 KB |
Output is correct |
37 |
Correct |
22 ms |
104532 KB |
Output is correct |
38 |
Correct |
23 ms |
104532 KB |
Output is correct |
39 |
Correct |
23 ms |
104532 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
22 ms |
104544 KB |
Output is correct |
43 |
Correct |
29 ms |
104540 KB |
Output is correct |
44 |
Correct |
34 ms |
104588 KB |
Output is correct |
45 |
Correct |
29 ms |
104540 KB |
Output is correct |
46 |
Correct |
33 ms |
104540 KB |
Output is correct |
47 |
Correct |
29 ms |
104580 KB |
Output is correct |
48 |
Correct |
30 ms |
104780 KB |
Output is correct |
49 |
Correct |
31 ms |
104488 KB |
Output is correct |
50 |
Correct |
29 ms |
104540 KB |
Output is correct |
51 |
Correct |
35 ms |
104436 KB |
Output is correct |
52 |
Correct |
32 ms |
104532 KB |
Output is correct |
53 |
Correct |
25 ms |
104416 KB |
Output is correct |
54 |
Correct |
28 ms |
104540 KB |
Output is correct |
55 |
Correct |
28 ms |
104540 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
112 ms |
104540 KB |
Output is correct |
59 |
Correct |
105 ms |
104364 KB |
Output is correct |
60 |
Correct |
104 ms |
104540 KB |
Output is correct |
61 |
Correct |
105 ms |
104444 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
41 ms |
104400 KB |
Output is correct |
64 |
Correct |
72 ms |
104540 KB |
Output is correct |
65 |
Correct |
86 ms |
104528 KB |
Output is correct |
66 |
Correct |
104 ms |
104532 KB |
Output is correct |
67 |
Correct |
107 ms |
104560 KB |
Output is correct |
68 |
Correct |
104 ms |
104588 KB |
Output is correct |
69 |
Correct |
112 ms |
104592 KB |
Output is correct |
70 |
Correct |
107 ms |
104584 KB |
Output is correct |
71 |
Correct |
103 ms |
104588 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |