#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define all(a) begin(a), end(a)
#define sz(a) ((int) (a).size())
using ll = long long;
using ld = long double;
const int N = 405;
const int INF = 0x3f3f3f3f;
int n;
vector<int> v[3];
int invs[3][3][N][N];
int dp[N][N][N][3];
void upd(int &a, int b) {
a = min(a, b);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> n >> s;
for (int i = 0; i < n; ++i) {
if (s[i] == 'R') v[0].push_back(i);
if (s[i] == 'G') v[1].push_back(i);
if (s[i] == 'Y') v[2].push_back(i);
}
for (int c1 = 0; c1 < 3; ++c1) {
for (int c2 = 0; c2 < 3; ++c2) {
if (c1 == c2) continue;
for (int i = 0; i < sz(v[c1]); ++i) {
int cnt = 0;
for (int j = 0; j <= sz(v[c2]); ++j) {
invs[c1][c2][i][j] = cnt;
if (j < sz(v[c2]) && v[c2][j] > v[c1][i]) {
++cnt;
}
}
}
}
}
memset(dp, 0x3f, sizeof(dp));
if (sz(v[0]) > 0) {
dp[1][0][0][0] = 0;
}
if (sz(v[1]) > 0) {
dp[0][1][0][1] = 0;
}
if (sz(v[2]) > 0) {
dp[0][0][1][2] = 0;
}
for (int i = 0; i <= sz(v[0]); ++i) {
for (int j = 0; j <= sz(v[1]); ++j) {
for (int k = 0; k <= sz(v[2]); ++k) {
for (int f = 0; f < 3; ++f) {
if (dp[i][j][k][f] == INF) continue;
if (i < sz(v[0]) && f != 0) {
upd(dp[i + 1][j][k][0], dp[i][j][k][f] + invs[0][1][i][j] + invs[0][2][i][k]);
}
if (j < sz(v[1]) && f != 1) {
upd(dp[i][j + 1][k][1], dp[i][j][k][f] + invs[1][0][j][i] + invs[1][2][j][k]);
}
if (k < sz(v[2]) && f != 2) {
upd(dp[i][j][k + 1][2], dp[i][j][k][f] + invs[2][0][k][i] + invs[2][1][k][j]);
}
}
}
}
}
int ans = INF;
for (int i = 0; i < 3; ++i) {
ans = min(ans, dp[sz(v[0])][sz(v[1])][sz(v[2])][i]);
}
cout << (ans == INF ? -1 : ans) << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
447 ms |
780232 KB |
Output is correct |
2 |
Correct |
304 ms |
780224 KB |
Output is correct |
3 |
Correct |
303 ms |
780216 KB |
Output is correct |
4 |
Correct |
293 ms |
780256 KB |
Output is correct |
5 |
Correct |
318 ms |
780328 KB |
Output is correct |
6 |
Correct |
295 ms |
780340 KB |
Output is correct |
7 |
Correct |
292 ms |
780352 KB |
Output is correct |
8 |
Correct |
298 ms |
780432 KB |
Output is correct |
9 |
Correct |
294 ms |
780468 KB |
Output is correct |
10 |
Correct |
297 ms |
780372 KB |
Output is correct |
11 |
Correct |
294 ms |
780268 KB |
Output is correct |
12 |
Correct |
300 ms |
780288 KB |
Output is correct |
13 |
Correct |
304 ms |
780324 KB |
Output is correct |
14 |
Correct |
294 ms |
780252 KB |
Output is correct |
15 |
Correct |
297 ms |
780288 KB |
Output is correct |
16 |
Correct |
289 ms |
780400 KB |
Output is correct |
17 |
Correct |
290 ms |
780320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
447 ms |
780232 KB |
Output is correct |
2 |
Correct |
304 ms |
780224 KB |
Output is correct |
3 |
Correct |
303 ms |
780216 KB |
Output is correct |
4 |
Correct |
293 ms |
780256 KB |
Output is correct |
5 |
Correct |
318 ms |
780328 KB |
Output is correct |
6 |
Correct |
295 ms |
780340 KB |
Output is correct |
7 |
Correct |
292 ms |
780352 KB |
Output is correct |
8 |
Correct |
298 ms |
780432 KB |
Output is correct |
9 |
Correct |
294 ms |
780468 KB |
Output is correct |
10 |
Correct |
297 ms |
780372 KB |
Output is correct |
11 |
Correct |
294 ms |
780268 KB |
Output is correct |
12 |
Correct |
300 ms |
780288 KB |
Output is correct |
13 |
Correct |
304 ms |
780324 KB |
Output is correct |
14 |
Correct |
294 ms |
780252 KB |
Output is correct |
15 |
Correct |
297 ms |
780288 KB |
Output is correct |
16 |
Correct |
289 ms |
780400 KB |
Output is correct |
17 |
Correct |
290 ms |
780320 KB |
Output is correct |
18 |
Correct |
305 ms |
780436 KB |
Output is correct |
19 |
Correct |
290 ms |
780520 KB |
Output is correct |
20 |
Correct |
303 ms |
780492 KB |
Output is correct |
21 |
Correct |
299 ms |
780492 KB |
Output is correct |
22 |
Correct |
294 ms |
780424 KB |
Output is correct |
23 |
Correct |
329 ms |
780540 KB |
Output is correct |
24 |
Correct |
299 ms |
780444 KB |
Output is correct |
25 |
Correct |
299 ms |
780480 KB |
Output is correct |
26 |
Correct |
303 ms |
780520 KB |
Output is correct |
27 |
Correct |
293 ms |
780452 KB |
Output is correct |
28 |
Correct |
331 ms |
780444 KB |
Output is correct |
29 |
Correct |
290 ms |
780408 KB |
Output is correct |
30 |
Correct |
295 ms |
780548 KB |
Output is correct |
31 |
Correct |
310 ms |
780436 KB |
Output is correct |
32 |
Correct |
291 ms |
780496 KB |
Output is correct |
33 |
Correct |
295 ms |
780416 KB |
Output is correct |
34 |
Correct |
303 ms |
780448 KB |
Output is correct |
35 |
Correct |
299 ms |
780460 KB |
Output is correct |
36 |
Correct |
299 ms |
780508 KB |
Output is correct |
37 |
Correct |
291 ms |
780464 KB |
Output is correct |
38 |
Correct |
295 ms |
780476 KB |
Output is correct |
39 |
Correct |
304 ms |
780500 KB |
Output is correct |
40 |
Correct |
304 ms |
780452 KB |
Output is correct |
41 |
Correct |
297 ms |
780440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
303 ms |
780260 KB |
Output is correct |
2 |
Correct |
297 ms |
781488 KB |
Output is correct |
3 |
Correct |
295 ms |
781548 KB |
Output is correct |
4 |
Correct |
306 ms |
781652 KB |
Output is correct |
5 |
Correct |
292 ms |
781516 KB |
Output is correct |
6 |
Correct |
295 ms |
781640 KB |
Output is correct |
7 |
Correct |
300 ms |
781468 KB |
Output is correct |
8 |
Correct |
327 ms |
781644 KB |
Output is correct |
9 |
Correct |
309 ms |
781576 KB |
Output is correct |
10 |
Correct |
301 ms |
781552 KB |
Output is correct |
11 |
Correct |
298 ms |
781588 KB |
Output is correct |
12 |
Correct |
296 ms |
780972 KB |
Output is correct |
13 |
Correct |
294 ms |
781096 KB |
Output is correct |
14 |
Correct |
298 ms |
781360 KB |
Output is correct |
15 |
Correct |
296 ms |
781512 KB |
Output is correct |
16 |
Correct |
303 ms |
781532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
447 ms |
780232 KB |
Output is correct |
2 |
Correct |
304 ms |
780224 KB |
Output is correct |
3 |
Correct |
303 ms |
780216 KB |
Output is correct |
4 |
Correct |
293 ms |
780256 KB |
Output is correct |
5 |
Correct |
318 ms |
780328 KB |
Output is correct |
6 |
Correct |
295 ms |
780340 KB |
Output is correct |
7 |
Correct |
292 ms |
780352 KB |
Output is correct |
8 |
Correct |
298 ms |
780432 KB |
Output is correct |
9 |
Correct |
294 ms |
780468 KB |
Output is correct |
10 |
Correct |
297 ms |
780372 KB |
Output is correct |
11 |
Correct |
294 ms |
780268 KB |
Output is correct |
12 |
Correct |
300 ms |
780288 KB |
Output is correct |
13 |
Correct |
304 ms |
780324 KB |
Output is correct |
14 |
Correct |
294 ms |
780252 KB |
Output is correct |
15 |
Correct |
297 ms |
780288 KB |
Output is correct |
16 |
Correct |
289 ms |
780400 KB |
Output is correct |
17 |
Correct |
290 ms |
780320 KB |
Output is correct |
18 |
Correct |
305 ms |
780436 KB |
Output is correct |
19 |
Correct |
290 ms |
780520 KB |
Output is correct |
20 |
Correct |
303 ms |
780492 KB |
Output is correct |
21 |
Correct |
299 ms |
780492 KB |
Output is correct |
22 |
Correct |
294 ms |
780424 KB |
Output is correct |
23 |
Correct |
329 ms |
780540 KB |
Output is correct |
24 |
Correct |
299 ms |
780444 KB |
Output is correct |
25 |
Correct |
299 ms |
780480 KB |
Output is correct |
26 |
Correct |
303 ms |
780520 KB |
Output is correct |
27 |
Correct |
293 ms |
780452 KB |
Output is correct |
28 |
Correct |
331 ms |
780444 KB |
Output is correct |
29 |
Correct |
290 ms |
780408 KB |
Output is correct |
30 |
Correct |
295 ms |
780548 KB |
Output is correct |
31 |
Correct |
310 ms |
780436 KB |
Output is correct |
32 |
Correct |
291 ms |
780496 KB |
Output is correct |
33 |
Correct |
295 ms |
780416 KB |
Output is correct |
34 |
Correct |
303 ms |
780448 KB |
Output is correct |
35 |
Correct |
299 ms |
780460 KB |
Output is correct |
36 |
Correct |
299 ms |
780508 KB |
Output is correct |
37 |
Correct |
291 ms |
780464 KB |
Output is correct |
38 |
Correct |
295 ms |
780476 KB |
Output is correct |
39 |
Correct |
304 ms |
780500 KB |
Output is correct |
40 |
Correct |
304 ms |
780452 KB |
Output is correct |
41 |
Correct |
297 ms |
780440 KB |
Output is correct |
42 |
Correct |
303 ms |
780260 KB |
Output is correct |
43 |
Correct |
297 ms |
781488 KB |
Output is correct |
44 |
Correct |
295 ms |
781548 KB |
Output is correct |
45 |
Correct |
306 ms |
781652 KB |
Output is correct |
46 |
Correct |
292 ms |
781516 KB |
Output is correct |
47 |
Correct |
295 ms |
781640 KB |
Output is correct |
48 |
Correct |
300 ms |
781468 KB |
Output is correct |
49 |
Correct |
327 ms |
781644 KB |
Output is correct |
50 |
Correct |
309 ms |
781576 KB |
Output is correct |
51 |
Correct |
301 ms |
781552 KB |
Output is correct |
52 |
Correct |
298 ms |
781588 KB |
Output is correct |
53 |
Correct |
296 ms |
780972 KB |
Output is correct |
54 |
Correct |
294 ms |
781096 KB |
Output is correct |
55 |
Correct |
298 ms |
781360 KB |
Output is correct |
56 |
Correct |
296 ms |
781512 KB |
Output is correct |
57 |
Correct |
303 ms |
781532 KB |
Output is correct |
58 |
Correct |
313 ms |
781596 KB |
Output is correct |
59 |
Correct |
366 ms |
781572 KB |
Output is correct |
60 |
Correct |
311 ms |
781584 KB |
Output is correct |
61 |
Correct |
342 ms |
781548 KB |
Output is correct |
62 |
Correct |
300 ms |
781540 KB |
Output is correct |
63 |
Correct |
300 ms |
781508 KB |
Output is correct |
64 |
Correct |
308 ms |
781576 KB |
Output is correct |
65 |
Correct |
305 ms |
781556 KB |
Output is correct |
66 |
Correct |
331 ms |
781524 KB |
Output is correct |
67 |
Correct |
307 ms |
781516 KB |
Output is correct |
68 |
Correct |
319 ms |
781500 KB |
Output is correct |
69 |
Correct |
317 ms |
781576 KB |
Output is correct |
70 |
Correct |
311 ms |
781596 KB |
Output is correct |
71 |
Correct |
326 ms |
781504 KB |
Output is correct |
72 |
Correct |
297 ms |
781560 KB |
Output is correct |
73 |
Correct |
295 ms |
781540 KB |
Output is correct |