#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#pragma GCC optimize("unroll-loops")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 4e2+20,mod = 1e9+7,inf = 1e9+10,maxm = (1 << 18)+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k /= 2;
}
return z;
}
int dp[2][N][N][3];
int R[N],G[N],Y[N];
int main(){
ios :: sync_with_stdio(0); cin.tie(0);
int n;
string s;
cin >> n >> s;
rep(i,0,n){
if (s[i] == 'R') R[i] = 1;
if (s[i] == 'G') G[i] = 1;
if (s[i] == 'Y') Y[i] = 1;
if (i){
R[i] += R[i-1];
G[i] += G[i-1];
Y[i] += Y[i-1];
}
}
dp[0][0][0][2] = inf;
dp[0][0][1][1] = inf;
dp[0][1][0][0] = inf;
rep(i,1,n){
bool f = (i&1);
rep(r,0,i+2){
rep(g,0,i-r+2){
rep(j,0,3) dp[f][r][g][j] = inf;
if (R[n-1] < r || G[n-1] < g || Y[n-1] < i+1-r-g) continue;
int lst[3] = {-1,-1,-1};
lst[0] = lower_bound(R,R+n,r)-R;
lst[1] = lower_bound(G,G+n,g)-G;
lst[2] = lower_bound(Y,Y+n,i+1-r-g)-Y;
if (r){
int count = max(0,g-G[lst[0]])+max(0,r-R[lst[0]])+max(0,i+1-r-g-Y[lst[0]]);
dp[f][r][g][1] = min(dp[f][r][g][1],count+dp[1-f][r-1][g][0]);
dp[f][r][g][2] = min(dp[f][r][g][2],count+dp[1-f][r-1][g][0]);
}
if (g){
int count = max(0,g-G[lst[1]])+max(0,r-R[lst[1]])+max(0,i+1-r-g-Y[lst[1]]);
dp[f][r][g][0] = min(dp[f][r][g][0],count+dp[1-f][r][g-1][1]);
dp[f][r][g][2] = min(dp[f][r][g][2],count+dp[1-f][r][g-1][1]);
}
if (i+1-g-r){
int count = max(0,g-G[lst[2]])+max(0,r-R[lst[2]])+max(0,i+1-r-g-Y[lst[2]]);
dp[f][r][g][1] = min(dp[f][r][g][1],count+dp[1-f][r][g][2]);
dp[f][r][g][0] = min(dp[f][r][g][0],count+dp[1-f][r][g][2]);
}
}
}
}
bool f = (n-1)&1;
int ans = inf;
rep(j,0,3) ans = min(ans,dp[f][R[n-1]][G[n-1]][j]);
if (ans == inf) cout << -1 << endl;
else cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
452 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
452 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
2 ms |
852 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
2 ms |
836 KB |
Output is correct |
22 |
Correct |
2 ms |
852 KB |
Output is correct |
23 |
Correct |
2 ms |
852 KB |
Output is correct |
24 |
Correct |
2 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
2 ms |
844 KB |
Output is correct |
28 |
Correct |
2 ms |
852 KB |
Output is correct |
29 |
Correct |
2 ms |
836 KB |
Output is correct |
30 |
Correct |
2 ms |
800 KB |
Output is correct |
31 |
Correct |
2 ms |
852 KB |
Output is correct |
32 |
Correct |
2 ms |
836 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
2 ms |
852 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
2 ms |
840 KB |
Output is correct |
37 |
Correct |
2 ms |
720 KB |
Output is correct |
38 |
Correct |
2 ms |
852 KB |
Output is correct |
39 |
Correct |
2 ms |
840 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
34 ms |
4168 KB |
Output is correct |
3 |
Correct |
35 ms |
4116 KB |
Output is correct |
4 |
Correct |
34 ms |
4152 KB |
Output is correct |
5 |
Correct |
38 ms |
4172 KB |
Output is correct |
6 |
Correct |
35 ms |
4164 KB |
Output is correct |
7 |
Correct |
42 ms |
4056 KB |
Output is correct |
8 |
Correct |
35 ms |
4132 KB |
Output is correct |
9 |
Correct |
33 ms |
4048 KB |
Output is correct |
10 |
Correct |
36 ms |
4060 KB |
Output is correct |
11 |
Correct |
33 ms |
4184 KB |
Output is correct |
12 |
Correct |
6 ms |
2228 KB |
Output is correct |
13 |
Correct |
12 ms |
2864 KB |
Output is correct |
14 |
Correct |
17 ms |
3412 KB |
Output is correct |
15 |
Correct |
27 ms |
4288 KB |
Output is correct |
16 |
Correct |
34 ms |
4300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
452 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
2 ms |
852 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
2 ms |
836 KB |
Output is correct |
22 |
Correct |
2 ms |
852 KB |
Output is correct |
23 |
Correct |
2 ms |
852 KB |
Output is correct |
24 |
Correct |
2 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
2 ms |
844 KB |
Output is correct |
28 |
Correct |
2 ms |
852 KB |
Output is correct |
29 |
Correct |
2 ms |
836 KB |
Output is correct |
30 |
Correct |
2 ms |
800 KB |
Output is correct |
31 |
Correct |
2 ms |
852 KB |
Output is correct |
32 |
Correct |
2 ms |
836 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
2 ms |
852 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
2 ms |
840 KB |
Output is correct |
37 |
Correct |
2 ms |
720 KB |
Output is correct |
38 |
Correct |
2 ms |
852 KB |
Output is correct |
39 |
Correct |
2 ms |
840 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
1 ms |
852 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
34 ms |
4168 KB |
Output is correct |
44 |
Correct |
35 ms |
4116 KB |
Output is correct |
45 |
Correct |
34 ms |
4152 KB |
Output is correct |
46 |
Correct |
38 ms |
4172 KB |
Output is correct |
47 |
Correct |
35 ms |
4164 KB |
Output is correct |
48 |
Correct |
42 ms |
4056 KB |
Output is correct |
49 |
Correct |
35 ms |
4132 KB |
Output is correct |
50 |
Correct |
33 ms |
4048 KB |
Output is correct |
51 |
Correct |
36 ms |
4060 KB |
Output is correct |
52 |
Correct |
33 ms |
4184 KB |
Output is correct |
53 |
Correct |
6 ms |
2228 KB |
Output is correct |
54 |
Correct |
12 ms |
2864 KB |
Output is correct |
55 |
Correct |
17 ms |
3412 KB |
Output is correct |
56 |
Correct |
27 ms |
4288 KB |
Output is correct |
57 |
Correct |
34 ms |
4300 KB |
Output is correct |
58 |
Correct |
300 ms |
4140 KB |
Output is correct |
59 |
Correct |
296 ms |
4200 KB |
Output is correct |
60 |
Correct |
292 ms |
4084 KB |
Output is correct |
61 |
Correct |
286 ms |
4096 KB |
Output is correct |
62 |
Correct |
45 ms |
4172 KB |
Output is correct |
63 |
Correct |
67 ms |
4160 KB |
Output is correct |
64 |
Correct |
137 ms |
4136 KB |
Output is correct |
65 |
Correct |
202 ms |
4104 KB |
Output is correct |
66 |
Correct |
243 ms |
4036 KB |
Output is correct |
67 |
Correct |
251 ms |
4176 KB |
Output is correct |
68 |
Correct |
257 ms |
4112 KB |
Output is correct |
69 |
Correct |
257 ms |
4192 KB |
Output is correct |
70 |
Correct |
274 ms |
4156 KB |
Output is correct |
71 |
Correct |
273 ms |
4088 KB |
Output is correct |
72 |
Correct |
97 ms |
4172 KB |
Output is correct |
73 |
Correct |
42 ms |
4172 KB |
Output is correct |