#include <bits/stdc++.h>
using namespace std;
const int N=404;
char c;
int n,Cr,Cg,Cy,t,id,A,B,x,y;
int a[N];
int dp[N][N][4],dp_[N][N][4];
int R[N],G[N],Y[N];
vector < int > v[4];
main () {
cin>>n;
for (int i=1; i<=n; i++) {
cin>>c;
R[i]=R[i-1]+(c=='R');
G[i]=G[i-1]+(c=='G');
Y[i]=Y[i-1]+(c=='Y');
if (c=='R') a[i]=0;
else
if (c=='G') a[i]=1;
else
if (c=='Y') a[i]=2;
v[a[i]].push_back(i);
}
for (Cr=0; Cr<=v[0].size(); Cr++)
for (Cg=0; Cg<=v[1].size(); Cg++)
for (t=0; t<3; t++)
dp_[Cr][Cg][t]=1e9;
dp_[0][0][0]=0;
dp_[0][0][1]=0;
dp_[0][0][2]=0;
for (int i=1; i<=n; i++) {
for (Cr=0; Cr<=i && Cr<=v[0].size(); Cr++)
for (Cg=0; Cg+Cr<=i && Cg<=v[1].size(); Cg++)
for (t=0; t<3; t++) {
dp[Cr][Cg][t]=1e9;
}
for (Cr=0; Cr<=i && Cr<=v[0].size(); Cr++)
for (Cg=0; Cg+Cr<=i && Cg<=v[1].size(); Cg++) {
Cy=i-Cr-Cg;
if (Cy>v[2].size()) continue;
if (Cr) {
id=v[0][Cr-1];
A=max(0,Cg-G[id]),B=max(0,Cy-Y[id]);
dp[Cr][Cg][0]= min(dp_[Cr-1][Cg][1],dp_[Cr-1][Cg][2]) + A + B;
}
if (Cg) {
id=v[1][Cg-1];
A=max(0,Cr-R[id]),B=max(0,Cy-Y[id]);
dp[Cr][Cg][1]= min(dp_[Cr][Cg-1][0],dp_[Cr][Cg-1][2]) + A + B;
}
if (Cy) {
id=v[2][Cy-1];
A=max(0,Cr-R[id]),B=max(0,Cg-G[id]);
dp[Cr][Cg][2]= min(dp_[Cr][Cg][0],dp_[Cr][Cg][1]) + A + B;
}
}
for (Cr=0; Cr<=i && Cr<=v[0].size(); Cr++)
for (Cg=0; Cg+Cr<=i && Cg<=v[1].size(); Cg++)
for (t=0; t<=2; t++)
dp_[Cr][Cg][t]=dp[Cr][Cg][t];
}
int ans=1e9;
for (t=0; t<3; ++t) {
ans=min(ans,dp_[v[0].size()][v[1].size()][t]);
}
if (ans==1e9) ans=-1;
cout<<ans<<"\n";
}
Compilation message
joi2019_ho_t3.cpp:14:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
14 | main () {
| ^
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:31:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (Cr=0; Cr<=v[0].size(); Cr++)
| ~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:32:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (Cg=0; Cg<=v[1].size(); Cg++)
| ~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:42:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (Cr=0; Cr<=i && Cr<=v[0].size(); Cr++)
| ~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:43:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (Cg=0; Cg+Cr<=i && Cg<=v[1].size(); Cg++)
| ~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:48:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (Cr=0; Cr<=i && Cr<=v[0].size(); Cr++)
| ~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:49:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (Cg=0; Cg+Cr<=i && Cg<=v[1].size(); Cg++) {
| ~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:51:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | if (Cy>v[2].size()) continue;
| ~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:72:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (Cr=0; Cr<=i && Cr<=v[0].size(); Cr++)
| ~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:73:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (Cg=0; Cg+Cr<=i && Cg<=v[1].size(); Cg++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
620 KB |
Output is correct |
26 |
Correct |
1 ms |
620 KB |
Output is correct |
27 |
Correct |
1 ms |
620 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
53 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
80 ms |
616 KB |
Output is correct |
34 |
Correct |
1 ms |
492 KB |
Output is correct |
35 |
Correct |
1 ms |
492 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
492 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
1 ms |
492 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
45 ms |
2924 KB |
Output is correct |
3 |
Correct |
43 ms |
2924 KB |
Output is correct |
4 |
Correct |
44 ms |
2924 KB |
Output is correct |
5 |
Correct |
45 ms |
2924 KB |
Output is correct |
6 |
Correct |
44 ms |
2924 KB |
Output is correct |
7 |
Correct |
44 ms |
2924 KB |
Output is correct |
8 |
Correct |
46 ms |
2924 KB |
Output is correct |
9 |
Correct |
53 ms |
2924 KB |
Output is correct |
10 |
Correct |
44 ms |
2924 KB |
Output is correct |
11 |
Correct |
44 ms |
3052 KB |
Output is correct |
12 |
Correct |
7 ms |
1516 KB |
Output is correct |
13 |
Correct |
16 ms |
2028 KB |
Output is correct |
14 |
Correct |
29 ms |
2412 KB |
Output is correct |
15 |
Correct |
48 ms |
2940 KB |
Output is correct |
16 |
Correct |
45 ms |
2924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
620 KB |
Output is correct |
26 |
Correct |
1 ms |
620 KB |
Output is correct |
27 |
Correct |
1 ms |
620 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
53 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
80 ms |
616 KB |
Output is correct |
34 |
Correct |
1 ms |
492 KB |
Output is correct |
35 |
Correct |
1 ms |
492 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
492 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
1 ms |
492 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
1 ms |
364 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
45 ms |
2924 KB |
Output is correct |
44 |
Correct |
43 ms |
2924 KB |
Output is correct |
45 |
Correct |
44 ms |
2924 KB |
Output is correct |
46 |
Correct |
45 ms |
2924 KB |
Output is correct |
47 |
Correct |
44 ms |
2924 KB |
Output is correct |
48 |
Correct |
44 ms |
2924 KB |
Output is correct |
49 |
Correct |
46 ms |
2924 KB |
Output is correct |
50 |
Correct |
53 ms |
2924 KB |
Output is correct |
51 |
Correct |
44 ms |
2924 KB |
Output is correct |
52 |
Correct |
44 ms |
3052 KB |
Output is correct |
53 |
Correct |
7 ms |
1516 KB |
Output is correct |
54 |
Correct |
16 ms |
2028 KB |
Output is correct |
55 |
Correct |
29 ms |
2412 KB |
Output is correct |
56 |
Correct |
48 ms |
2940 KB |
Output is correct |
57 |
Correct |
45 ms |
2924 KB |
Output is correct |
58 |
Correct |
46 ms |
1772 KB |
Output is correct |
59 |
Correct |
50 ms |
2156 KB |
Output is correct |
60 |
Correct |
48 ms |
2028 KB |
Output is correct |
61 |
Correct |
47 ms |
2028 KB |
Output is correct |
62 |
Correct |
45 ms |
3052 KB |
Output is correct |
63 |
Correct |
48 ms |
2796 KB |
Output is correct |
64 |
Correct |
50 ms |
2668 KB |
Output is correct |
65 |
Correct |
52 ms |
2412 KB |
Output is correct |
66 |
Correct |
51 ms |
2028 KB |
Output is correct |
67 |
Correct |
47 ms |
1900 KB |
Output is correct |
68 |
Correct |
48 ms |
2028 KB |
Output is correct |
69 |
Correct |
48 ms |
2028 KB |
Output is correct |
70 |
Correct |
48 ms |
2048 KB |
Output is correct |
71 |
Correct |
46 ms |
2028 KB |
Output is correct |
72 |
Correct |
22 ms |
2924 KB |
Output is correct |
73 |
Correct |
3 ms |
364 KB |
Output is correct |