#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];
}
int zero=0; for (int i=0; i<3; i++) zero+=(ar[n-1][i]==0);
if (zero>1) return cout<<0, 0;
int mx=max({ar[n-1][0], ar[n-1][1], ar[n-1][2]}), rst=n-mx; --mx;
if (mx>rst) return cout<<-1, 0;
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;
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:86:21: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
86 | if (k==0&&!a)continue; if (k==1&&!b)continue; if (k==2&&!c)continue;
| ^~
joi2019_ho_t3.cpp:86:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
86 | if (k==0&&!a)continue; if (k==1&&!b)continue; if (k==2&&!c)continue;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
100092 KB |
Output is correct |
2 |
Correct |
15 ms |
99932 KB |
Output is correct |
3 |
Correct |
13 ms |
99932 KB |
Output is correct |
4 |
Correct |
12 ms |
100012 KB |
Output is correct |
5 |
Correct |
12 ms |
99932 KB |
Output is correct |
6 |
Correct |
13 ms |
100092 KB |
Output is correct |
7 |
Correct |
12 ms |
99928 KB |
Output is correct |
8 |
Correct |
13 ms |
99928 KB |
Output is correct |
9 |
Correct |
13 ms |
99932 KB |
Output is correct |
10 |
Correct |
12 ms |
99932 KB |
Output is correct |
11 |
Correct |
15 ms |
99932 KB |
Output is correct |
12 |
Correct |
13 ms |
100108 KB |
Output is correct |
13 |
Correct |
12 ms |
99932 KB |
Output is correct |
14 |
Correct |
13 ms |
99932 KB |
Output is correct |
15 |
Correct |
13 ms |
99932 KB |
Output is correct |
16 |
Correct |
12 ms |
99996 KB |
Output is correct |
17 |
Correct |
13 ms |
99932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
100092 KB |
Output is correct |
2 |
Correct |
15 ms |
99932 KB |
Output is correct |
3 |
Correct |
13 ms |
99932 KB |
Output is correct |
4 |
Correct |
12 ms |
100012 KB |
Output is correct |
5 |
Correct |
12 ms |
99932 KB |
Output is correct |
6 |
Correct |
13 ms |
100092 KB |
Output is correct |
7 |
Correct |
12 ms |
99928 KB |
Output is correct |
8 |
Correct |
13 ms |
99928 KB |
Output is correct |
9 |
Correct |
13 ms |
99932 KB |
Output is correct |
10 |
Correct |
12 ms |
99932 KB |
Output is correct |
11 |
Correct |
15 ms |
99932 KB |
Output is correct |
12 |
Correct |
13 ms |
100108 KB |
Output is correct |
13 |
Correct |
12 ms |
99932 KB |
Output is correct |
14 |
Correct |
13 ms |
99932 KB |
Output is correct |
15 |
Correct |
13 ms |
99932 KB |
Output is correct |
16 |
Correct |
12 ms |
99996 KB |
Output is correct |
17 |
Correct |
13 ms |
99932 KB |
Output is correct |
18 |
Correct |
13 ms |
99928 KB |
Output is correct |
19 |
Correct |
13 ms |
99932 KB |
Output is correct |
20 |
Correct |
14 ms |
100096 KB |
Output is correct |
21 |
Correct |
12 ms |
99932 KB |
Output is correct |
22 |
Correct |
12 ms |
100028 KB |
Output is correct |
23 |
Correct |
13 ms |
99932 KB |
Output is correct |
24 |
Correct |
13 ms |
100028 KB |
Output is correct |
25 |
Correct |
13 ms |
99932 KB |
Output is correct |
26 |
Correct |
13 ms |
99932 KB |
Output is correct |
27 |
Correct |
13 ms |
99928 KB |
Output is correct |
28 |
Correct |
13 ms |
100132 KB |
Output is correct |
29 |
Correct |
13 ms |
100340 KB |
Output is correct |
30 |
Correct |
13 ms |
99988 KB |
Output is correct |
31 |
Correct |
13 ms |
100092 KB |
Output is correct |
32 |
Correct |
13 ms |
100052 KB |
Output is correct |
33 |
Correct |
13 ms |
99932 KB |
Output is correct |
34 |
Correct |
13 ms |
100188 KB |
Output is correct |
35 |
Correct |
13 ms |
99932 KB |
Output is correct |
36 |
Correct |
14 ms |
99932 KB |
Output is correct |
37 |
Correct |
13 ms |
99932 KB |
Output is correct |
38 |
Correct |
13 ms |
99932 KB |
Output is correct |
39 |
Correct |
13 ms |
100184 KB |
Output is correct |
40 |
Correct |
13 ms |
99928 KB |
Output is correct |
41 |
Correct |
13 ms |
99932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
99932 KB |
Output is correct |
2 |
Correct |
19 ms |
100048 KB |
Output is correct |
3 |
Correct |
18 ms |
99932 KB |
Output is correct |
4 |
Correct |
20 ms |
100136 KB |
Output is correct |
5 |
Correct |
18 ms |
99932 KB |
Output is correct |
6 |
Correct |
19 ms |
99932 KB |
Output is correct |
7 |
Correct |
20 ms |
99932 KB |
Output is correct |
8 |
Correct |
19 ms |
99932 KB |
Output is correct |
9 |
Correct |
18 ms |
99932 KB |
Output is correct |
10 |
Correct |
23 ms |
100096 KB |
Output is correct |
11 |
Correct |
19 ms |
99932 KB |
Output is correct |
12 |
Correct |
14 ms |
99928 KB |
Output is correct |
13 |
Correct |
15 ms |
99932 KB |
Output is correct |
14 |
Correct |
16 ms |
99932 KB |
Output is correct |
15 |
Correct |
13 ms |
100040 KB |
Output is correct |
16 |
Correct |
12 ms |
99932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
100092 KB |
Output is correct |
2 |
Correct |
15 ms |
99932 KB |
Output is correct |
3 |
Correct |
13 ms |
99932 KB |
Output is correct |
4 |
Correct |
12 ms |
100012 KB |
Output is correct |
5 |
Correct |
12 ms |
99932 KB |
Output is correct |
6 |
Correct |
13 ms |
100092 KB |
Output is correct |
7 |
Correct |
12 ms |
99928 KB |
Output is correct |
8 |
Correct |
13 ms |
99928 KB |
Output is correct |
9 |
Correct |
13 ms |
99932 KB |
Output is correct |
10 |
Correct |
12 ms |
99932 KB |
Output is correct |
11 |
Correct |
15 ms |
99932 KB |
Output is correct |
12 |
Correct |
13 ms |
100108 KB |
Output is correct |
13 |
Correct |
12 ms |
99932 KB |
Output is correct |
14 |
Correct |
13 ms |
99932 KB |
Output is correct |
15 |
Correct |
13 ms |
99932 KB |
Output is correct |
16 |
Correct |
12 ms |
99996 KB |
Output is correct |
17 |
Correct |
13 ms |
99932 KB |
Output is correct |
18 |
Correct |
13 ms |
99928 KB |
Output is correct |
19 |
Correct |
13 ms |
99932 KB |
Output is correct |
20 |
Correct |
14 ms |
100096 KB |
Output is correct |
21 |
Correct |
12 ms |
99932 KB |
Output is correct |
22 |
Correct |
12 ms |
100028 KB |
Output is correct |
23 |
Correct |
13 ms |
99932 KB |
Output is correct |
24 |
Correct |
13 ms |
100028 KB |
Output is correct |
25 |
Correct |
13 ms |
99932 KB |
Output is correct |
26 |
Correct |
13 ms |
99932 KB |
Output is correct |
27 |
Correct |
13 ms |
99928 KB |
Output is correct |
28 |
Correct |
13 ms |
100132 KB |
Output is correct |
29 |
Correct |
13 ms |
100340 KB |
Output is correct |
30 |
Correct |
13 ms |
99988 KB |
Output is correct |
31 |
Correct |
13 ms |
100092 KB |
Output is correct |
32 |
Correct |
13 ms |
100052 KB |
Output is correct |
33 |
Correct |
13 ms |
99932 KB |
Output is correct |
34 |
Correct |
13 ms |
100188 KB |
Output is correct |
35 |
Correct |
13 ms |
99932 KB |
Output is correct |
36 |
Correct |
14 ms |
99932 KB |
Output is correct |
37 |
Correct |
13 ms |
99932 KB |
Output is correct |
38 |
Correct |
13 ms |
99932 KB |
Output is correct |
39 |
Correct |
13 ms |
100184 KB |
Output is correct |
40 |
Correct |
13 ms |
99928 KB |
Output is correct |
41 |
Correct |
13 ms |
99932 KB |
Output is correct |
42 |
Correct |
12 ms |
99932 KB |
Output is correct |
43 |
Correct |
19 ms |
100048 KB |
Output is correct |
44 |
Correct |
18 ms |
99932 KB |
Output is correct |
45 |
Correct |
20 ms |
100136 KB |
Output is correct |
46 |
Correct |
18 ms |
99932 KB |
Output is correct |
47 |
Correct |
19 ms |
99932 KB |
Output is correct |
48 |
Correct |
20 ms |
99932 KB |
Output is correct |
49 |
Correct |
19 ms |
99932 KB |
Output is correct |
50 |
Correct |
18 ms |
99932 KB |
Output is correct |
51 |
Correct |
23 ms |
100096 KB |
Output is correct |
52 |
Correct |
19 ms |
99932 KB |
Output is correct |
53 |
Correct |
14 ms |
99928 KB |
Output is correct |
54 |
Correct |
15 ms |
99932 KB |
Output is correct |
55 |
Correct |
16 ms |
99932 KB |
Output is correct |
56 |
Correct |
13 ms |
100040 KB |
Output is correct |
57 |
Correct |
12 ms |
99932 KB |
Output is correct |
58 |
Correct |
73 ms |
100116 KB |
Output is correct |
59 |
Correct |
82 ms |
100120 KB |
Output is correct |
60 |
Correct |
80 ms |
100112 KB |
Output is correct |
61 |
Correct |
80 ms |
100116 KB |
Output is correct |
62 |
Correct |
12 ms |
99932 KB |
Output is correct |
63 |
Correct |
20 ms |
99928 KB |
Output is correct |
64 |
Correct |
29 ms |
100144 KB |
Output is correct |
65 |
Correct |
42 ms |
99932 KB |
Output is correct |
66 |
Correct |
75 ms |
99932 KB |
Output is correct |
67 |
Correct |
72 ms |
99932 KB |
Output is correct |
68 |
Correct |
76 ms |
99932 KB |
Output is correct |
69 |
Correct |
80 ms |
100128 KB |
Output is correct |
70 |
Correct |
76 ms |
100120 KB |
Output is correct |
71 |
Correct |
73 ms |
100124 KB |
Output is correct |
72 |
Correct |
12 ms |
99928 KB |
Output is correct |
73 |
Correct |
13 ms |
99932 KB |
Output is correct |