#include <bits/stdc++.h>
using namespace std;
#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(0);
using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using ld = long double;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const ll LINF = ll(4e15) + ll(2e15);
const double EPS = 1e-9;
static int LamyIsCute = []() {
EmiliaMyWife
return 48763;
}();
const int N = 403;
int dp[N][N][N][3], cnt[N][3];
signed main() {
int n;
cin >> n;
vector<int> arr(n);
{
string s;
cin >> s;
for(int i = 0; i < n; i++) {
if(s[i] == 'R')
arr[i] = 0;
if(s[i] == 'G')
arr[i] = 1;
if(s[i] == 'Y')
arr[i] = 2;
}
}
array<vector<int>, 3> pos;
for(int i = 0; i < n; i++) {
pos[arr[i]].push_back(i);
cnt[i][arr[i]]++;
if(i) {
for(int j = 0; j < 3; j++)
cnt[i][j] += cnt[i - 1][j];
}
}
int a = pos[0].size(), b = pos[1].size(), c = pos[2].size();
memset(dp, 0x3f, sizeof dp);
for(int i = 0; i < 3; i++)
dp[0][0][0][i] = 0;
for(int i = 0; i <= a; i++) {
for(int j = 0; j <= b; j++) {
for(int k = 0; k <= c; k++) {
for(int r = 0; r < 3; r++)
dp[i][j][k][r] = min(dp[i][j][k][r], INF);
if(i < a) {
int cost = max(0, cnt[pos[0][i]][1] - j) + max(0, cnt[pos[0][i]][2] - k);
dp[i + 1][j][k][0] = min(dp[i][j][k][1] + cost, dp[i][j][k][2] + cost);
}
if(j < b) {
int cost = max(0, cnt[pos[1][j]][0] - i) + max(0, cnt[pos[1][j]][2] - k);
dp[i][j + 1][k][1] = min(dp[i][j][k][0] + cost, dp[i][j][k][2] + cost);
}
if(k < c) {
int cost = max(0, cnt[pos[2][k]][0] - i) + max(0, cnt[pos[2][k]][1] - j);
dp[i][j][k + 1][2] = min(dp[i][j][k][0] + cost, dp[i][j][k][1] + cost);
}
}
}
}
int r = min({dp[a][b][c][0], dp[a][b][c][1], dp[a][b][c][2]});
if(r == INF)
cout << "-1\n";
else
cout << r << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
281 ms |
768800 KB |
Output is correct |
2 |
Correct |
276 ms |
768812 KB |
Output is correct |
3 |
Correct |
296 ms |
768780 KB |
Output is correct |
4 |
Correct |
283 ms |
768800 KB |
Output is correct |
5 |
Correct |
284 ms |
768676 KB |
Output is correct |
6 |
Correct |
310 ms |
768688 KB |
Output is correct |
7 |
Correct |
282 ms |
768708 KB |
Output is correct |
8 |
Correct |
286 ms |
768780 KB |
Output is correct |
9 |
Correct |
358 ms |
768736 KB |
Output is correct |
10 |
Correct |
276 ms |
768708 KB |
Output is correct |
11 |
Correct |
287 ms |
768708 KB |
Output is correct |
12 |
Correct |
335 ms |
768700 KB |
Output is correct |
13 |
Correct |
278 ms |
768712 KB |
Output is correct |
14 |
Correct |
320 ms |
768732 KB |
Output is correct |
15 |
Correct |
280 ms |
768788 KB |
Output is correct |
16 |
Correct |
281 ms |
768764 KB |
Output is correct |
17 |
Correct |
297 ms |
768712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
281 ms |
768800 KB |
Output is correct |
2 |
Correct |
276 ms |
768812 KB |
Output is correct |
3 |
Correct |
296 ms |
768780 KB |
Output is correct |
4 |
Correct |
283 ms |
768800 KB |
Output is correct |
5 |
Correct |
284 ms |
768676 KB |
Output is correct |
6 |
Correct |
310 ms |
768688 KB |
Output is correct |
7 |
Correct |
282 ms |
768708 KB |
Output is correct |
8 |
Correct |
286 ms |
768780 KB |
Output is correct |
9 |
Correct |
358 ms |
768736 KB |
Output is correct |
10 |
Correct |
276 ms |
768708 KB |
Output is correct |
11 |
Correct |
287 ms |
768708 KB |
Output is correct |
12 |
Correct |
335 ms |
768700 KB |
Output is correct |
13 |
Correct |
278 ms |
768712 KB |
Output is correct |
14 |
Correct |
320 ms |
768732 KB |
Output is correct |
15 |
Correct |
280 ms |
768788 KB |
Output is correct |
16 |
Correct |
281 ms |
768764 KB |
Output is correct |
17 |
Correct |
297 ms |
768712 KB |
Output is correct |
18 |
Correct |
278 ms |
768708 KB |
Output is correct |
19 |
Correct |
302 ms |
768820 KB |
Output is correct |
20 |
Correct |
275 ms |
768704 KB |
Output is correct |
21 |
Correct |
282 ms |
768788 KB |
Output is correct |
22 |
Correct |
315 ms |
768812 KB |
Output is correct |
23 |
Correct |
278 ms |
768676 KB |
Output is correct |
24 |
Correct |
355 ms |
768772 KB |
Output is correct |
25 |
Correct |
283 ms |
768708 KB |
Output is correct |
26 |
Correct |
295 ms |
768796 KB |
Output is correct |
27 |
Correct |
323 ms |
768808 KB |
Output is correct |
28 |
Correct |
286 ms |
768704 KB |
Output is correct |
29 |
Correct |
327 ms |
768800 KB |
Output is correct |
30 |
Correct |
290 ms |
768804 KB |
Output is correct |
31 |
Correct |
286 ms |
768716 KB |
Output is correct |
32 |
Correct |
308 ms |
768708 KB |
Output is correct |
33 |
Correct |
286 ms |
768708 KB |
Output is correct |
34 |
Correct |
309 ms |
768852 KB |
Output is correct |
35 |
Correct |
292 ms |
768780 KB |
Output is correct |
36 |
Correct |
294 ms |
768812 KB |
Output is correct |
37 |
Correct |
320 ms |
768764 KB |
Output is correct |
38 |
Correct |
300 ms |
768680 KB |
Output is correct |
39 |
Correct |
303 ms |
768700 KB |
Output is correct |
40 |
Correct |
288 ms |
768804 KB |
Output is correct |
41 |
Correct |
291 ms |
768732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
301 ms |
768800 KB |
Output is correct |
2 |
Correct |
359 ms |
768768 KB |
Output is correct |
3 |
Correct |
335 ms |
768764 KB |
Output is correct |
4 |
Correct |
297 ms |
768756 KB |
Output is correct |
5 |
Correct |
299 ms |
768888 KB |
Output is correct |
6 |
Correct |
300 ms |
768692 KB |
Output is correct |
7 |
Correct |
306 ms |
768724 KB |
Output is correct |
8 |
Correct |
281 ms |
768692 KB |
Output is correct |
9 |
Correct |
310 ms |
768696 KB |
Output is correct |
10 |
Correct |
283 ms |
768684 KB |
Output is correct |
11 |
Correct |
285 ms |
768756 KB |
Output is correct |
12 |
Correct |
294 ms |
768784 KB |
Output is correct |
13 |
Correct |
293 ms |
768716 KB |
Output is correct |
14 |
Correct |
281 ms |
768716 KB |
Output is correct |
15 |
Correct |
296 ms |
768708 KB |
Output is correct |
16 |
Correct |
281 ms |
768768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
281 ms |
768800 KB |
Output is correct |
2 |
Correct |
276 ms |
768812 KB |
Output is correct |
3 |
Correct |
296 ms |
768780 KB |
Output is correct |
4 |
Correct |
283 ms |
768800 KB |
Output is correct |
5 |
Correct |
284 ms |
768676 KB |
Output is correct |
6 |
Correct |
310 ms |
768688 KB |
Output is correct |
7 |
Correct |
282 ms |
768708 KB |
Output is correct |
8 |
Correct |
286 ms |
768780 KB |
Output is correct |
9 |
Correct |
358 ms |
768736 KB |
Output is correct |
10 |
Correct |
276 ms |
768708 KB |
Output is correct |
11 |
Correct |
287 ms |
768708 KB |
Output is correct |
12 |
Correct |
335 ms |
768700 KB |
Output is correct |
13 |
Correct |
278 ms |
768712 KB |
Output is correct |
14 |
Correct |
320 ms |
768732 KB |
Output is correct |
15 |
Correct |
280 ms |
768788 KB |
Output is correct |
16 |
Correct |
281 ms |
768764 KB |
Output is correct |
17 |
Correct |
297 ms |
768712 KB |
Output is correct |
18 |
Correct |
278 ms |
768708 KB |
Output is correct |
19 |
Correct |
302 ms |
768820 KB |
Output is correct |
20 |
Correct |
275 ms |
768704 KB |
Output is correct |
21 |
Correct |
282 ms |
768788 KB |
Output is correct |
22 |
Correct |
315 ms |
768812 KB |
Output is correct |
23 |
Correct |
278 ms |
768676 KB |
Output is correct |
24 |
Correct |
355 ms |
768772 KB |
Output is correct |
25 |
Correct |
283 ms |
768708 KB |
Output is correct |
26 |
Correct |
295 ms |
768796 KB |
Output is correct |
27 |
Correct |
323 ms |
768808 KB |
Output is correct |
28 |
Correct |
286 ms |
768704 KB |
Output is correct |
29 |
Correct |
327 ms |
768800 KB |
Output is correct |
30 |
Correct |
290 ms |
768804 KB |
Output is correct |
31 |
Correct |
286 ms |
768716 KB |
Output is correct |
32 |
Correct |
308 ms |
768708 KB |
Output is correct |
33 |
Correct |
286 ms |
768708 KB |
Output is correct |
34 |
Correct |
309 ms |
768852 KB |
Output is correct |
35 |
Correct |
292 ms |
768780 KB |
Output is correct |
36 |
Correct |
294 ms |
768812 KB |
Output is correct |
37 |
Correct |
320 ms |
768764 KB |
Output is correct |
38 |
Correct |
300 ms |
768680 KB |
Output is correct |
39 |
Correct |
303 ms |
768700 KB |
Output is correct |
40 |
Correct |
288 ms |
768804 KB |
Output is correct |
41 |
Correct |
291 ms |
768732 KB |
Output is correct |
42 |
Correct |
301 ms |
768800 KB |
Output is correct |
43 |
Correct |
359 ms |
768768 KB |
Output is correct |
44 |
Correct |
335 ms |
768764 KB |
Output is correct |
45 |
Correct |
297 ms |
768756 KB |
Output is correct |
46 |
Correct |
299 ms |
768888 KB |
Output is correct |
47 |
Correct |
300 ms |
768692 KB |
Output is correct |
48 |
Correct |
306 ms |
768724 KB |
Output is correct |
49 |
Correct |
281 ms |
768692 KB |
Output is correct |
50 |
Correct |
310 ms |
768696 KB |
Output is correct |
51 |
Correct |
283 ms |
768684 KB |
Output is correct |
52 |
Correct |
285 ms |
768756 KB |
Output is correct |
53 |
Correct |
294 ms |
768784 KB |
Output is correct |
54 |
Correct |
293 ms |
768716 KB |
Output is correct |
55 |
Correct |
281 ms |
768716 KB |
Output is correct |
56 |
Correct |
296 ms |
768708 KB |
Output is correct |
57 |
Correct |
281 ms |
768768 KB |
Output is correct |
58 |
Correct |
307 ms |
768736 KB |
Output is correct |
59 |
Correct |
317 ms |
768752 KB |
Output is correct |
60 |
Correct |
304 ms |
768728 KB |
Output is correct |
61 |
Correct |
323 ms |
768700 KB |
Output is correct |
62 |
Correct |
282 ms |
768800 KB |
Output is correct |
63 |
Correct |
323 ms |
768836 KB |
Output is correct |
64 |
Correct |
292 ms |
768708 KB |
Output is correct |
65 |
Correct |
311 ms |
768724 KB |
Output is correct |
66 |
Correct |
308 ms |
768784 KB |
Output is correct |
67 |
Correct |
337 ms |
768748 KB |
Output is correct |
68 |
Correct |
333 ms |
768896 KB |
Output is correct |
69 |
Correct |
309 ms |
768704 KB |
Output is correct |
70 |
Correct |
331 ms |
768804 KB |
Output is correct |
71 |
Correct |
317 ms |
768708 KB |
Output is correct |
72 |
Correct |
289 ms |
768688 KB |
Output is correct |
73 |
Correct |
308 ms |
768840 KB |
Output is correct |