# 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 |
3 ms |
20816 KB |
Output is correct |
5 |
Correct |
3 ms |
29204 KB |
Output is correct |
6 |
Correct |
3 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 |
4 ms |
29008 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 |
4 ms |
29008 KB |
Output is correct |
15 |
Correct |
3 ms |
29008 KB |
Output is correct |
16 |
Correct |
3 ms |
29112 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 |
3 ms |
20816 KB |
Output is correct |
5 |
Correct |
3 ms |
29204 KB |
Output is correct |
6 |
Correct |
3 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 |
4 ms |
29008 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 |
4 ms |
29008 KB |
Output is correct |
15 |
Correct |
3 ms |
29008 KB |
Output is correct |
16 |
Correct |
3 ms |
29112 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 |
115432 KB |
Output is correct |
20 |
Correct |
11 ms |
117520 KB |
Output is correct |
21 |
Correct |
12 ms |
115280 KB |
Output is correct |
22 |
Correct |
12 ms |
117496 KB |
Output is correct |
23 |
Correct |
11 ms |
115292 KB |
Output is correct |
24 |
Correct |
12 ms |
115280 KB |
Output is correct |
25 |
Correct |
11 ms |
117584 KB |
Output is correct |
26 |
Correct |
12 ms |
117520 KB |
Output is correct |
27 |
Correct |
11 ms |
117500 KB |
Output is correct |
28 |
Correct |
11 ms |
117344 KB |
Output is correct |
29 |
Correct |
11 ms |
117328 KB |
Output is correct |
30 |
Correct |
11 ms |
115280 KB |
Output is correct |
31 |
Correct |
11 ms |
115280 KB |
Output is correct |
32 |
Correct |
11 ms |
115280 KB |
Output is correct |
33 |
Correct |
12 ms |
115296 KB |
Output is correct |
34 |
Correct |
11 ms |
115280 KB |
Output is correct |
35 |
Correct |
11 ms |
113448 KB |
Output is correct |
36 |
Correct |
11 ms |
115296 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 |
12 ms |
115316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6480 KB |
Output is correct |
2 |
Correct |
204 ms |
514376 KB |
Output is correct |
3 |
Correct |
216 ms |
525896 KB |
Output is correct |
4 |
Correct |
210 ms |
527304 KB |
Output is correct |
5 |
Correct |
239 ms |
532808 KB |
Output is correct |
6 |
Correct |
221 ms |
528460 KB |
Output is correct |
7 |
Correct |
208 ms |
528968 KB |
Output is correct |
8 |
Correct |
213 ms |
523592 KB |
Output is correct |
9 |
Correct |
214 ms |
520364 KB |
Output is correct |
10 |
Correct |
230 ms |
536824 KB |
Output is correct |
11 |
Correct |
204 ms |
542024 KB |
Output is correct |
12 |
Correct |
48 ms |
338768 KB |
Output is correct |
13 |
Correct |
87 ms |
388936 KB |
Output is correct |
14 |
Correct |
149 ms |
455740 KB |
Output is correct |
15 |
Correct |
209 ms |
550176 KB |
Output is correct |
16 |
Correct |
213 ms |
546120 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 |
3 ms |
20816 KB |
Output is correct |
5 |
Correct |
3 ms |
29204 KB |
Output is correct |
6 |
Correct |
3 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 |
4 ms |
29008 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 |
4 ms |
29008 KB |
Output is correct |
15 |
Correct |
3 ms |
29008 KB |
Output is correct |
16 |
Correct |
3 ms |
29112 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 |
115432 KB |
Output is correct |
20 |
Correct |
11 ms |
117520 KB |
Output is correct |
21 |
Correct |
12 ms |
115280 KB |
Output is correct |
22 |
Correct |
12 ms |
117496 KB |
Output is correct |
23 |
Correct |
11 ms |
115292 KB |
Output is correct |
24 |
Correct |
12 ms |
115280 KB |
Output is correct |
25 |
Correct |
11 ms |
117584 KB |
Output is correct |
26 |
Correct |
12 ms |
117520 KB |
Output is correct |
27 |
Correct |
11 ms |
117500 KB |
Output is correct |
28 |
Correct |
11 ms |
117344 KB |
Output is correct |
29 |
Correct |
11 ms |
117328 KB |
Output is correct |
30 |
Correct |
11 ms |
115280 KB |
Output is correct |
31 |
Correct |
11 ms |
115280 KB |
Output is correct |
32 |
Correct |
11 ms |
115280 KB |
Output is correct |
33 |
Correct |
12 ms |
115296 KB |
Output is correct |
34 |
Correct |
11 ms |
115280 KB |
Output is correct |
35 |
Correct |
11 ms |
113448 KB |
Output is correct |
36 |
Correct |
11 ms |
115296 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 |
12 ms |
115316 KB |
Output is correct |
42 |
Correct |
2 ms |
6480 KB |
Output is correct |
43 |
Correct |
204 ms |
514376 KB |
Output is correct |
44 |
Correct |
216 ms |
525896 KB |
Output is correct |
45 |
Correct |
210 ms |
527304 KB |
Output is correct |
46 |
Correct |
239 ms |
532808 KB |
Output is correct |
47 |
Correct |
221 ms |
528460 KB |
Output is correct |
48 |
Correct |
208 ms |
528968 KB |
Output is correct |
49 |
Correct |
213 ms |
523592 KB |
Output is correct |
50 |
Correct |
214 ms |
520364 KB |
Output is correct |
51 |
Correct |
230 ms |
536824 KB |
Output is correct |
52 |
Correct |
204 ms |
542024 KB |
Output is correct |
53 |
Correct |
48 ms |
338768 KB |
Output is correct |
54 |
Correct |
87 ms |
388936 KB |
Output is correct |
55 |
Correct |
149 ms |
455740 KB |
Output is correct |
56 |
Correct |
209 ms |
550176 KB |
Output is correct |
57 |
Correct |
213 ms |
546120 KB |
Output is correct |
58 |
Correct |
233 ms |
457444 KB |
Output is correct |
59 |
Correct |
239 ms |
471368 KB |
Output is correct |
60 |
Correct |
261 ms |
468960 KB |
Output is correct |
61 |
Correct |
231 ms |
451908 KB |
Output is correct |
62 |
Correct |
221 ms |
561568 KB |
Output is correct |
63 |
Correct |
213 ms |
539976 KB |
Output is correct |
64 |
Correct |
234 ms |
522972 KB |
Output is correct |
65 |
Correct |
256 ms |
481960 KB |
Output is correct |
66 |
Correct |
239 ms |
453448 KB |
Output is correct |
67 |
Correct |
222 ms |
426312 KB |
Output is correct |
68 |
Correct |
214 ms |
451148 KB |
Output is correct |
69 |
Correct |
213 ms |
438088 KB |
Output is correct |
70 |
Correct |
217 ms |
444972 KB |
Output is correct |
71 |
Correct |
218 ms |
472392 KB |
Output is correct |
72 |
Correct |
236 ms |
612940 KB |
Output is correct |
73 |
Correct |
35 ms |
308836 KB |
Output is correct |