# include <bits/stdc++.h>
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)1e9 + 0;
const int mod = (int)1e9 + 7;
const bool T = 0;
int n;
string s;
int dp[maxn][maxn][maxn][3];
int pref[3][maxn];
vector <int> g[3];
int d[3];
int 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 (int i = 0; i < n; ++i) {
for (int j = 0; j < 3; ++j) {
pref[j][i + 1] = pref[j][i];
}
int x = f(s[i]);
pref[x][i + 1]++;
g[x].push_back(i + 1);
}
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 (int i = 0; i < n; ++i) {
for (int x = 0; x <= i; ++x) {
for (int y = 0; y <= i; ++y) {
if (x + y > i) continue;
int z = i - x - y;
d[0] = x, d[1] = y, d[2] = z;
bool boo = 1;
for (int j = 0; j < 3; ++j) {
if (d[j] > pref[j][n]) {
boo = 0;
}
}
if (!boo) continue;
for (int prv = 0; prv < 3; ++prv) {
for (int cur = 0; cur < 3; ++cur) {
if (prv == cur || d[cur] == pref[cur][n]) continue;
// cout << "continue everything" << nl;
// continue;
int othr = prv ^ cur ^ 3;
assert(othr >= 0 && othr < 3 && othr != prv && othr != cur);
int 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 = lim;
for (int 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 == lim ? -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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
461 ms |
774472 KB |
Output is correct |
2 |
Correct |
434 ms |
774472 KB |
Output is correct |
3 |
Correct |
435 ms |
774648 KB |
Output is correct |
4 |
Correct |
415 ms |
774472 KB |
Output is correct |
5 |
Correct |
406 ms |
774480 KB |
Output is correct |
6 |
Correct |
413 ms |
774472 KB |
Output is correct |
7 |
Correct |
421 ms |
774676 KB |
Output is correct |
8 |
Correct |
417 ms |
774620 KB |
Output is correct |
9 |
Correct |
427 ms |
774596 KB |
Output is correct |
10 |
Correct |
452 ms |
774480 KB |
Output is correct |
11 |
Correct |
458 ms |
774472 KB |
Output is correct |
12 |
Correct |
446 ms |
774628 KB |
Output is correct |
13 |
Correct |
448 ms |
774472 KB |
Output is correct |
14 |
Correct |
458 ms |
774476 KB |
Output is correct |
15 |
Correct |
443 ms |
774564 KB |
Output is correct |
16 |
Correct |
405 ms |
774472 KB |
Output is correct |
17 |
Correct |
409 ms |
774512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
461 ms |
774472 KB |
Output is correct |
2 |
Correct |
434 ms |
774472 KB |
Output is correct |
3 |
Correct |
435 ms |
774648 KB |
Output is correct |
4 |
Correct |
415 ms |
774472 KB |
Output is correct |
5 |
Correct |
406 ms |
774480 KB |
Output is correct |
6 |
Correct |
413 ms |
774472 KB |
Output is correct |
7 |
Correct |
421 ms |
774676 KB |
Output is correct |
8 |
Correct |
417 ms |
774620 KB |
Output is correct |
9 |
Correct |
427 ms |
774596 KB |
Output is correct |
10 |
Correct |
452 ms |
774480 KB |
Output is correct |
11 |
Correct |
458 ms |
774472 KB |
Output is correct |
12 |
Correct |
446 ms |
774628 KB |
Output is correct |
13 |
Correct |
448 ms |
774472 KB |
Output is correct |
14 |
Correct |
458 ms |
774476 KB |
Output is correct |
15 |
Correct |
443 ms |
774564 KB |
Output is correct |
16 |
Correct |
405 ms |
774472 KB |
Output is correct |
17 |
Correct |
409 ms |
774512 KB |
Output is correct |
18 |
Correct |
430 ms |
774464 KB |
Output is correct |
19 |
Correct |
418 ms |
774452 KB |
Output is correct |
20 |
Correct |
460 ms |
774632 KB |
Output is correct |
21 |
Correct |
458 ms |
774532 KB |
Output is correct |
22 |
Correct |
493 ms |
774676 KB |
Output is correct |
23 |
Correct |
472 ms |
774472 KB |
Output is correct |
24 |
Correct |
456 ms |
774608 KB |
Output is correct |
25 |
Correct |
456 ms |
774584 KB |
Output is correct |
26 |
Correct |
422 ms |
774508 KB |
Output is correct |
27 |
Correct |
414 ms |
774572 KB |
Output is correct |
28 |
Correct |
400 ms |
774468 KB |
Output is correct |
29 |
Correct |
421 ms |
774504 KB |
Output is correct |
30 |
Correct |
413 ms |
774472 KB |
Output is correct |
31 |
Correct |
415 ms |
774636 KB |
Output is correct |
32 |
Correct |
457 ms |
774472 KB |
Output is correct |
33 |
Correct |
446 ms |
774472 KB |
Output is correct |
34 |
Correct |
440 ms |
774472 KB |
Output is correct |
35 |
Correct |
454 ms |
774472 KB |
Output is correct |
36 |
Correct |
452 ms |
774472 KB |
Output is correct |
37 |
Correct |
470 ms |
774556 KB |
Output is correct |
38 |
Correct |
445 ms |
774656 KB |
Output is correct |
39 |
Correct |
446 ms |
774584 KB |
Output is correct |
40 |
Correct |
446 ms |
774472 KB |
Output is correct |
41 |
Correct |
473 ms |
774568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
443 ms |
774472 KB |
Output is correct |
2 |
Correct |
491 ms |
774504 KB |
Output is correct |
3 |
Correct |
486 ms |
774472 KB |
Output is correct |
4 |
Correct |
482 ms |
774608 KB |
Output is correct |
5 |
Correct |
441 ms |
774472 KB |
Output is correct |
6 |
Correct |
469 ms |
774580 KB |
Output is correct |
7 |
Correct |
451 ms |
774616 KB |
Output is correct |
8 |
Correct |
428 ms |
774696 KB |
Output is correct |
9 |
Correct |
433 ms |
774472 KB |
Output is correct |
10 |
Correct |
430 ms |
774472 KB |
Output is correct |
11 |
Correct |
434 ms |
774472 KB |
Output is correct |
12 |
Correct |
405 ms |
774696 KB |
Output is correct |
13 |
Correct |
429 ms |
774536 KB |
Output is correct |
14 |
Correct |
465 ms |
774472 KB |
Output is correct |
15 |
Execution timed out |
501 ms |
774532 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
461 ms |
774472 KB |
Output is correct |
2 |
Correct |
434 ms |
774472 KB |
Output is correct |
3 |
Correct |
435 ms |
774648 KB |
Output is correct |
4 |
Correct |
415 ms |
774472 KB |
Output is correct |
5 |
Correct |
406 ms |
774480 KB |
Output is correct |
6 |
Correct |
413 ms |
774472 KB |
Output is correct |
7 |
Correct |
421 ms |
774676 KB |
Output is correct |
8 |
Correct |
417 ms |
774620 KB |
Output is correct |
9 |
Correct |
427 ms |
774596 KB |
Output is correct |
10 |
Correct |
452 ms |
774480 KB |
Output is correct |
11 |
Correct |
458 ms |
774472 KB |
Output is correct |
12 |
Correct |
446 ms |
774628 KB |
Output is correct |
13 |
Correct |
448 ms |
774472 KB |
Output is correct |
14 |
Correct |
458 ms |
774476 KB |
Output is correct |
15 |
Correct |
443 ms |
774564 KB |
Output is correct |
16 |
Correct |
405 ms |
774472 KB |
Output is correct |
17 |
Correct |
409 ms |
774512 KB |
Output is correct |
18 |
Correct |
430 ms |
774464 KB |
Output is correct |
19 |
Correct |
418 ms |
774452 KB |
Output is correct |
20 |
Correct |
460 ms |
774632 KB |
Output is correct |
21 |
Correct |
458 ms |
774532 KB |
Output is correct |
22 |
Correct |
493 ms |
774676 KB |
Output is correct |
23 |
Correct |
472 ms |
774472 KB |
Output is correct |
24 |
Correct |
456 ms |
774608 KB |
Output is correct |
25 |
Correct |
456 ms |
774584 KB |
Output is correct |
26 |
Correct |
422 ms |
774508 KB |
Output is correct |
27 |
Correct |
414 ms |
774572 KB |
Output is correct |
28 |
Correct |
400 ms |
774468 KB |
Output is correct |
29 |
Correct |
421 ms |
774504 KB |
Output is correct |
30 |
Correct |
413 ms |
774472 KB |
Output is correct |
31 |
Correct |
415 ms |
774636 KB |
Output is correct |
32 |
Correct |
457 ms |
774472 KB |
Output is correct |
33 |
Correct |
446 ms |
774472 KB |
Output is correct |
34 |
Correct |
440 ms |
774472 KB |
Output is correct |
35 |
Correct |
454 ms |
774472 KB |
Output is correct |
36 |
Correct |
452 ms |
774472 KB |
Output is correct |
37 |
Correct |
470 ms |
774556 KB |
Output is correct |
38 |
Correct |
445 ms |
774656 KB |
Output is correct |
39 |
Correct |
446 ms |
774584 KB |
Output is correct |
40 |
Correct |
446 ms |
774472 KB |
Output is correct |
41 |
Correct |
473 ms |
774568 KB |
Output is correct |
42 |
Correct |
443 ms |
774472 KB |
Output is correct |
43 |
Correct |
491 ms |
774504 KB |
Output is correct |
44 |
Correct |
486 ms |
774472 KB |
Output is correct |
45 |
Correct |
482 ms |
774608 KB |
Output is correct |
46 |
Correct |
441 ms |
774472 KB |
Output is correct |
47 |
Correct |
469 ms |
774580 KB |
Output is correct |
48 |
Correct |
451 ms |
774616 KB |
Output is correct |
49 |
Correct |
428 ms |
774696 KB |
Output is correct |
50 |
Correct |
433 ms |
774472 KB |
Output is correct |
51 |
Correct |
430 ms |
774472 KB |
Output is correct |
52 |
Correct |
434 ms |
774472 KB |
Output is correct |
53 |
Correct |
405 ms |
774696 KB |
Output is correct |
54 |
Correct |
429 ms |
774536 KB |
Output is correct |
55 |
Correct |
465 ms |
774472 KB |
Output is correct |
56 |
Execution timed out |
501 ms |
774532 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |