# include <bits/stdc++.h>
// #pragma optimize("g", on)
// #pragma GCC optimize ("inline")
// #pragma GCC optimize ("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC optimize ("03")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx2,mmx,fma,avx,tune=native")
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
# define ll long long
# define pii pair <int, int>
# define pll pair <ll, ll>
# define nl '\n'
# define sz(x) (int)(x).size()
# define all(x) (x).begin(), (x).end()
# define deb(x) cerr << #x << " = " << x << endl;
const int maxn = (int)404;
const ll inf = (ll)2e5;
const int mod = (int)1e9 + 7;
const bool T = 0;
short n;
string s;
int dp[maxn][maxn][maxn][3];
short pref[3][maxn];
vector <short> g[3];
short d[3];
short f (char c) {
if (c == 'R') return 0;
if (c == 'G') return 1;
if (c == 'Y') return 2;
assert(0);
return -1;
}
void op (int &a, int b) {
a = min(a, b);
}
void ma1n () {
cin >> n >> s;
g[0].push_back(0);
g[1].push_back(0);
g[2].push_back(0);
for (short i = 0; i < n; ++i) {
for (short j = 0; j < 3; ++j) {
pref[j][i + 1] = pref[j][i];
}
short x = f(s[i]);
pref[x][i + 1]++;
g[x].push_back(i + 1);
}
for (short i = 0; i <= n; ++i) {
for (short x = 0; x <= pref[0][n]; ++x) {
for (short y = 0; y <= pref[1][n]; ++y) {
for (short k = 0; k < 3; ++k) {
dp[i][x][y][k] = inf;
}
}
}
}
// memset(dp, 0x3f, sizeof dp);
// int lim = dp[3][2][1][0];
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
// deb(s);
// exit(0);
for (short i = 0; i < n; ++i) {
for (short x = 0; x <= min(i, pref[0][n]); ++x) {
for (short y = 0; y <= min((short)(i - x), pref[1][n]); ++y) {
if (x + y > i) continue;
short z = i - x - y;
if (z > pref[2][n]) continue;
d[0] = x, d[1] = y, d[2] = z;
for (short prv = 0; prv < 3; ++prv) {
for (short cur = 0; cur < 3; ++cur) {
if (prv == cur || d[cur] == pref[cur][n]) continue;
// cout << "continue everything" << nl;
// continue;
short othr = prv ^ cur ^ 3;
assert(othr >= 0 && othr < 3 && othr != prv && othr != cur);
short id = g[cur][d[cur] + 1];
op(dp[i + 1][x + (cur == 0)][y + (cur == 1)][cur], dp[i][x][y][prv] + max(pref[prv][id] - d[prv], 0) + max(pref[othr][id] - d[othr], 0));
}
}
}
}
}
int ans = inf;
for (short last = 0; last < 3; ++last) {
ans = min(ans, dp[n][pref[0][n]][pref[1][n]][last]);
// cout << pref[0][n] << ' ' << pref[1][n] << ' ' << last << ' ' <<dp[n][pref[0][n]][pref[1][n]][last] << nl;
}
// for (int x = 0; x <= n; ++x) {
// for (int y = 0; y <= n; ++y) {
// if (x + y > n) continue;
// for (int last = 0; last < 3; ++last) {
// ans = min(ans, dp[n][x][y][last]);
// if (dp[n][x][y][last] != lim) {
// cout << x << ' ' << y << ' ' << last << ' ' << dp[n][x][y][last] << nl;
// }
// }
// }
// }
cout << (ans == inf ? -1 : ans) << nl;
}
signed main () {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int ttt = 1;
if (T) cin >> ttt;
while (ttt--) {
ma1n();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
20816 KB |
Output is correct |
5 |
Correct |
4 ms |
29008 KB |
Output is correct |
6 |
Correct |
4 ms |
29008 KB |
Output is correct |
7 |
Correct |
3 ms |
29008 KB |
Output is correct |
8 |
Correct |
3 ms |
29008 KB |
Output is correct |
9 |
Correct |
3 ms |
29176 KB |
Output is correct |
10 |
Correct |
3 ms |
29008 KB |
Output is correct |
11 |
Correct |
3 ms |
29008 KB |
Output is correct |
12 |
Correct |
3 ms |
29008 KB |
Output is correct |
13 |
Correct |
3 ms |
29008 KB |
Output is correct |
14 |
Correct |
3 ms |
29008 KB |
Output is correct |
15 |
Correct |
3 ms |
29172 KB |
Output is correct |
16 |
Correct |
3 ms |
29008 KB |
Output is correct |
17 |
Correct |
3 ms |
26960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
20816 KB |
Output is correct |
5 |
Correct |
4 ms |
29008 KB |
Output is correct |
6 |
Correct |
4 ms |
29008 KB |
Output is correct |
7 |
Correct |
3 ms |
29008 KB |
Output is correct |
8 |
Correct |
3 ms |
29008 KB |
Output is correct |
9 |
Correct |
3 ms |
29176 KB |
Output is correct |
10 |
Correct |
3 ms |
29008 KB |
Output is correct |
11 |
Correct |
3 ms |
29008 KB |
Output is correct |
12 |
Correct |
3 ms |
29008 KB |
Output is correct |
13 |
Correct |
3 ms |
29008 KB |
Output is correct |
14 |
Correct |
3 ms |
29008 KB |
Output is correct |
15 |
Correct |
3 ms |
29172 KB |
Output is correct |
16 |
Correct |
3 ms |
29008 KB |
Output is correct |
17 |
Correct |
3 ms |
26960 KB |
Output is correct |
18 |
Correct |
11 ms |
117328 KB |
Output is correct |
19 |
Correct |
11 ms |
115280 KB |
Output is correct |
20 |
Correct |
11 ms |
117368 KB |
Output is correct |
21 |
Correct |
11 ms |
115280 KB |
Output is correct |
22 |
Correct |
11 ms |
117328 KB |
Output is correct |
23 |
Correct |
10 ms |
115280 KB |
Output is correct |
24 |
Correct |
11 ms |
115460 KB |
Output is correct |
25 |
Correct |
13 ms |
117584 KB |
Output is correct |
26 |
Correct |
12 ms |
117328 KB |
Output is correct |
27 |
Correct |
12 ms |
117328 KB |
Output is correct |
28 |
Correct |
13 ms |
117340 KB |
Output is correct |
29 |
Correct |
12 ms |
117328 KB |
Output is correct |
30 |
Correct |
12 ms |
115292 KB |
Output is correct |
31 |
Correct |
11 ms |
115280 KB |
Output is correct |
32 |
Correct |
14 ms |
115448 KB |
Output is correct |
33 |
Correct |
12 ms |
115280 KB |
Output is correct |
34 |
Correct |
12 ms |
115280 KB |
Output is correct |
35 |
Correct |
12 ms |
113500 KB |
Output is correct |
36 |
Correct |
11 ms |
115280 KB |
Output is correct |
37 |
Correct |
11 ms |
111184 KB |
Output is correct |
38 |
Correct |
12 ms |
115280 KB |
Output is correct |
39 |
Correct |
11 ms |
115280 KB |
Output is correct |
40 |
Correct |
11 ms |
115280 KB |
Output is correct |
41 |
Correct |
11 ms |
115404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6480 KB |
Output is correct |
2 |
Correct |
233 ms |
489800 KB |
Output is correct |
3 |
Correct |
221 ms |
499272 KB |
Output is correct |
4 |
Correct |
222 ms |
504020 KB |
Output is correct |
5 |
Correct |
208 ms |
520776 KB |
Output is correct |
6 |
Correct |
223 ms |
520496 KB |
Output is correct |
7 |
Correct |
226 ms |
512264 KB |
Output is correct |
8 |
Correct |
220 ms |
511304 KB |
Output is correct |
9 |
Correct |
224 ms |
513820 KB |
Output is correct |
10 |
Correct |
220 ms |
521600 KB |
Output is correct |
11 |
Correct |
215 ms |
528968 KB |
Output is correct |
12 |
Correct |
49 ms |
303688 KB |
Output is correct |
13 |
Correct |
96 ms |
358984 KB |
Output is correct |
14 |
Correct |
156 ms |
422984 KB |
Output is correct |
15 |
Correct |
221 ms |
518996 KB |
Output is correct |
16 |
Correct |
207 ms |
518216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
20816 KB |
Output is correct |
5 |
Correct |
4 ms |
29008 KB |
Output is correct |
6 |
Correct |
4 ms |
29008 KB |
Output is correct |
7 |
Correct |
3 ms |
29008 KB |
Output is correct |
8 |
Correct |
3 ms |
29008 KB |
Output is correct |
9 |
Correct |
3 ms |
29176 KB |
Output is correct |
10 |
Correct |
3 ms |
29008 KB |
Output is correct |
11 |
Correct |
3 ms |
29008 KB |
Output is correct |
12 |
Correct |
3 ms |
29008 KB |
Output is correct |
13 |
Correct |
3 ms |
29008 KB |
Output is correct |
14 |
Correct |
3 ms |
29008 KB |
Output is correct |
15 |
Correct |
3 ms |
29172 KB |
Output is correct |
16 |
Correct |
3 ms |
29008 KB |
Output is correct |
17 |
Correct |
3 ms |
26960 KB |
Output is correct |
18 |
Correct |
11 ms |
117328 KB |
Output is correct |
19 |
Correct |
11 ms |
115280 KB |
Output is correct |
20 |
Correct |
11 ms |
117368 KB |
Output is correct |
21 |
Correct |
11 ms |
115280 KB |
Output is correct |
22 |
Correct |
11 ms |
117328 KB |
Output is correct |
23 |
Correct |
10 ms |
115280 KB |
Output is correct |
24 |
Correct |
11 ms |
115460 KB |
Output is correct |
25 |
Correct |
13 ms |
117584 KB |
Output is correct |
26 |
Correct |
12 ms |
117328 KB |
Output is correct |
27 |
Correct |
12 ms |
117328 KB |
Output is correct |
28 |
Correct |
13 ms |
117340 KB |
Output is correct |
29 |
Correct |
12 ms |
117328 KB |
Output is correct |
30 |
Correct |
12 ms |
115292 KB |
Output is correct |
31 |
Correct |
11 ms |
115280 KB |
Output is correct |
32 |
Correct |
14 ms |
115448 KB |
Output is correct |
33 |
Correct |
12 ms |
115280 KB |
Output is correct |
34 |
Correct |
12 ms |
115280 KB |
Output is correct |
35 |
Correct |
12 ms |
113500 KB |
Output is correct |
36 |
Correct |
11 ms |
115280 KB |
Output is correct |
37 |
Correct |
11 ms |
111184 KB |
Output is correct |
38 |
Correct |
12 ms |
115280 KB |
Output is correct |
39 |
Correct |
11 ms |
115280 KB |
Output is correct |
40 |
Correct |
11 ms |
115280 KB |
Output is correct |
41 |
Correct |
11 ms |
115404 KB |
Output is correct |
42 |
Correct |
1 ms |
6480 KB |
Output is correct |
43 |
Correct |
233 ms |
489800 KB |
Output is correct |
44 |
Correct |
221 ms |
499272 KB |
Output is correct |
45 |
Correct |
222 ms |
504020 KB |
Output is correct |
46 |
Correct |
208 ms |
520776 KB |
Output is correct |
47 |
Correct |
223 ms |
520496 KB |
Output is correct |
48 |
Correct |
226 ms |
512264 KB |
Output is correct |
49 |
Correct |
220 ms |
511304 KB |
Output is correct |
50 |
Correct |
224 ms |
513820 KB |
Output is correct |
51 |
Correct |
220 ms |
521600 KB |
Output is correct |
52 |
Correct |
215 ms |
528968 KB |
Output is correct |
53 |
Correct |
49 ms |
303688 KB |
Output is correct |
54 |
Correct |
96 ms |
358984 KB |
Output is correct |
55 |
Correct |
156 ms |
422984 KB |
Output is correct |
56 |
Correct |
221 ms |
518996 KB |
Output is correct |
57 |
Correct |
207 ms |
518216 KB |
Output is correct |
58 |
Correct |
225 ms |
457288 KB |
Output is correct |
59 |
Correct |
242 ms |
468768 KB |
Output is correct |
60 |
Correct |
232 ms |
451656 KB |
Output is correct |
61 |
Correct |
243 ms |
446024 KB |
Output is correct |
62 |
Correct |
228 ms |
546376 KB |
Output is correct |
63 |
Correct |
227 ms |
526084 KB |
Output is correct |
64 |
Correct |
242 ms |
513140 KB |
Output is correct |
65 |
Correct |
234 ms |
488008 KB |
Output is correct |
66 |
Correct |
235 ms |
454728 KB |
Output is correct |
67 |
Correct |
229 ms |
434760 KB |
Output is correct |
68 |
Correct |
218 ms |
448328 KB |
Output is correct |
69 |
Correct |
234 ms |
446060 KB |
Output is correct |
70 |
Correct |
231 ms |
449096 KB |
Output is correct |
71 |
Correct |
237 ms |
456008 KB |
Output is correct |
72 |
Correct |
236 ms |
618136 KB |
Output is correct |
73 |
Correct |
33 ms |
284408 KB |
Output is correct |