#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
string inps;
int ar[510];
int dp[410][410][410][3];
vector<int>e[3];
int difc[510][3];
void upd(int A,int B,int C,int tl,int a,int b,int c,int tll){
if(A>e[0].size()){return;}
if(B>e[1].size()){return;}
if(C>e[2].size()){return;}
int cal=dp[a][b][c][tll];
int i=A+B+C;
cal-=i;
int pl;
int us=0;
if(tl==0){pl=e[0][A-1];us=max(B-difc[pl][1],0ll)+max(C-difc[pl][2],0ll); }
if(tl==1){pl=e[1][B-1];us=max(A-difc[pl][0],0ll)+max(C-difc[pl][2],0ll); }
if(tl==2){pl=e[2][C-1];us=max(B-difc[pl][1],0ll)+max(A-difc[pl][0],0ll); }
cal+=pl;
cal+=us;
dp[A][B][C][tl]=min(dp[A][B][C][tl],cal);
}
signed main(){
cin>>n>>inps;
for(int i=0;i<n;i++){
if(inps[i]=='R'){
ar[i+1]=0;
}
if(inps[i]=='G'){
ar[i+1]=1;
}
if(inps[i]=='Y'){
ar[i+1]=2;
}
}
for(int i=1;i<=n;i++){
e[ar[i]].push_back(i);
}
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
if(ar[j]!=ar[i]){
difc[i][ar[j]]++;
}
}
}
for(int a=0;a<=e[0].size();a++){
for(int b=0;b<=e[1].size();b++){
for(int c=0;c<=e[2].size();c++){
dp[a][b][c][0]=1e9;
dp[a][b][c][1]=1e9;
dp[a][b][c][2]=1e9;
}
}
}
if(e[0].size()){
dp[1][0][0][0]=e[0][0]-1;
}
if(e[1].size()){
dp[0][1][0][1]=e[1][0]-1;
}
if(e[2].size()){
dp[0][0][1][2]=e[2][0]-1;
}
for(int a=0;a<=e[0].size();a++){
for(int b=0;b<=e[1].size();b++){
for(int c=0;c<=e[2].size();c++){
upd(a,b,c+1,2,a,b,c,0);
upd(a,b+1,c,1,a,b,c,0);
upd(a+1,b,c,0,a,b,c,1);
upd(a,b,c+1,2,a,b,c,1);
upd(a+1,b,c,0,a,b,c,2);
upd(a,b+1,c,1,a,b,c,2);
}
}
}
int ans=min({dp[e[0].size()][e[1].size()][e[2].size()][0],dp[e[0].size()][e[1].size()][e[2].size()][1],dp[e[0].size()][e[1].size()][e[2].size()][2]});
if(ans==1e9){ans=-1;}
cout<<ans<<endl;
int a;int b;int c;
return 0;
while(cin>>a>>b>>c){
cout<<dp[a][b][c][0]<<" "<<dp[a][b][c][1]<<" "<<dp[a][b][c][2]<<endl;
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int, long long int, long long int, long long int)':
joi2019_ho_t3.cpp:14:5: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | if(A>e[0].size()){return;}
| ~^~~~~~~~~~~~
joi2019_ho_t3.cpp:15:5: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | if(B>e[1].size()){return;}
| ~^~~~~~~~~~~~
joi2019_ho_t3.cpp:16:5: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | if(C>e[2].size()){return;}
| ~^~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:54:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int a=0;a<=e[0].size();a++){
| ~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:55:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int b=0;b<=e[1].size();b++){
| ~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:56:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int c=0;c<=e[2].size();c++){
| ~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:73:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int a=0;a<=e[0].size();a++){
| ~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:74:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int b=0;b<=e[1].size();b++){
| ~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:75:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int c=0;c<=e[2].size();c++){
| ~^~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int, long long int, long long int, long long int)':
joi2019_ho_t3.cpp:25:4: warning: 'pl' may be used uninitialized in this function [-Wmaybe-uninitialized]
25 | cal+=pl;
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
444 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
444 KB |
Output is correct |
12 |
Correct |
1 ms |
444 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
444 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
444 KB |
Output is correct |
12 |
Correct |
1 ms |
444 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
3 ms |
2388 KB |
Output is correct |
19 |
Correct |
2 ms |
2004 KB |
Output is correct |
20 |
Correct |
3 ms |
2364 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
1748 KB |
Output is correct |
23 |
Correct |
2 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3156 KB |
Output is correct |
28 |
Correct |
2 ms |
2388 KB |
Output is correct |
29 |
Correct |
3 ms |
2388 KB |
Output is correct |
30 |
Correct |
2 ms |
2388 KB |
Output is correct |
31 |
Correct |
2 ms |
2004 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
2 ms |
3900 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
1976 KB |
Output is correct |
37 |
Correct |
2 ms |
1748 KB |
Output is correct |
38 |
Correct |
2 ms |
2260 KB |
Output is correct |
39 |
Correct |
2 ms |
2388 KB |
Output is correct |
40 |
Correct |
1 ms |
320 KB |
Output is correct |
41 |
Correct |
2 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
68 ms |
163996 KB |
Output is correct |
3 |
Correct |
61 ms |
163288 KB |
Output is correct |
4 |
Correct |
67 ms |
164028 KB |
Output is correct |
5 |
Correct |
69 ms |
163976 KB |
Output is correct |
6 |
Correct |
65 ms |
164052 KB |
Output is correct |
7 |
Correct |
62 ms |
163272 KB |
Output is correct |
8 |
Correct |
61 ms |
163148 KB |
Output is correct |
9 |
Correct |
68 ms |
162360 KB |
Output is correct |
10 |
Correct |
75 ms |
164144 KB |
Output is correct |
11 |
Correct |
68 ms |
164016 KB |
Output is correct |
12 |
Correct |
19 ms |
44356 KB |
Output is correct |
13 |
Correct |
31 ms |
77648 KB |
Output is correct |
14 |
Correct |
48 ms |
112152 KB |
Output is correct |
15 |
Correct |
65 ms |
164044 KB |
Output is correct |
16 |
Correct |
73 ms |
164008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
444 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
444 KB |
Output is correct |
12 |
Correct |
1 ms |
444 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
3 ms |
2388 KB |
Output is correct |
19 |
Correct |
2 ms |
2004 KB |
Output is correct |
20 |
Correct |
3 ms |
2364 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
1748 KB |
Output is correct |
23 |
Correct |
2 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3156 KB |
Output is correct |
28 |
Correct |
2 ms |
2388 KB |
Output is correct |
29 |
Correct |
3 ms |
2388 KB |
Output is correct |
30 |
Correct |
2 ms |
2388 KB |
Output is correct |
31 |
Correct |
2 ms |
2004 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
2 ms |
3900 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
1976 KB |
Output is correct |
37 |
Correct |
2 ms |
1748 KB |
Output is correct |
38 |
Correct |
2 ms |
2260 KB |
Output is correct |
39 |
Correct |
2 ms |
2388 KB |
Output is correct |
40 |
Correct |
1 ms |
320 KB |
Output is correct |
41 |
Correct |
2 ms |
2260 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
68 ms |
163996 KB |
Output is correct |
44 |
Correct |
61 ms |
163288 KB |
Output is correct |
45 |
Correct |
67 ms |
164028 KB |
Output is correct |
46 |
Correct |
69 ms |
163976 KB |
Output is correct |
47 |
Correct |
65 ms |
164052 KB |
Output is correct |
48 |
Correct |
62 ms |
163272 KB |
Output is correct |
49 |
Correct |
61 ms |
163148 KB |
Output is correct |
50 |
Correct |
68 ms |
162360 KB |
Output is correct |
51 |
Correct |
75 ms |
164144 KB |
Output is correct |
52 |
Correct |
68 ms |
164016 KB |
Output is correct |
53 |
Correct |
19 ms |
44356 KB |
Output is correct |
54 |
Correct |
31 ms |
77648 KB |
Output is correct |
55 |
Correct |
48 ms |
112152 KB |
Output is correct |
56 |
Correct |
65 ms |
164044 KB |
Output is correct |
57 |
Correct |
73 ms |
164008 KB |
Output is correct |
58 |
Correct |
157 ms |
120688 KB |
Output is correct |
59 |
Correct |
174 ms |
137856 KB |
Output is correct |
60 |
Correct |
176 ms |
129844 KB |
Output is correct |
61 |
Correct |
171 ms |
128108 KB |
Output is correct |
62 |
Correct |
76 ms |
163528 KB |
Output is correct |
63 |
Correct |
86 ms |
164840 KB |
Output is correct |
64 |
Correct |
137 ms |
164544 KB |
Output is correct |
65 |
Correct |
141 ms |
158828 KB |
Output is correct |
66 |
Correct |
168 ms |
128328 KB |
Output is correct |
67 |
Correct |
156 ms |
125616 KB |
Output is correct |
68 |
Correct |
166 ms |
131236 KB |
Output is correct |
69 |
Correct |
162 ms |
128912 KB |
Output is correct |
70 |
Correct |
168 ms |
134872 KB |
Output is correct |
71 |
Correct |
177 ms |
126316 KB |
Output is correct |
72 |
Correct |
50 ms |
49572 KB |
Output is correct |
73 |
Correct |
10 ms |
5460 KB |
Output is correct |