#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ld long double
#define fi first
#define se second
#define ll long long
#define ss(x) (int) x.size()
#define mp make_pair
#define FOR(i, n) for(int i = 1; n >= i; ++i)
using namespace std;
using namespace __gnu_pbds;
const int inf = 1e9 + 1;
int dp[405][405][405][3];
int n;
string s;
int cnt[405][3];
int t[405];
vector <int> v[3];
int ile[3];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n >> s;
for(int i = 0; i < n; ++i) {
if(s[i] == 'R')
t[i + 1] = 0;
if(s[i] == 'G')
t[i + 1] = 1;
if(s[i] == 'Y')
t[i + 1] = 2;
}
for(int i = 0; i < 3; ++i) {
v[i].pb(0);
}
for(int i = 1; i <= n; ++i) {
for(int j = 0; j < 3; ++j)
cnt[i][j] = cnt[i - 1][j];
cnt[i][t[i]]++;
v[t[i]].pb(i);
}
int R = cnt[n][0];
int G = cnt[n][1];
int Y = cnt[n][2];
for(int r = 0; r <= R; ++r)
for(int g = 0; g <= G; ++g)
for(int y = 0; y <= Y; ++y)
for(int z = 0; z < 3; ++z)
dp[r][g][y][z] = inf;
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
int maxi = max({R, G, Y});
if(maxi > (n + 1) / 2) {
cout << -1 << endl;
return 0;
}
for(int r = 0; r <= R; ++r)
for(int g = 0; g <= G; ++g)
for(int y = 0; y <= Y; ++y) {
for(int last = 0; last < 3; ++last) {
int ans = dp[r][g][y][last];
ile[0] = r;
ile[1] = g;
ile[2] = y;
for(int i = 0; i < 3; ++i) {
if(i == last || ile[i] + 1 == ss(v[i]))
continue;
int pos = v[i][ile[i] + 1];
int add = -1 + pos - r - g - y;
for(int j = 0; j < 3; ++j) {
int ost = v[j][ile[j]];
add += max(0, cnt[ost][j] - cnt[pos][j]);
}
//cout << r << " " << g << " " << y << " " << " " << i << " " << ans << endl;
ile[i]++;
dp[ile[0]][ile[1]][ile[2]][i] = min(dp[ile[0]][ile[1]][ile[2]][i], add + ans);
ile[i]--;
}
}
}
int best = inf;
for(int i = 0; i < 3; ++i)
best = min(best, dp[R][G][Y][i]);
cout << best;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
432 KB |
Output is correct |
18 |
Correct |
4 ms |
2432 KB |
Output is correct |
19 |
Correct |
3 ms |
1920 KB |
Output is correct |
20 |
Correct |
4 ms |
2304 KB |
Output is correct |
21 |
Correct |
4 ms |
2432 KB |
Output is correct |
22 |
Correct |
3 ms |
1792 KB |
Output is correct |
23 |
Correct |
4 ms |
2176 KB |
Output is correct |
24 |
Correct |
3 ms |
1664 KB |
Output is correct |
25 |
Correct |
5 ms |
3968 KB |
Output is correct |
26 |
Correct |
4 ms |
4224 KB |
Output is correct |
27 |
Correct |
4 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
2432 KB |
Output is correct |
29 |
Correct |
3 ms |
2304 KB |
Output is correct |
30 |
Correct |
4 ms |
2304 KB |
Output is correct |
31 |
Correct |
4 ms |
2048 KB |
Output is correct |
32 |
Correct |
4 ms |
2432 KB |
Output is correct |
33 |
Correct |
5 ms |
3968 KB |
Output is correct |
34 |
Correct |
4 ms |
3584 KB |
Output is correct |
35 |
Correct |
4 ms |
2816 KB |
Output is correct |
36 |
Correct |
3 ms |
1920 KB |
Output is correct |
37 |
Correct |
3 ms |
1792 KB |
Output is correct |
38 |
Correct |
3 ms |
2176 KB |
Output is correct |
39 |
Correct |
3 ms |
2304 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
2304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
115 ms |
163016 KB |
Output is correct |
3 |
Correct |
117 ms |
162212 KB |
Output is correct |
4 |
Correct |
117 ms |
163160 KB |
Output is correct |
5 |
Correct |
115 ms |
162936 KB |
Output is correct |
6 |
Correct |
118 ms |
163072 KB |
Output is correct |
7 |
Correct |
118 ms |
162168 KB |
Output is correct |
8 |
Correct |
115 ms |
162172 KB |
Output is correct |
9 |
Correct |
117 ms |
161468 KB |
Output is correct |
10 |
Correct |
114 ms |
163064 KB |
Output is correct |
11 |
Correct |
116 ms |
162936 KB |
Output is correct |
12 |
Correct |
59 ms |
44228 KB |
Output is correct |
13 |
Correct |
58 ms |
77176 KB |
Output is correct |
14 |
Correct |
80 ms |
111352 KB |
Output is correct |
15 |
Correct |
109 ms |
163036 KB |
Output is correct |
16 |
Correct |
112 ms |
163064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
432 KB |
Output is correct |
18 |
Correct |
4 ms |
2432 KB |
Output is correct |
19 |
Correct |
3 ms |
1920 KB |
Output is correct |
20 |
Correct |
4 ms |
2304 KB |
Output is correct |
21 |
Correct |
4 ms |
2432 KB |
Output is correct |
22 |
Correct |
3 ms |
1792 KB |
Output is correct |
23 |
Correct |
4 ms |
2176 KB |
Output is correct |
24 |
Correct |
3 ms |
1664 KB |
Output is correct |
25 |
Correct |
5 ms |
3968 KB |
Output is correct |
26 |
Correct |
4 ms |
4224 KB |
Output is correct |
27 |
Correct |
4 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
2432 KB |
Output is correct |
29 |
Correct |
3 ms |
2304 KB |
Output is correct |
30 |
Correct |
4 ms |
2304 KB |
Output is correct |
31 |
Correct |
4 ms |
2048 KB |
Output is correct |
32 |
Correct |
4 ms |
2432 KB |
Output is correct |
33 |
Correct |
5 ms |
3968 KB |
Output is correct |
34 |
Correct |
4 ms |
3584 KB |
Output is correct |
35 |
Correct |
4 ms |
2816 KB |
Output is correct |
36 |
Correct |
3 ms |
1920 KB |
Output is correct |
37 |
Correct |
3 ms |
1792 KB |
Output is correct |
38 |
Correct |
3 ms |
2176 KB |
Output is correct |
39 |
Correct |
3 ms |
2304 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
2304 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
115 ms |
163016 KB |
Output is correct |
44 |
Correct |
117 ms |
162212 KB |
Output is correct |
45 |
Correct |
117 ms |
163160 KB |
Output is correct |
46 |
Correct |
115 ms |
162936 KB |
Output is correct |
47 |
Correct |
118 ms |
163072 KB |
Output is correct |
48 |
Correct |
118 ms |
162168 KB |
Output is correct |
49 |
Correct |
115 ms |
162172 KB |
Output is correct |
50 |
Correct |
117 ms |
161468 KB |
Output is correct |
51 |
Correct |
114 ms |
163064 KB |
Output is correct |
52 |
Correct |
116 ms |
162936 KB |
Output is correct |
53 |
Correct |
59 ms |
44228 KB |
Output is correct |
54 |
Correct |
58 ms |
77176 KB |
Output is correct |
55 |
Correct |
80 ms |
111352 KB |
Output is correct |
56 |
Correct |
109 ms |
163036 KB |
Output is correct |
57 |
Correct |
112 ms |
163064 KB |
Output is correct |
58 |
Correct |
120 ms |
76252 KB |
Output is correct |
59 |
Correct |
133 ms |
96796 KB |
Output is correct |
60 |
Correct |
130 ms |
87932 KB |
Output is correct |
61 |
Correct |
128 ms |
84964 KB |
Output is correct |
62 |
Correct |
106 ms |
160632 KB |
Output is correct |
63 |
Correct |
121 ms |
160412 KB |
Output is correct |
64 |
Correct |
148 ms |
150200 KB |
Output is correct |
65 |
Correct |
151 ms |
138716 KB |
Output is correct |
66 |
Correct |
124 ms |
86136 KB |
Output is correct |
67 |
Correct |
120 ms |
82964 KB |
Output is correct |
68 |
Correct |
132 ms |
88864 KB |
Output is correct |
69 |
Correct |
130 ms |
85496 KB |
Output is correct |
70 |
Correct |
130 ms |
92656 KB |
Output is correct |
71 |
Correct |
121 ms |
83920 KB |
Output is correct |
72 |
Correct |
29 ms |
41600 KB |
Output is correct |
73 |
Correct |
3 ms |
2944 KB |
Output is correct |