#include <bits/stdc++.h>
using namespace std;
long long dp[3][301][301][301];
vector<int>col[3];
long long pref[3][401];
long long solve(int la,int r,int g,int b){
if(r+1==col[0].size()&&g+1==col[1].size()&&b+1==col[2].size()){
return 0;
}
if(dp[la][r][g][b]!=-1)return dp[la][r][g][b];
long long c1 = 1e18;
for(int ne = 0;ne<3;ne++){
if(ne==la)continue;
if((ne==0&&r+1==col[0].size())||(ne==1&&g+1==col[1].size())||(ne==2&&b+1==col[2].size()))continue;
int p1 = 0 , p2 = 0 , p3 = 0 , dif;
if(ne==0)p1 = col[ne][r+1];
if(ne==1)p1 = col[ne][g+1];
if(ne==2)p1 = col[ne][b+1];
if(la==0)p2 = col[la][r];
if(la==1)p2 = col[la][g];
if(la==2)p2 = col[la][b];
if(ne!=0&&la!=0){p3 = col[0][r];dif = 0;}
if(ne!=1&&la!=1){p3 = col[1][g];dif = 1;}
if(ne!=2&&la!=2){p3 = col[2][b];dif = 2;}
long long ans = max(0LL,pref[la][p1]-pref[la][p2])+max(0LL,pref[dif][p1]-pref[dif][p3]);
if(ne==0)c1 = min(c1,solve(ne,r+1,g,b)+ans);
if(ne==1)c1 = min(c1,solve(ne,r,g+1,b)+ans);
if(ne==2)c1 = min(c1,solve(ne,r,g,b+1)+ans);
}
return dp[la][r][g][b] =c1;
}
signed main(){
int len;cin>>len;
string s;cin>>s;
s = 'a'+s;
col[0].push_back(0);col[1].push_back(0);col[2].push_back(0);
memset(pref,0,sizeof pref);
for(int i = 1;i<s.size();i++){
pref[0][i]+=pref[0][i-1];
pref[1][i]+=pref[1][i-1];
pref[2][i]+=pref[2][i-1];
if(s[i]=='R'){col[0].push_back(i);pref[0][i]++;}
if(s[i]=='G'){col[1].push_back(i);pref[1][i]++;}
if(s[i]=='Y'){col[2].push_back(i);pref[2][i]++;}
}
memset(dp,-1,sizeof dp);
cout<<(min({solve(0,0,0,0),solve(1,0,0,0),solve(2,0,0,0)})==1e18?-1:min({solve(0,0,0,0),solve(1,0,0,0),solve(2,0,0,0)}));
}
Compilation message
joi2019_ho_t3.cpp: In function 'long long int solve(int, int, int, int)':
joi2019_ho_t3.cpp:8:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | if(r+1==col[0].size()&&g+1==col[1].size()&&b+1==col[2].size()){
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:8:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | if(r+1==col[0].size()&&g+1==col[1].size()&&b+1==col[2].size()){
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:8:51: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | if(r+1==col[0].size()&&g+1==col[1].size()&&b+1==col[2].size()){
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:15:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | if((ne==0&&r+1==col[0].size())||(ne==1&&g+1==col[1].size())||(ne==2&&b+1==col[2].size()))continue;
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:15:52: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | if((ne==0&&r+1==col[0].size())||(ne==1&&g+1==col[1].size())||(ne==2&&b+1==col[2].size()))continue;
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:15:81: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | if((ne==0&&r+1==col[0].size())||(ne==1&&g+1==col[1].size())||(ne==2&&b+1==col[2].size()))continue;
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:39:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 1;i<s.size();i++){
| ~^~~~~~~~~
joi2019_ho_t3.cpp: In function 'long long int solve(int, int, int, int)':
joi2019_ho_t3.cpp:26:94: warning: 'dif' may be used uninitialized in this function [-Wmaybe-uninitialized]
26 | long long ans = max(0LL,pref[la][p1]-pref[la][p2])+max(0LL,pref[dif][p1]-pref[dif][p3]);
| ~~~~~~~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
227 ms |
640688 KB |
Output is correct |
2 |
Correct |
230 ms |
640652 KB |
Output is correct |
3 |
Correct |
244 ms |
640656 KB |
Output is correct |
4 |
Correct |
228 ms |
640708 KB |
Output is correct |
5 |
Correct |
234 ms |
640716 KB |
Output is correct |
6 |
Correct |
231 ms |
640588 KB |
Output is correct |
7 |
Correct |
229 ms |
640636 KB |
Output is correct |
8 |
Correct |
231 ms |
640588 KB |
Output is correct |
9 |
Correct |
248 ms |
640612 KB |
Output is correct |
10 |
Correct |
229 ms |
640720 KB |
Output is correct |
11 |
Correct |
242 ms |
640612 KB |
Output is correct |
12 |
Correct |
226 ms |
640700 KB |
Output is correct |
13 |
Correct |
250 ms |
640652 KB |
Output is correct |
14 |
Correct |
230 ms |
640692 KB |
Output is correct |
15 |
Correct |
229 ms |
640708 KB |
Output is correct |
16 |
Correct |
242 ms |
640676 KB |
Output is correct |
17 |
Correct |
234 ms |
640752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
227 ms |
640688 KB |
Output is correct |
2 |
Correct |
230 ms |
640652 KB |
Output is correct |
3 |
Correct |
244 ms |
640656 KB |
Output is correct |
4 |
Correct |
228 ms |
640708 KB |
Output is correct |
5 |
Correct |
234 ms |
640716 KB |
Output is correct |
6 |
Correct |
231 ms |
640588 KB |
Output is correct |
7 |
Correct |
229 ms |
640636 KB |
Output is correct |
8 |
Correct |
231 ms |
640588 KB |
Output is correct |
9 |
Correct |
248 ms |
640612 KB |
Output is correct |
10 |
Correct |
229 ms |
640720 KB |
Output is correct |
11 |
Correct |
242 ms |
640612 KB |
Output is correct |
12 |
Correct |
226 ms |
640700 KB |
Output is correct |
13 |
Correct |
250 ms |
640652 KB |
Output is correct |
14 |
Correct |
230 ms |
640692 KB |
Output is correct |
15 |
Correct |
229 ms |
640708 KB |
Output is correct |
16 |
Correct |
242 ms |
640676 KB |
Output is correct |
17 |
Correct |
234 ms |
640752 KB |
Output is correct |
18 |
Correct |
228 ms |
640592 KB |
Output is correct |
19 |
Correct |
231 ms |
640716 KB |
Output is correct |
20 |
Correct |
228 ms |
640716 KB |
Output is correct |
21 |
Correct |
227 ms |
640764 KB |
Output is correct |
22 |
Correct |
240 ms |
640708 KB |
Output is correct |
23 |
Correct |
227 ms |
640608 KB |
Output is correct |
24 |
Correct |
234 ms |
640608 KB |
Output is correct |
25 |
Correct |
236 ms |
640592 KB |
Output is correct |
26 |
Correct |
226 ms |
640616 KB |
Output is correct |
27 |
Correct |
231 ms |
640736 KB |
Output is correct |
28 |
Correct |
230 ms |
640704 KB |
Output is correct |
29 |
Correct |
224 ms |
640664 KB |
Output is correct |
30 |
Correct |
239 ms |
640712 KB |
Output is correct |
31 |
Correct |
237 ms |
640768 KB |
Output is correct |
32 |
Correct |
228 ms |
640636 KB |
Output is correct |
33 |
Correct |
234 ms |
640644 KB |
Output is correct |
34 |
Correct |
231 ms |
640716 KB |
Output is correct |
35 |
Correct |
227 ms |
640656 KB |
Output is correct |
36 |
Correct |
228 ms |
640692 KB |
Output is correct |
37 |
Correct |
233 ms |
640700 KB |
Output is correct |
38 |
Correct |
225 ms |
640596 KB |
Output is correct |
39 |
Correct |
228 ms |
640800 KB |
Output is correct |
40 |
Correct |
233 ms |
640616 KB |
Output is correct |
41 |
Correct |
232 ms |
640608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
240 ms |
640684 KB |
Output is correct |
2 |
Correct |
234 ms |
640764 KB |
Output is correct |
3 |
Correct |
227 ms |
640688 KB |
Output is correct |
4 |
Correct |
243 ms |
640868 KB |
Output is correct |
5 |
Correct |
235 ms |
640812 KB |
Output is correct |
6 |
Correct |
235 ms |
640776 KB |
Output is correct |
7 |
Correct |
232 ms |
640684 KB |
Output is correct |
8 |
Correct |
228 ms |
640716 KB |
Output is correct |
9 |
Correct |
225 ms |
640764 KB |
Output is correct |
10 |
Correct |
229 ms |
640844 KB |
Output is correct |
11 |
Correct |
224 ms |
640720 KB |
Output is correct |
12 |
Correct |
227 ms |
640748 KB |
Output is correct |
13 |
Correct |
226 ms |
640692 KB |
Output is correct |
14 |
Correct |
224 ms |
640652 KB |
Output is correct |
15 |
Correct |
224 ms |
640700 KB |
Output is correct |
16 |
Correct |
257 ms |
640684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
227 ms |
640688 KB |
Output is correct |
2 |
Correct |
230 ms |
640652 KB |
Output is correct |
3 |
Correct |
244 ms |
640656 KB |
Output is correct |
4 |
Correct |
228 ms |
640708 KB |
Output is correct |
5 |
Correct |
234 ms |
640716 KB |
Output is correct |
6 |
Correct |
231 ms |
640588 KB |
Output is correct |
7 |
Correct |
229 ms |
640636 KB |
Output is correct |
8 |
Correct |
231 ms |
640588 KB |
Output is correct |
9 |
Correct |
248 ms |
640612 KB |
Output is correct |
10 |
Correct |
229 ms |
640720 KB |
Output is correct |
11 |
Correct |
242 ms |
640612 KB |
Output is correct |
12 |
Correct |
226 ms |
640700 KB |
Output is correct |
13 |
Correct |
250 ms |
640652 KB |
Output is correct |
14 |
Correct |
230 ms |
640692 KB |
Output is correct |
15 |
Correct |
229 ms |
640708 KB |
Output is correct |
16 |
Correct |
242 ms |
640676 KB |
Output is correct |
17 |
Correct |
234 ms |
640752 KB |
Output is correct |
18 |
Correct |
228 ms |
640592 KB |
Output is correct |
19 |
Correct |
231 ms |
640716 KB |
Output is correct |
20 |
Correct |
228 ms |
640716 KB |
Output is correct |
21 |
Correct |
227 ms |
640764 KB |
Output is correct |
22 |
Correct |
240 ms |
640708 KB |
Output is correct |
23 |
Correct |
227 ms |
640608 KB |
Output is correct |
24 |
Correct |
234 ms |
640608 KB |
Output is correct |
25 |
Correct |
236 ms |
640592 KB |
Output is correct |
26 |
Correct |
226 ms |
640616 KB |
Output is correct |
27 |
Correct |
231 ms |
640736 KB |
Output is correct |
28 |
Correct |
230 ms |
640704 KB |
Output is correct |
29 |
Correct |
224 ms |
640664 KB |
Output is correct |
30 |
Correct |
239 ms |
640712 KB |
Output is correct |
31 |
Correct |
237 ms |
640768 KB |
Output is correct |
32 |
Correct |
228 ms |
640636 KB |
Output is correct |
33 |
Correct |
234 ms |
640644 KB |
Output is correct |
34 |
Correct |
231 ms |
640716 KB |
Output is correct |
35 |
Correct |
227 ms |
640656 KB |
Output is correct |
36 |
Correct |
228 ms |
640692 KB |
Output is correct |
37 |
Correct |
233 ms |
640700 KB |
Output is correct |
38 |
Correct |
225 ms |
640596 KB |
Output is correct |
39 |
Correct |
228 ms |
640800 KB |
Output is correct |
40 |
Correct |
233 ms |
640616 KB |
Output is correct |
41 |
Correct |
232 ms |
640608 KB |
Output is correct |
42 |
Correct |
240 ms |
640684 KB |
Output is correct |
43 |
Correct |
234 ms |
640764 KB |
Output is correct |
44 |
Correct |
227 ms |
640688 KB |
Output is correct |
45 |
Correct |
243 ms |
640868 KB |
Output is correct |
46 |
Correct |
235 ms |
640812 KB |
Output is correct |
47 |
Correct |
235 ms |
640776 KB |
Output is correct |
48 |
Correct |
232 ms |
640684 KB |
Output is correct |
49 |
Correct |
228 ms |
640716 KB |
Output is correct |
50 |
Correct |
225 ms |
640764 KB |
Output is correct |
51 |
Correct |
229 ms |
640844 KB |
Output is correct |
52 |
Correct |
224 ms |
640720 KB |
Output is correct |
53 |
Correct |
227 ms |
640748 KB |
Output is correct |
54 |
Correct |
226 ms |
640692 KB |
Output is correct |
55 |
Correct |
224 ms |
640652 KB |
Output is correct |
56 |
Correct |
224 ms |
640700 KB |
Output is correct |
57 |
Correct |
257 ms |
640684 KB |
Output is correct |
58 |
Correct |
357 ms |
640780 KB |
Output is correct |
59 |
Correct |
351 ms |
640784 KB |
Output is correct |
60 |
Correct |
366 ms |
640784 KB |
Output is correct |
61 |
Correct |
370 ms |
640784 KB |
Output is correct |
62 |
Correct |
227 ms |
640680 KB |
Output is correct |
63 |
Correct |
226 ms |
640760 KB |
Output is correct |
64 |
Correct |
245 ms |
640724 KB |
Output is correct |
65 |
Correct |
282 ms |
640708 KB |
Output is correct |
66 |
Correct |
357 ms |
640700 KB |
Output is correct |
67 |
Correct |
357 ms |
640740 KB |
Output is correct |
68 |
Correct |
360 ms |
640680 KB |
Output is correct |
69 |
Correct |
362 ms |
640724 KB |
Output is correct |
70 |
Correct |
380 ms |
640780 KB |
Output is correct |
71 |
Correct |
382 ms |
640780 KB |
Output is correct |
72 |
Correct |
238 ms |
640632 KB |
Output is correct |
73 |
Correct |
226 ms |
640688 KB |
Output is correct |