//#pragma GCC optimize("O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 405;
int dp[N][N][4][2];
int cnt[3];
int n;
int suf[N][N][3];
void init(int q) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int t = 0; t < 4; t++) {
dp[i][j][t][q] = INT32_MAX;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
map<char, int> mp;
mp['R'] = 0;
mp['G'] = 1;
mp['Y'] = 2;
string s;
cin >> s;
vector<int> pos[3];
for (int i = 0; i < n; i++) {
cnt[mp[s[i]]]++;
pos[mp[s[i]]].push_back(i);
}
for (int i = 0; i < 3; i++) {
pos[i].push_back(-1);
reverse(pos[i].begin(), pos[i].end());
}
for (int t = 0; t < 3; t++) {
for (int j = 0; j < n; j++) {
for (int i = 1; i <= cnt[t]; i++) {
suf[i][j][t] = suf[i - 1][j][t] + (pos[t][i] < j);
}
}
}
init(0);
dp[cnt[0]][cnt[1]][3][0] = 0;
for (int i = 0; i < n; i++) {
int q = i & 1;
int w = q ^ 1;
init(w);
for (int j = 0; j <= cnt[0]; j++) {
for (int k = 0; k <= min(n - i - j, cnt[1]); k++) {
for (int t = 0; t < 4; t++) {
if (dp[j][k][t][q] == INT32_MAX) {
continue;
}
if (j > 0 && t != 0) {
dp[j - 1][k][0][w] = min(dp[j - 1][k][0][w], dp[j][k][t][q] + suf[k][pos[0][j]][1] + suf[n - i - j - k][pos[0][j]][2]);
}
if (k > 0 && t != 1) {
dp[j][k - 1][1][w] = min(dp[j][k - 1][1][w], dp[j][k][t][q] + suf[j][pos[1][k]][0] + suf[n - i - j - k][pos[1][k]][2]);
}
if (n - i - j - k > 0 && t != 2) {
dp[j][k][2][w] = min(dp[j][k][2][w], dp[j][k][t][q] + suf[j][pos[2][n - i - j - k]][0] + suf[k][pos[2][n - i - j - k]][1]);
}
}
}
}
}
int ans = INT32_MAX;
for (int i = 0; i < 4; i++) {
ans = min(ans, dp[0][0][i][n & 1]);
}
if (ans == INT32_MAX) {
cout << -1;
} else {
cout << ans;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5496 KB |
Output is correct |
2 |
Correct |
8 ms |
5496 KB |
Output is correct |
3 |
Correct |
7 ms |
5496 KB |
Output is correct |
4 |
Correct |
10 ms |
5496 KB |
Output is correct |
5 |
Correct |
13 ms |
5496 KB |
Output is correct |
6 |
Correct |
13 ms |
5496 KB |
Output is correct |
7 |
Correct |
12 ms |
5496 KB |
Output is correct |
8 |
Correct |
12 ms |
5496 KB |
Output is correct |
9 |
Correct |
13 ms |
5432 KB |
Output is correct |
10 |
Correct |
13 ms |
5496 KB |
Output is correct |
11 |
Correct |
13 ms |
5496 KB |
Output is correct |
12 |
Correct |
12 ms |
5496 KB |
Output is correct |
13 |
Correct |
13 ms |
5496 KB |
Output is correct |
14 |
Correct |
12 ms |
5496 KB |
Output is correct |
15 |
Correct |
12 ms |
5496 KB |
Output is correct |
16 |
Correct |
12 ms |
5496 KB |
Output is correct |
17 |
Correct |
11 ms |
5496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5496 KB |
Output is correct |
2 |
Correct |
8 ms |
5496 KB |
Output is correct |
3 |
Correct |
7 ms |
5496 KB |
Output is correct |
4 |
Correct |
10 ms |
5496 KB |
Output is correct |
5 |
Correct |
13 ms |
5496 KB |
Output is correct |
6 |
Correct |
13 ms |
5496 KB |
Output is correct |
7 |
Correct |
12 ms |
5496 KB |
Output is correct |
8 |
Correct |
12 ms |
5496 KB |
Output is correct |
9 |
Correct |
13 ms |
5432 KB |
Output is correct |
10 |
Correct |
13 ms |
5496 KB |
Output is correct |
11 |
Correct |
13 ms |
5496 KB |
Output is correct |
12 |
Correct |
12 ms |
5496 KB |
Output is correct |
13 |
Correct |
13 ms |
5496 KB |
Output is correct |
14 |
Correct |
12 ms |
5496 KB |
Output is correct |
15 |
Correct |
12 ms |
5496 KB |
Output is correct |
16 |
Correct |
12 ms |
5496 KB |
Output is correct |
17 |
Correct |
11 ms |
5496 KB |
Output is correct |
18 |
Correct |
28 ms |
5624 KB |
Output is correct |
19 |
Correct |
29 ms |
5624 KB |
Output is correct |
20 |
Correct |
29 ms |
5644 KB |
Output is correct |
21 |
Correct |
29 ms |
5628 KB |
Output is correct |
22 |
Correct |
29 ms |
5624 KB |
Output is correct |
23 |
Correct |
28 ms |
5624 KB |
Output is correct |
24 |
Correct |
28 ms |
5624 KB |
Output is correct |
25 |
Correct |
28 ms |
5624 KB |
Output is correct |
26 |
Correct |
29 ms |
5624 KB |
Output is correct |
27 |
Correct |
29 ms |
5624 KB |
Output is correct |
28 |
Correct |
28 ms |
5496 KB |
Output is correct |
29 |
Correct |
29 ms |
5624 KB |
Output is correct |
30 |
Correct |
28 ms |
5624 KB |
Output is correct |
31 |
Correct |
29 ms |
5624 KB |
Output is correct |
32 |
Correct |
29 ms |
5624 KB |
Output is correct |
33 |
Correct |
28 ms |
5624 KB |
Output is correct |
34 |
Correct |
28 ms |
5624 KB |
Output is correct |
35 |
Correct |
28 ms |
5624 KB |
Output is correct |
36 |
Correct |
28 ms |
5624 KB |
Output is correct |
37 |
Correct |
28 ms |
5624 KB |
Output is correct |
38 |
Correct |
31 ms |
5624 KB |
Output is correct |
39 |
Correct |
30 ms |
5752 KB |
Output is correct |
40 |
Correct |
28 ms |
5624 KB |
Output is correct |
41 |
Correct |
29 ms |
5768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5496 KB |
Output is correct |
2 |
Correct |
211 ms |
6396 KB |
Output is correct |
3 |
Correct |
209 ms |
6392 KB |
Output is correct |
4 |
Correct |
211 ms |
6516 KB |
Output is correct |
5 |
Correct |
211 ms |
6480 KB |
Output is correct |
6 |
Correct |
211 ms |
6476 KB |
Output is correct |
7 |
Correct |
212 ms |
6476 KB |
Output is correct |
8 |
Correct |
210 ms |
6472 KB |
Output is correct |
9 |
Correct |
209 ms |
6392 KB |
Output is correct |
10 |
Correct |
211 ms |
6476 KB |
Output is correct |
11 |
Correct |
211 ms |
6520 KB |
Output is correct |
12 |
Correct |
89 ms |
6012 KB |
Output is correct |
13 |
Correct |
129 ms |
6184 KB |
Output is correct |
14 |
Correct |
160 ms |
6392 KB |
Output is correct |
15 |
Correct |
212 ms |
6480 KB |
Output is correct |
16 |
Correct |
211 ms |
6480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5496 KB |
Output is correct |
2 |
Correct |
8 ms |
5496 KB |
Output is correct |
3 |
Correct |
7 ms |
5496 KB |
Output is correct |
4 |
Correct |
10 ms |
5496 KB |
Output is correct |
5 |
Correct |
13 ms |
5496 KB |
Output is correct |
6 |
Correct |
13 ms |
5496 KB |
Output is correct |
7 |
Correct |
12 ms |
5496 KB |
Output is correct |
8 |
Correct |
12 ms |
5496 KB |
Output is correct |
9 |
Correct |
13 ms |
5432 KB |
Output is correct |
10 |
Correct |
13 ms |
5496 KB |
Output is correct |
11 |
Correct |
13 ms |
5496 KB |
Output is correct |
12 |
Correct |
12 ms |
5496 KB |
Output is correct |
13 |
Correct |
13 ms |
5496 KB |
Output is correct |
14 |
Correct |
12 ms |
5496 KB |
Output is correct |
15 |
Correct |
12 ms |
5496 KB |
Output is correct |
16 |
Correct |
12 ms |
5496 KB |
Output is correct |
17 |
Correct |
11 ms |
5496 KB |
Output is correct |
18 |
Correct |
28 ms |
5624 KB |
Output is correct |
19 |
Correct |
29 ms |
5624 KB |
Output is correct |
20 |
Correct |
29 ms |
5644 KB |
Output is correct |
21 |
Correct |
29 ms |
5628 KB |
Output is correct |
22 |
Correct |
29 ms |
5624 KB |
Output is correct |
23 |
Correct |
28 ms |
5624 KB |
Output is correct |
24 |
Correct |
28 ms |
5624 KB |
Output is correct |
25 |
Correct |
28 ms |
5624 KB |
Output is correct |
26 |
Correct |
29 ms |
5624 KB |
Output is correct |
27 |
Correct |
29 ms |
5624 KB |
Output is correct |
28 |
Correct |
28 ms |
5496 KB |
Output is correct |
29 |
Correct |
29 ms |
5624 KB |
Output is correct |
30 |
Correct |
28 ms |
5624 KB |
Output is correct |
31 |
Correct |
29 ms |
5624 KB |
Output is correct |
32 |
Correct |
29 ms |
5624 KB |
Output is correct |
33 |
Correct |
28 ms |
5624 KB |
Output is correct |
34 |
Correct |
28 ms |
5624 KB |
Output is correct |
35 |
Correct |
28 ms |
5624 KB |
Output is correct |
36 |
Correct |
28 ms |
5624 KB |
Output is correct |
37 |
Correct |
28 ms |
5624 KB |
Output is correct |
38 |
Correct |
31 ms |
5624 KB |
Output is correct |
39 |
Correct |
30 ms |
5752 KB |
Output is correct |
40 |
Correct |
28 ms |
5624 KB |
Output is correct |
41 |
Correct |
29 ms |
5768 KB |
Output is correct |
42 |
Correct |
7 ms |
5496 KB |
Output is correct |
43 |
Correct |
211 ms |
6396 KB |
Output is correct |
44 |
Correct |
209 ms |
6392 KB |
Output is correct |
45 |
Correct |
211 ms |
6516 KB |
Output is correct |
46 |
Correct |
211 ms |
6480 KB |
Output is correct |
47 |
Correct |
211 ms |
6476 KB |
Output is correct |
48 |
Correct |
212 ms |
6476 KB |
Output is correct |
49 |
Correct |
210 ms |
6472 KB |
Output is correct |
50 |
Correct |
209 ms |
6392 KB |
Output is correct |
51 |
Correct |
211 ms |
6476 KB |
Output is correct |
52 |
Correct |
211 ms |
6520 KB |
Output is correct |
53 |
Correct |
89 ms |
6012 KB |
Output is correct |
54 |
Correct |
129 ms |
6184 KB |
Output is correct |
55 |
Correct |
160 ms |
6392 KB |
Output is correct |
56 |
Correct |
212 ms |
6480 KB |
Output is correct |
57 |
Correct |
211 ms |
6480 KB |
Output is correct |
58 |
Correct |
205 ms |
6264 KB |
Output is correct |
59 |
Correct |
211 ms |
6140 KB |
Output is correct |
60 |
Correct |
205 ms |
6136 KB |
Output is correct |
61 |
Correct |
208 ms |
6172 KB |
Output is correct |
62 |
Correct |
210 ms |
6584 KB |
Output is correct |
63 |
Correct |
212 ms |
6392 KB |
Output is correct |
64 |
Correct |
210 ms |
6416 KB |
Output is correct |
65 |
Correct |
210 ms |
6392 KB |
Output is correct |
66 |
Correct |
204 ms |
6216 KB |
Output is correct |
67 |
Correct |
204 ms |
6288 KB |
Output is correct |
68 |
Correct |
205 ms |
6136 KB |
Output is correct |
69 |
Correct |
246 ms |
6172 KB |
Output is correct |
70 |
Correct |
214 ms |
6236 KB |
Output is correct |
71 |
Correct |
210 ms |
6264 KB |
Output is correct |
72 |
Correct |
179 ms |
6904 KB |
Output is correct |
73 |
Correct |
155 ms |
6912 KB |
Output is correct |