#include <bits/stdc++.h>
#define all(i) (i).begin(), (i).end()
using namespace std;
template<typename T1, typename ...T2>
void debug(bool _split, T1 i, T2 ...j) {
if (_split)
cerr << ", ";
cerr << i;
debug(true, j...);
}
#define debug(args...) cout << "Line" << __LINE__ << " : [" << #args << << "] is [" << debug(false, args) << "]" << endl;
typedef pair<int, int> pi;
typedef long long ll;
const int inf = 0x3f3f3f3f, lg = 20;
const ll mod = 1e9 + 7, INF = 0x3f3f3f3f;
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
const int N = 405;
int n;
string s;
cin >> n >> s;
int cnt[3];
cnt[0] = count(all(s), 'R');
cnt[1] = count(all(s), 'G');
cnt[2] = count(all(s), 'Y');
vector<int> pos[3];
int pre[n][3];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 3; ++j)
pre[i][j] = i ? pre[i - 1][j] : 0;
if (s[i] == 'R')
pos[0].push_back(i), ++pre[i][0];
else if (s[i] == 'G')
pos[1].push_back(i), ++pre[i][1];
else
pos[2].push_back(i), ++pre[i][2];
}
int dp[2][N][N][3];
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
for (int k = 0; k < 3; ++k)
dp[0][i][j][k] = inf;
for (int i = 0; i < 3; ++i)
dp[0][0][0][i] = 0;
for (int _i = 0; _i <= cnt[0]; ++_i) {
int x = _i & 1, y = !x, i[3];
i[0] = _i;
for (i[1] = 0; i[1] < N; ++i[1])
for (i[2] = 0; i[2] < N; ++i[2])
for (int j = 0; j < 3; ++j)
dp[y][i[1]][i[2]][j] = inf;
for (i[1] = 0; i[1] <= cnt[1]; ++i[1])
for (i[2] = 0; i[2] <= cnt[2]; ++i[2])
for (int j = 0; j < 3; ++j)
for (int k = 0; k < 3; ++k)
if (j != k && i[k] < cnt[k]) {
int tmp = dp[x][i[1]][i[2]][j];
++i[k], y = i[0] & 1;
for (int l = 0; l < 3; ++l)
tmp += max(0, i[l] - pre[pos[k][i[k] - 1]][l]);
dp[y][i[1]][i[2]][k] = min(dp[y][i[1]][i[2]][k], tmp);
--i[k];
}
}
int ans = inf;
for (int i = 0; i < 3; ++i)
ans = min(ans, dp[cnt[0] & 1][cnt[1]][cnt[2]][i]);
cout << (ans < inf ? ans : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4044 KB |
Output is correct |
2 |
Correct |
3 ms |
4160 KB |
Output is correct |
3 |
Correct |
3 ms |
4044 KB |
Output is correct |
4 |
Correct |
4 ms |
4044 KB |
Output is correct |
5 |
Correct |
5 ms |
4136 KB |
Output is correct |
6 |
Correct |
5 ms |
4164 KB |
Output is correct |
7 |
Correct |
4 ms |
4156 KB |
Output is correct |
8 |
Correct |
3 ms |
4044 KB |
Output is correct |
9 |
Correct |
3 ms |
4044 KB |
Output is correct |
10 |
Correct |
5 ms |
4112 KB |
Output is correct |
11 |
Correct |
3 ms |
4044 KB |
Output is correct |
12 |
Correct |
4 ms |
4160 KB |
Output is correct |
13 |
Correct |
4 ms |
4136 KB |
Output is correct |
14 |
Correct |
4 ms |
4160 KB |
Output is correct |
15 |
Correct |
3 ms |
4156 KB |
Output is correct |
16 |
Correct |
6 ms |
4044 KB |
Output is correct |
17 |
Correct |
3 ms |
4136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4044 KB |
Output is correct |
2 |
Correct |
3 ms |
4160 KB |
Output is correct |
3 |
Correct |
3 ms |
4044 KB |
Output is correct |
4 |
Correct |
4 ms |
4044 KB |
Output is correct |
5 |
Correct |
5 ms |
4136 KB |
Output is correct |
6 |
Correct |
5 ms |
4164 KB |
Output is correct |
7 |
Correct |
4 ms |
4156 KB |
Output is correct |
8 |
Correct |
3 ms |
4044 KB |
Output is correct |
9 |
Correct |
3 ms |
4044 KB |
Output is correct |
10 |
Correct |
5 ms |
4112 KB |
Output is correct |
11 |
Correct |
3 ms |
4044 KB |
Output is correct |
12 |
Correct |
4 ms |
4160 KB |
Output is correct |
13 |
Correct |
4 ms |
4136 KB |
Output is correct |
14 |
Correct |
4 ms |
4160 KB |
Output is correct |
15 |
Correct |
3 ms |
4156 KB |
Output is correct |
16 |
Correct |
6 ms |
4044 KB |
Output is correct |
17 |
Correct |
3 ms |
4136 KB |
Output is correct |
18 |
Correct |
8 ms |
4044 KB |
Output is correct |
19 |
Correct |
6 ms |
4156 KB |
Output is correct |
20 |
Correct |
7 ms |
4044 KB |
Output is correct |
21 |
Correct |
6 ms |
4044 KB |
Output is correct |
22 |
Correct |
8 ms |
4044 KB |
Output is correct |
23 |
Correct |
7 ms |
4284 KB |
Output is correct |
24 |
Correct |
7 ms |
4160 KB |
Output is correct |
25 |
Correct |
9 ms |
4044 KB |
Output is correct |
26 |
Correct |
8 ms |
4044 KB |
Output is correct |
27 |
Correct |
10 ms |
4156 KB |
Output is correct |
28 |
Correct |
7 ms |
4044 KB |
Output is correct |
29 |
Correct |
10 ms |
4160 KB |
Output is correct |
30 |
Correct |
9 ms |
4164 KB |
Output is correct |
31 |
Correct |
6 ms |
4044 KB |
Output is correct |
32 |
Correct |
8 ms |
4160 KB |
Output is correct |
33 |
Correct |
7 ms |
4044 KB |
Output is correct |
34 |
Correct |
7 ms |
4044 KB |
Output is correct |
35 |
Correct |
7 ms |
4164 KB |
Output is correct |
36 |
Correct |
6 ms |
4044 KB |
Output is correct |
37 |
Correct |
5 ms |
4044 KB |
Output is correct |
38 |
Correct |
5 ms |
4044 KB |
Output is correct |
39 |
Correct |
10 ms |
4044 KB |
Output is correct |
40 |
Correct |
3 ms |
4156 KB |
Output is correct |
41 |
Correct |
4 ms |
4044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4044 KB |
Output is correct |
2 |
Correct |
41 ms |
4164 KB |
Output is correct |
3 |
Correct |
41 ms |
4156 KB |
Output is correct |
4 |
Correct |
55 ms |
4148 KB |
Output is correct |
5 |
Correct |
37 ms |
4144 KB |
Output is correct |
6 |
Correct |
40 ms |
4044 KB |
Output is correct |
7 |
Correct |
41 ms |
4044 KB |
Output is correct |
8 |
Correct |
43 ms |
4044 KB |
Output is correct |
9 |
Correct |
49 ms |
4044 KB |
Output is correct |
10 |
Correct |
42 ms |
4156 KB |
Output is correct |
11 |
Correct |
38 ms |
4044 KB |
Output is correct |
12 |
Correct |
21 ms |
4044 KB |
Output is correct |
13 |
Correct |
28 ms |
4156 KB |
Output is correct |
14 |
Correct |
39 ms |
4044 KB |
Output is correct |
15 |
Correct |
37 ms |
4044 KB |
Output is correct |
16 |
Correct |
45 ms |
4144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4044 KB |
Output is correct |
2 |
Correct |
3 ms |
4160 KB |
Output is correct |
3 |
Correct |
3 ms |
4044 KB |
Output is correct |
4 |
Correct |
4 ms |
4044 KB |
Output is correct |
5 |
Correct |
5 ms |
4136 KB |
Output is correct |
6 |
Correct |
5 ms |
4164 KB |
Output is correct |
7 |
Correct |
4 ms |
4156 KB |
Output is correct |
8 |
Correct |
3 ms |
4044 KB |
Output is correct |
9 |
Correct |
3 ms |
4044 KB |
Output is correct |
10 |
Correct |
5 ms |
4112 KB |
Output is correct |
11 |
Correct |
3 ms |
4044 KB |
Output is correct |
12 |
Correct |
4 ms |
4160 KB |
Output is correct |
13 |
Correct |
4 ms |
4136 KB |
Output is correct |
14 |
Correct |
4 ms |
4160 KB |
Output is correct |
15 |
Correct |
3 ms |
4156 KB |
Output is correct |
16 |
Correct |
6 ms |
4044 KB |
Output is correct |
17 |
Correct |
3 ms |
4136 KB |
Output is correct |
18 |
Correct |
8 ms |
4044 KB |
Output is correct |
19 |
Correct |
6 ms |
4156 KB |
Output is correct |
20 |
Correct |
7 ms |
4044 KB |
Output is correct |
21 |
Correct |
6 ms |
4044 KB |
Output is correct |
22 |
Correct |
8 ms |
4044 KB |
Output is correct |
23 |
Correct |
7 ms |
4284 KB |
Output is correct |
24 |
Correct |
7 ms |
4160 KB |
Output is correct |
25 |
Correct |
9 ms |
4044 KB |
Output is correct |
26 |
Correct |
8 ms |
4044 KB |
Output is correct |
27 |
Correct |
10 ms |
4156 KB |
Output is correct |
28 |
Correct |
7 ms |
4044 KB |
Output is correct |
29 |
Correct |
10 ms |
4160 KB |
Output is correct |
30 |
Correct |
9 ms |
4164 KB |
Output is correct |
31 |
Correct |
6 ms |
4044 KB |
Output is correct |
32 |
Correct |
8 ms |
4160 KB |
Output is correct |
33 |
Correct |
7 ms |
4044 KB |
Output is correct |
34 |
Correct |
7 ms |
4044 KB |
Output is correct |
35 |
Correct |
7 ms |
4164 KB |
Output is correct |
36 |
Correct |
6 ms |
4044 KB |
Output is correct |
37 |
Correct |
5 ms |
4044 KB |
Output is correct |
38 |
Correct |
5 ms |
4044 KB |
Output is correct |
39 |
Correct |
10 ms |
4044 KB |
Output is correct |
40 |
Correct |
3 ms |
4156 KB |
Output is correct |
41 |
Correct |
4 ms |
4044 KB |
Output is correct |
42 |
Correct |
3 ms |
4044 KB |
Output is correct |
43 |
Correct |
41 ms |
4164 KB |
Output is correct |
44 |
Correct |
41 ms |
4156 KB |
Output is correct |
45 |
Correct |
55 ms |
4148 KB |
Output is correct |
46 |
Correct |
37 ms |
4144 KB |
Output is correct |
47 |
Correct |
40 ms |
4044 KB |
Output is correct |
48 |
Correct |
41 ms |
4044 KB |
Output is correct |
49 |
Correct |
43 ms |
4044 KB |
Output is correct |
50 |
Correct |
49 ms |
4044 KB |
Output is correct |
51 |
Correct |
42 ms |
4156 KB |
Output is correct |
52 |
Correct |
38 ms |
4044 KB |
Output is correct |
53 |
Correct |
21 ms |
4044 KB |
Output is correct |
54 |
Correct |
28 ms |
4156 KB |
Output is correct |
55 |
Correct |
39 ms |
4044 KB |
Output is correct |
56 |
Correct |
37 ms |
4044 KB |
Output is correct |
57 |
Correct |
45 ms |
4144 KB |
Output is correct |
58 |
Correct |
174 ms |
4164 KB |
Output is correct |
59 |
Correct |
173 ms |
4148 KB |
Output is correct |
60 |
Correct |
187 ms |
4144 KB |
Output is correct |
61 |
Correct |
196 ms |
4172 KB |
Output is correct |
62 |
Correct |
59 ms |
4172 KB |
Output is correct |
63 |
Correct |
62 ms |
4160 KB |
Output is correct |
64 |
Correct |
107 ms |
4164 KB |
Output is correct |
65 |
Correct |
147 ms |
4148 KB |
Output is correct |
66 |
Correct |
172 ms |
4156 KB |
Output is correct |
67 |
Correct |
161 ms |
4172 KB |
Output is correct |
68 |
Correct |
162 ms |
4144 KB |
Output is correct |
69 |
Correct |
187 ms |
4044 KB |
Output is correct |
70 |
Correct |
172 ms |
4156 KB |
Output is correct |
71 |
Correct |
180 ms |
4164 KB |
Output is correct |
72 |
Correct |
95 ms |
4160 KB |
Output is correct |
73 |
Correct |
13 ms |
4156 KB |
Output is correct |