#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<ll,ll>
#define iii tuple<ll,ll,ll>
const int maxn=205;
const int inf=1e8;
int dp[maxn][maxn][maxn][4];
int cost[maxn][maxn][maxn][4];
vector<int> r,g,y;
int suff[410];
int solve(int a, int b, int c, int flag){
if (a<0 || b<0 || c<0) return inf;
if (a==0 && b==0 && c==0) return 0;
if (dp[a][b][c][flag]!=-1) return dp[a][b][c][flag];
if (flag==1){
dp[a][b][c][flag] = min(solve(a-1,b,c,2),solve(a-1,b,c,3));
}
if (flag==2){
dp[a][b][c][flag] = min(solve(a,b-1,c,1),solve(a,b-1,c,3));
}
if (flag==3){
dp[a][b][c][flag] = min(solve(a,b,c-1,1),solve(a,b,c-1,2));
}
dp[a][b][c][flag]+=cost[a][b][c][flag];
return dp[a][b][c][flag];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
string s;
cin>>s;
for (int i=1;i<=n;i++){
if (s[i-1]=='R') r.emplace_back(i);
if (s[i-1]=='Y') y.emplace_back(i);
if (s[i-1]=='G') g.emplace_back(i);
}
if (2*max({r.size(),g.size(),y.size()})>n+(n&1)){
cout<<-1;
return 0;
}
for (int i=0;i<=r.size();i++){
for (int j=0;j<=g.size();j++){
//create suffix sum array
memset(suff,0,sizeof(suff));
for (int k=0;k<i;k++) suff[r[k]]=1;
for (int k=0;k<j;k++) suff[g[k]]=1;
for (int k=405;k>=0;k--) suff[k]+=suff[k+1];
for (int k=0;k<y.size();k++){
cost[i][j][k+1][3]=abs((suff[y[k]]+y[k])-(i+j+k+1));
}
}
}
for (int i=0;i<=g.size();i++){
for (int j=0;j<=y.size();j++){
//create suffix sum array
memset(suff,0,sizeof(suff));
for (int k=0;k<i;k++) suff[g[k]]=1;
for (int k=0;k<j;k++) suff[y[k]]=1;
for (int k=405;k>=0;k--) suff[k]+=suff[k+1];
for (int k=0;k<r.size();k++){
cost[k+1][i][j][1]=abs((suff[r[k]]+r[k])-(i+j+k+1));
}
}
}
for (int i=0;i<=y.size();i++){
for (int j=0;j<=r.size();j++){
//create suffix sum array
memset(suff,0,sizeof(suff));
for (int k=0;k<i;k++) suff[y[k]]=1;
for (int k=0;k<j;k++) suff[r[k]]=1;
for (int k=405;k>=0;k--) suff[k]+=suff[k+1];
for (int k=0;k<g.size();k++){
cost[j][k+1][i][2]=abs((suff[g[k]]+g[k])-(i+j+k+1));
}
}
}
memset(dp,-1,sizeof(dp));
cout<<min({solve(r.size(),g.size(),y.size(),1),
solve(r.size(),g.size(),y.size(),2),
solve(r.size(),g.size(),y.size(),3)});
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:43:41: warning: comparison of integer expressions of different signedness: 'long unsigned int' and 'int' [-Wsign-compare]
43 | if (2*max({r.size(),g.size(),y.size()})>n+(n&1)){
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
joi2019_ho_t3.cpp:47:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i=0;i<=r.size();i++){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:48:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int j=0;j<=g.size();j++){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:54:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int k=0;k<y.size();k++){
| ~^~~~~~~~~
joi2019_ho_t3.cpp:59:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i=0;i<=g.size();i++){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:60:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int j=0;j<=y.size();j++){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:66:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int k=0;k<r.size();k++){
| ~^~~~~~~~~
joi2019_ho_t3.cpp:71:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (int i=0;i<=y.size();i++){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:72:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int j=0;j<=r.size();j++){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:78:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (int k=0;k<g.size();k++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
138612 KB |
Output is correct |
2 |
Correct |
18 ms |
138588 KB |
Output is correct |
3 |
Correct |
18 ms |
136800 KB |
Output is correct |
4 |
Correct |
18 ms |
138588 KB |
Output is correct |
5 |
Correct |
18 ms |
140712 KB |
Output is correct |
6 |
Correct |
18 ms |
140636 KB |
Output is correct |
7 |
Correct |
19 ms |
142940 KB |
Output is correct |
8 |
Correct |
18 ms |
140636 KB |
Output is correct |
9 |
Correct |
18 ms |
140524 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
18 ms |
140756 KB |
Output is correct |
12 |
Correct |
20 ms |
140616 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
19 ms |
140580 KB |
Output is correct |
15 |
Correct |
18 ms |
140624 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
138612 KB |
Output is correct |
2 |
Correct |
18 ms |
138588 KB |
Output is correct |
3 |
Correct |
18 ms |
136800 KB |
Output is correct |
4 |
Correct |
18 ms |
138588 KB |
Output is correct |
5 |
Correct |
18 ms |
140712 KB |
Output is correct |
6 |
Correct |
18 ms |
140636 KB |
Output is correct |
7 |
Correct |
19 ms |
142940 KB |
Output is correct |
8 |
Correct |
18 ms |
140636 KB |
Output is correct |
9 |
Correct |
18 ms |
140524 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
18 ms |
140756 KB |
Output is correct |
12 |
Correct |
20 ms |
140616 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
19 ms |
140580 KB |
Output is correct |
15 |
Correct |
18 ms |
140624 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
24 ms |
153084 KB |
Output is correct |
19 |
Correct |
20 ms |
150876 KB |
Output is correct |
20 |
Correct |
22 ms |
153064 KB |
Output is correct |
21 |
Correct |
23 ms |
150892 KB |
Output is correct |
22 |
Correct |
22 ms |
153064 KB |
Output is correct |
23 |
Correct |
22 ms |
148820 KB |
Output is correct |
24 |
Correct |
20 ms |
144732 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
21 ms |
154984 KB |
Output is correct |
28 |
Correct |
22 ms |
150964 KB |
Output is correct |
29 |
Correct |
20 ms |
150872 KB |
Output is correct |
30 |
Correct |
20 ms |
148828 KB |
Output is correct |
31 |
Correct |
20 ms |
146948 KB |
Output is correct |
32 |
Correct |
31 ms |
148884 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
22 ms |
152924 KB |
Output is correct |
36 |
Correct |
21 ms |
150876 KB |
Output is correct |
37 |
Correct |
20 ms |
146944 KB |
Output is correct |
38 |
Correct |
20 ms |
146772 KB |
Output is correct |
39 |
Correct |
22 ms |
151336 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
138580 KB |
Output is correct |
2 |
Correct |
93 ms |
270204 KB |
Output is correct |
3 |
Correct |
77 ms |
270544 KB |
Output is correct |
4 |
Correct |
76 ms |
270008 KB |
Output is correct |
5 |
Correct |
74 ms |
270008 KB |
Output is correct |
6 |
Correct |
76 ms |
270164 KB |
Output is correct |
7 |
Correct |
85 ms |
270160 KB |
Output is correct |
8 |
Correct |
75 ms |
270016 KB |
Output is correct |
9 |
Correct |
73 ms |
270164 KB |
Output is correct |
10 |
Correct |
74 ms |
270160 KB |
Output is correct |
11 |
Correct |
74 ms |
270160 KB |
Output is correct |
12 |
Correct |
38 ms |
206492 KB |
Output is correct |
13 |
Correct |
59 ms |
228964 KB |
Output is correct |
14 |
Correct |
59 ms |
247496 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
138612 KB |
Output is correct |
2 |
Correct |
18 ms |
138588 KB |
Output is correct |
3 |
Correct |
18 ms |
136800 KB |
Output is correct |
4 |
Correct |
18 ms |
138588 KB |
Output is correct |
5 |
Correct |
18 ms |
140712 KB |
Output is correct |
6 |
Correct |
18 ms |
140636 KB |
Output is correct |
7 |
Correct |
19 ms |
142940 KB |
Output is correct |
8 |
Correct |
18 ms |
140636 KB |
Output is correct |
9 |
Correct |
18 ms |
140524 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
18 ms |
140756 KB |
Output is correct |
12 |
Correct |
20 ms |
140616 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
19 ms |
140580 KB |
Output is correct |
15 |
Correct |
18 ms |
140624 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
24 ms |
153084 KB |
Output is correct |
19 |
Correct |
20 ms |
150876 KB |
Output is correct |
20 |
Correct |
22 ms |
153064 KB |
Output is correct |
21 |
Correct |
23 ms |
150892 KB |
Output is correct |
22 |
Correct |
22 ms |
153064 KB |
Output is correct |
23 |
Correct |
22 ms |
148820 KB |
Output is correct |
24 |
Correct |
20 ms |
144732 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
21 ms |
154984 KB |
Output is correct |
28 |
Correct |
22 ms |
150964 KB |
Output is correct |
29 |
Correct |
20 ms |
150872 KB |
Output is correct |
30 |
Correct |
20 ms |
148828 KB |
Output is correct |
31 |
Correct |
20 ms |
146948 KB |
Output is correct |
32 |
Correct |
31 ms |
148884 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
22 ms |
152924 KB |
Output is correct |
36 |
Correct |
21 ms |
150876 KB |
Output is correct |
37 |
Correct |
20 ms |
146944 KB |
Output is correct |
38 |
Correct |
20 ms |
146772 KB |
Output is correct |
39 |
Correct |
22 ms |
151336 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
30 ms |
138580 KB |
Output is correct |
43 |
Correct |
93 ms |
270204 KB |
Output is correct |
44 |
Correct |
77 ms |
270544 KB |
Output is correct |
45 |
Correct |
76 ms |
270008 KB |
Output is correct |
46 |
Correct |
74 ms |
270008 KB |
Output is correct |
47 |
Correct |
76 ms |
270164 KB |
Output is correct |
48 |
Correct |
85 ms |
270160 KB |
Output is correct |
49 |
Correct |
75 ms |
270016 KB |
Output is correct |
50 |
Correct |
73 ms |
270164 KB |
Output is correct |
51 |
Correct |
74 ms |
270160 KB |
Output is correct |
52 |
Correct |
74 ms |
270160 KB |
Output is correct |
53 |
Correct |
38 ms |
206492 KB |
Output is correct |
54 |
Correct |
59 ms |
228964 KB |
Output is correct |
55 |
Correct |
59 ms |
247496 KB |
Output is correct |
56 |
Correct |
1 ms |
512 KB |
Output is correct |
57 |
Correct |
0 ms |
344 KB |
Output is correct |
58 |
Correct |
177 ms |
218964 KB |
Output is correct |
59 |
Correct |
171 ms |
233252 KB |
Output is correct |
60 |
Correct |
183 ms |
230992 KB |
Output is correct |
61 |
Correct |
181 ms |
225032 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
81 ms |
265928 KB |
Output is correct |
64 |
Correct |
105 ms |
261928 KB |
Output is correct |
65 |
Correct |
135 ms |
243396 KB |
Output is correct |
66 |
Correct |
170 ms |
226896 KB |
Output is correct |
67 |
Correct |
164 ms |
212560 KB |
Output is correct |
68 |
Correct |
173 ms |
226896 KB |
Output is correct |
69 |
Correct |
175 ms |
226980 KB |
Output is correct |
70 |
Correct |
182 ms |
227136 KB |
Output is correct |
71 |
Correct |
166 ms |
237268 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |