#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define nn "\n"
#define x_x ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define intt int _; cin >> _; while (_--)
#define emp push_back
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define ld long double
#define A first
#define B second
//#define int long long
typedef long long ll;
const ld eps = 1e-27;
// diff between decimals 0.000000001 mt19937 mt(time(nullptr));
int dp[204][204][204][3], n;
int nm(int i, int x, int a, int b, int c, int ar[][3], vector<int>pos[]) {
bool f=0;
if(x==0&&a==ar[n-1][0]) f=1;
if (x==1&&ar[n-1][1]==b) f=1;
if (x==2&&ar[n-1][2]==c) f=1;
if (f) return 1000000;
int j;
switch(x) {
case 0:
j=pos[x][a]; break;
case 1:
j=pos[x][b]; break;
default:
j=pos[x][c]; break;
}
int rn=max(0, a-ar[j][0]), gn=max(0, b-ar[j][1]), yn=max(0, c-ar[j][2]);
return rn+gn+yn+(j-i)-1;
}
int main() {
x_x
string s; cin>>n>>s; memset(dp, -1, sizeof(dp));
int ar[n][3]; vector<int>pos[3];
for (int i=0; i<n; i++) {
ar[i][0]=ar[i][1]=ar[i][2]=0;
switch(s[i]) {
case 'R':
ar[i][0]++, pos[0].emp(i);
break;
case 'G':
ar[i][1]++, pos[1].emp(i);
break;
case 'Y':
ar[i][2]++, pos[2].emp(i);
break;
default:
break;
}
for(int j=0; j<3&&i; j++) ar[i][j]+=ar[i-1][j];
}
for (int ind=0; ind<n; ind++) {
for (int a=0; a<=min(ar[n-1][0],ind); a++) {
for (int b=0; b<=min(ind,ar[n-1][1])&&(a+b<=ind); b++) {
int c=ind-(a+b); if(c>ar[n-1][2]) continue;
int mx=max({a,b,c}), rst=ind-mx; --mx;
if (mx>rst)
{/*dp[a+1][b][c][0]=dp[a][b+1][c][1]=dp[a][b][c+1][2]=1000000;*/ continue;}
int m[3];
m[0]=nm(ind-1, 0, a, b, c, ar, pos);
m[1]=nm(ind-1, 1, a, b, c, ar, pos), m[2]=nm(ind-1, 2, a, b, c, ar, pos);
if (!ind) {
dp[1][0][0][0]=m[0]; dp[0][1][0][1]=m[1]; dp[0][0][1][2]=m[2];
}
else {
for (int k=0; k<3; k++) {
if (k==0&&!a)continue; if (k==1&&!b)continue; if (k==2&&!c)continue;
for (int r=0; r<3; r++) {
if (r!=k) {
int x=a+(r==0), y=b+(r==1), z=c+(r==2);
if (x>ar[n-1][0]||y>ar[n-1][1]||z>ar[n-1][2]) continue;
if (dp[x][y][z][r]==-1) dp[x][y][z][r]=1000000;
if (dp[a][b][c][k]==-1) continue;
dp[x][y][z][r]=min(dp[x][y][z][r], m[r]+dp[a][b][c][k]);
}
}
}
}
}
}
}
int a=ar[n-1][0], b=ar[n-1][1], c=ar[n-1][2];
int ans=INT_MAX;
for (int r=0; r<3; r++) if(dp[a][b][c][r]>-1)ans=min(ans, dp[a][b][c][r]);
cout<<(ans>=1000000?-1:ans);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:80:21: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
80 | if (k==0&&!a)continue; if (k==1&&!b)continue; if (k==2&&!c)continue;
| ^~
joi2019_ho_t3.cpp:80:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
80 | if (k==0&&!a)continue; if (k==1&&!b)continue; if (k==2&&!c)continue;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
100068 KB |
Output is correct |
2 |
Correct |
16 ms |
99960 KB |
Output is correct |
3 |
Correct |
14 ms |
99932 KB |
Output is correct |
4 |
Correct |
12 ms |
100080 KB |
Output is correct |
5 |
Correct |
13 ms |
99932 KB |
Output is correct |
6 |
Correct |
13 ms |
99928 KB |
Output is correct |
7 |
Correct |
14 ms |
99928 KB |
Output is correct |
8 |
Correct |
13 ms |
99928 KB |
Output is correct |
9 |
Correct |
12 ms |
99928 KB |
Output is correct |
10 |
Correct |
13 ms |
99932 KB |
Output is correct |
11 |
Correct |
13 ms |
99932 KB |
Output is correct |
12 |
Correct |
12 ms |
99932 KB |
Output is correct |
13 |
Correct |
13 ms |
99984 KB |
Output is correct |
14 |
Correct |
12 ms |
99928 KB |
Output is correct |
15 |
Correct |
12 ms |
99932 KB |
Output is correct |
16 |
Correct |
12 ms |
100184 KB |
Output is correct |
17 |
Correct |
12 ms |
99928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
100068 KB |
Output is correct |
2 |
Correct |
16 ms |
99960 KB |
Output is correct |
3 |
Correct |
14 ms |
99932 KB |
Output is correct |
4 |
Correct |
12 ms |
100080 KB |
Output is correct |
5 |
Correct |
13 ms |
99932 KB |
Output is correct |
6 |
Correct |
13 ms |
99928 KB |
Output is correct |
7 |
Correct |
14 ms |
99928 KB |
Output is correct |
8 |
Correct |
13 ms |
99928 KB |
Output is correct |
9 |
Correct |
12 ms |
99928 KB |
Output is correct |
10 |
Correct |
13 ms |
99932 KB |
Output is correct |
11 |
Correct |
13 ms |
99932 KB |
Output is correct |
12 |
Correct |
12 ms |
99932 KB |
Output is correct |
13 |
Correct |
13 ms |
99984 KB |
Output is correct |
14 |
Correct |
12 ms |
99928 KB |
Output is correct |
15 |
Correct |
12 ms |
99932 KB |
Output is correct |
16 |
Correct |
12 ms |
100184 KB |
Output is correct |
17 |
Correct |
12 ms |
99928 KB |
Output is correct |
18 |
Correct |
14 ms |
99932 KB |
Output is correct |
19 |
Correct |
13 ms |
100260 KB |
Output is correct |
20 |
Correct |
15 ms |
100092 KB |
Output is correct |
21 |
Correct |
13 ms |
99984 KB |
Output is correct |
22 |
Correct |
13 ms |
100088 KB |
Output is correct |
23 |
Correct |
13 ms |
99932 KB |
Output is correct |
24 |
Correct |
13 ms |
100140 KB |
Output is correct |
25 |
Correct |
13 ms |
99932 KB |
Output is correct |
26 |
Correct |
12 ms |
99996 KB |
Output is correct |
27 |
Correct |
13 ms |
99932 KB |
Output is correct |
28 |
Correct |
13 ms |
100128 KB |
Output is correct |
29 |
Correct |
13 ms |
100020 KB |
Output is correct |
30 |
Correct |
13 ms |
100072 KB |
Output is correct |
31 |
Correct |
13 ms |
100144 KB |
Output is correct |
32 |
Correct |
13 ms |
99968 KB |
Output is correct |
33 |
Correct |
13 ms |
100048 KB |
Output is correct |
34 |
Correct |
13 ms |
99932 KB |
Output is correct |
35 |
Correct |
13 ms |
99928 KB |
Output is correct |
36 |
Correct |
13 ms |
100116 KB |
Output is correct |
37 |
Correct |
13 ms |
100076 KB |
Output is correct |
38 |
Correct |
13 ms |
99928 KB |
Output is correct |
39 |
Correct |
13 ms |
99928 KB |
Output is correct |
40 |
Correct |
13 ms |
99932 KB |
Output is correct |
41 |
Correct |
13 ms |
99928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
99928 KB |
Output is correct |
2 |
Correct |
20 ms |
99928 KB |
Output is correct |
3 |
Correct |
21 ms |
100080 KB |
Output is correct |
4 |
Correct |
21 ms |
99932 KB |
Output is correct |
5 |
Correct |
21 ms |
99932 KB |
Output is correct |
6 |
Correct |
21 ms |
99932 KB |
Output is correct |
7 |
Correct |
21 ms |
99932 KB |
Output is correct |
8 |
Correct |
21 ms |
100084 KB |
Output is correct |
9 |
Correct |
24 ms |
99928 KB |
Output is correct |
10 |
Correct |
21 ms |
99928 KB |
Output is correct |
11 |
Correct |
20 ms |
99928 KB |
Output is correct |
12 |
Correct |
14 ms |
99932 KB |
Output is correct |
13 |
Correct |
16 ms |
99932 KB |
Output is correct |
14 |
Correct |
17 ms |
100144 KB |
Output is correct |
15 |
Correct |
21 ms |
99932 KB |
Output is correct |
16 |
Correct |
21 ms |
99932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
100068 KB |
Output is correct |
2 |
Correct |
16 ms |
99960 KB |
Output is correct |
3 |
Correct |
14 ms |
99932 KB |
Output is correct |
4 |
Correct |
12 ms |
100080 KB |
Output is correct |
5 |
Correct |
13 ms |
99932 KB |
Output is correct |
6 |
Correct |
13 ms |
99928 KB |
Output is correct |
7 |
Correct |
14 ms |
99928 KB |
Output is correct |
8 |
Correct |
13 ms |
99928 KB |
Output is correct |
9 |
Correct |
12 ms |
99928 KB |
Output is correct |
10 |
Correct |
13 ms |
99932 KB |
Output is correct |
11 |
Correct |
13 ms |
99932 KB |
Output is correct |
12 |
Correct |
12 ms |
99932 KB |
Output is correct |
13 |
Correct |
13 ms |
99984 KB |
Output is correct |
14 |
Correct |
12 ms |
99928 KB |
Output is correct |
15 |
Correct |
12 ms |
99932 KB |
Output is correct |
16 |
Correct |
12 ms |
100184 KB |
Output is correct |
17 |
Correct |
12 ms |
99928 KB |
Output is correct |
18 |
Correct |
14 ms |
99932 KB |
Output is correct |
19 |
Correct |
13 ms |
100260 KB |
Output is correct |
20 |
Correct |
15 ms |
100092 KB |
Output is correct |
21 |
Correct |
13 ms |
99984 KB |
Output is correct |
22 |
Correct |
13 ms |
100088 KB |
Output is correct |
23 |
Correct |
13 ms |
99932 KB |
Output is correct |
24 |
Correct |
13 ms |
100140 KB |
Output is correct |
25 |
Correct |
13 ms |
99932 KB |
Output is correct |
26 |
Correct |
12 ms |
99996 KB |
Output is correct |
27 |
Correct |
13 ms |
99932 KB |
Output is correct |
28 |
Correct |
13 ms |
100128 KB |
Output is correct |
29 |
Correct |
13 ms |
100020 KB |
Output is correct |
30 |
Correct |
13 ms |
100072 KB |
Output is correct |
31 |
Correct |
13 ms |
100144 KB |
Output is correct |
32 |
Correct |
13 ms |
99968 KB |
Output is correct |
33 |
Correct |
13 ms |
100048 KB |
Output is correct |
34 |
Correct |
13 ms |
99932 KB |
Output is correct |
35 |
Correct |
13 ms |
99928 KB |
Output is correct |
36 |
Correct |
13 ms |
100116 KB |
Output is correct |
37 |
Correct |
13 ms |
100076 KB |
Output is correct |
38 |
Correct |
13 ms |
99928 KB |
Output is correct |
39 |
Correct |
13 ms |
99928 KB |
Output is correct |
40 |
Correct |
13 ms |
99932 KB |
Output is correct |
41 |
Correct |
13 ms |
99928 KB |
Output is correct |
42 |
Correct |
13 ms |
99928 KB |
Output is correct |
43 |
Correct |
20 ms |
99928 KB |
Output is correct |
44 |
Correct |
21 ms |
100080 KB |
Output is correct |
45 |
Correct |
21 ms |
99932 KB |
Output is correct |
46 |
Correct |
21 ms |
99932 KB |
Output is correct |
47 |
Correct |
21 ms |
99932 KB |
Output is correct |
48 |
Correct |
21 ms |
99932 KB |
Output is correct |
49 |
Correct |
21 ms |
100084 KB |
Output is correct |
50 |
Correct |
24 ms |
99928 KB |
Output is correct |
51 |
Correct |
21 ms |
99928 KB |
Output is correct |
52 |
Correct |
20 ms |
99928 KB |
Output is correct |
53 |
Correct |
14 ms |
99932 KB |
Output is correct |
54 |
Correct |
16 ms |
99932 KB |
Output is correct |
55 |
Correct |
17 ms |
100144 KB |
Output is correct |
56 |
Correct |
21 ms |
99932 KB |
Output is correct |
57 |
Correct |
21 ms |
99932 KB |
Output is correct |
58 |
Correct |
77 ms |
99928 KB |
Output is correct |
59 |
Correct |
80 ms |
99932 KB |
Output is correct |
60 |
Correct |
76 ms |
100120 KB |
Output is correct |
61 |
Correct |
81 ms |
100132 KB |
Output is correct |
62 |
Runtime error |
101 ms |
202516 KB |
Execution killed with signal 11 |
63 |
Halted |
0 ms |
0 KB |
- |