# include <bits/stdc++.h>
#pragma optimize("g", on)
#pragma GCC optimize ("inline")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("03")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx2,mmx,fma,avx,tune=native")
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 <= min(i, pref[0][n]); ++x) {
for (int y = 0; y <= min(i - x, pref[1][n]); ++y) {
if (x + y > i) continue;
int z = i - x - y;
if (z > pref[2][n]) continue;
d[0] = x, d[1] = y, d[2] = z;
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;
}
Compilation message
joi2019_ho_t3.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
3 | #pragma optimize("g", on)
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
470 ms |
774592 KB |
Output is correct |
2 |
Correct |
427 ms |
774580 KB |
Output is correct |
3 |
Correct |
436 ms |
774684 KB |
Output is correct |
4 |
Correct |
391 ms |
774688 KB |
Output is correct |
5 |
Correct |
382 ms |
774476 KB |
Output is correct |
6 |
Correct |
395 ms |
774532 KB |
Output is correct |
7 |
Correct |
392 ms |
774688 KB |
Output is correct |
8 |
Correct |
423 ms |
774472 KB |
Output is correct |
9 |
Correct |
398 ms |
774708 KB |
Output is correct |
10 |
Correct |
401 ms |
774660 KB |
Output is correct |
11 |
Correct |
456 ms |
774472 KB |
Output is correct |
12 |
Correct |
447 ms |
774584 KB |
Output is correct |
13 |
Correct |
445 ms |
774516 KB |
Output is correct |
14 |
Correct |
450 ms |
774872 KB |
Output is correct |
15 |
Correct |
490 ms |
774472 KB |
Output is correct |
16 |
Correct |
458 ms |
774692 KB |
Output is correct |
17 |
Correct |
459 ms |
774480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
470 ms |
774592 KB |
Output is correct |
2 |
Correct |
427 ms |
774580 KB |
Output is correct |
3 |
Correct |
436 ms |
774684 KB |
Output is correct |
4 |
Correct |
391 ms |
774688 KB |
Output is correct |
5 |
Correct |
382 ms |
774476 KB |
Output is correct |
6 |
Correct |
395 ms |
774532 KB |
Output is correct |
7 |
Correct |
392 ms |
774688 KB |
Output is correct |
8 |
Correct |
423 ms |
774472 KB |
Output is correct |
9 |
Correct |
398 ms |
774708 KB |
Output is correct |
10 |
Correct |
401 ms |
774660 KB |
Output is correct |
11 |
Correct |
456 ms |
774472 KB |
Output is correct |
12 |
Correct |
447 ms |
774584 KB |
Output is correct |
13 |
Correct |
445 ms |
774516 KB |
Output is correct |
14 |
Correct |
450 ms |
774872 KB |
Output is correct |
15 |
Correct |
490 ms |
774472 KB |
Output is correct |
16 |
Correct |
458 ms |
774692 KB |
Output is correct |
17 |
Correct |
459 ms |
774480 KB |
Output is correct |
18 |
Correct |
457 ms |
774664 KB |
Output is correct |
19 |
Correct |
455 ms |
774472 KB |
Output is correct |
20 |
Correct |
460 ms |
774472 KB |
Output is correct |
21 |
Correct |
481 ms |
774596 KB |
Output is correct |
22 |
Correct |
472 ms |
774440 KB |
Output is correct |
23 |
Correct |
457 ms |
774548 KB |
Output is correct |
24 |
Correct |
472 ms |
774472 KB |
Output is correct |
25 |
Correct |
433 ms |
774492 KB |
Output is correct |
26 |
Correct |
418 ms |
774740 KB |
Output is correct |
27 |
Correct |
402 ms |
774464 KB |
Output is correct |
28 |
Correct |
418 ms |
774540 KB |
Output is correct |
29 |
Correct |
421 ms |
774528 KB |
Output is correct |
30 |
Correct |
415 ms |
774624 KB |
Output is correct |
31 |
Correct |
438 ms |
774456 KB |
Output is correct |
32 |
Correct |
433 ms |
774652 KB |
Output is correct |
33 |
Correct |
457 ms |
774472 KB |
Output is correct |
34 |
Correct |
462 ms |
774472 KB |
Output is correct |
35 |
Correct |
436 ms |
774536 KB |
Output is correct |
36 |
Correct |
441 ms |
774472 KB |
Output is correct |
37 |
Correct |
458 ms |
774472 KB |
Output is correct |
38 |
Correct |
456 ms |
774460 KB |
Output is correct |
39 |
Correct |
469 ms |
774468 KB |
Output is correct |
40 |
Correct |
478 ms |
774472 KB |
Output is correct |
41 |
Correct |
482 ms |
774480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
460 ms |
774472 KB |
Output is correct |
2 |
Correct |
489 ms |
774660 KB |
Output is correct |
3 |
Correct |
420 ms |
774528 KB |
Output is correct |
4 |
Correct |
420 ms |
774472 KB |
Output is correct |
5 |
Correct |
430 ms |
774616 KB |
Output is correct |
6 |
Correct |
417 ms |
774672 KB |
Output is correct |
7 |
Correct |
425 ms |
774456 KB |
Output is correct |
8 |
Correct |
421 ms |
774472 KB |
Output is correct |
9 |
Correct |
443 ms |
774472 KB |
Output is correct |
10 |
Correct |
462 ms |
774480 KB |
Output is correct |
11 |
Correct |
458 ms |
774576 KB |
Output is correct |
12 |
Correct |
458 ms |
774472 KB |
Output is correct |
13 |
Correct |
472 ms |
774472 KB |
Output is correct |
14 |
Correct |
467 ms |
774472 KB |
Output is correct |
15 |
Correct |
483 ms |
774472 KB |
Output is correct |
16 |
Correct |
485 ms |
774508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
470 ms |
774592 KB |
Output is correct |
2 |
Correct |
427 ms |
774580 KB |
Output is correct |
3 |
Correct |
436 ms |
774684 KB |
Output is correct |
4 |
Correct |
391 ms |
774688 KB |
Output is correct |
5 |
Correct |
382 ms |
774476 KB |
Output is correct |
6 |
Correct |
395 ms |
774532 KB |
Output is correct |
7 |
Correct |
392 ms |
774688 KB |
Output is correct |
8 |
Correct |
423 ms |
774472 KB |
Output is correct |
9 |
Correct |
398 ms |
774708 KB |
Output is correct |
10 |
Correct |
401 ms |
774660 KB |
Output is correct |
11 |
Correct |
456 ms |
774472 KB |
Output is correct |
12 |
Correct |
447 ms |
774584 KB |
Output is correct |
13 |
Correct |
445 ms |
774516 KB |
Output is correct |
14 |
Correct |
450 ms |
774872 KB |
Output is correct |
15 |
Correct |
490 ms |
774472 KB |
Output is correct |
16 |
Correct |
458 ms |
774692 KB |
Output is correct |
17 |
Correct |
459 ms |
774480 KB |
Output is correct |
18 |
Correct |
457 ms |
774664 KB |
Output is correct |
19 |
Correct |
455 ms |
774472 KB |
Output is correct |
20 |
Correct |
460 ms |
774472 KB |
Output is correct |
21 |
Correct |
481 ms |
774596 KB |
Output is correct |
22 |
Correct |
472 ms |
774440 KB |
Output is correct |
23 |
Correct |
457 ms |
774548 KB |
Output is correct |
24 |
Correct |
472 ms |
774472 KB |
Output is correct |
25 |
Correct |
433 ms |
774492 KB |
Output is correct |
26 |
Correct |
418 ms |
774740 KB |
Output is correct |
27 |
Correct |
402 ms |
774464 KB |
Output is correct |
28 |
Correct |
418 ms |
774540 KB |
Output is correct |
29 |
Correct |
421 ms |
774528 KB |
Output is correct |
30 |
Correct |
415 ms |
774624 KB |
Output is correct |
31 |
Correct |
438 ms |
774456 KB |
Output is correct |
32 |
Correct |
433 ms |
774652 KB |
Output is correct |
33 |
Correct |
457 ms |
774472 KB |
Output is correct |
34 |
Correct |
462 ms |
774472 KB |
Output is correct |
35 |
Correct |
436 ms |
774536 KB |
Output is correct |
36 |
Correct |
441 ms |
774472 KB |
Output is correct |
37 |
Correct |
458 ms |
774472 KB |
Output is correct |
38 |
Correct |
456 ms |
774460 KB |
Output is correct |
39 |
Correct |
469 ms |
774468 KB |
Output is correct |
40 |
Correct |
478 ms |
774472 KB |
Output is correct |
41 |
Correct |
482 ms |
774480 KB |
Output is correct |
42 |
Correct |
460 ms |
774472 KB |
Output is correct |
43 |
Correct |
489 ms |
774660 KB |
Output is correct |
44 |
Correct |
420 ms |
774528 KB |
Output is correct |
45 |
Correct |
420 ms |
774472 KB |
Output is correct |
46 |
Correct |
430 ms |
774616 KB |
Output is correct |
47 |
Correct |
417 ms |
774672 KB |
Output is correct |
48 |
Correct |
425 ms |
774456 KB |
Output is correct |
49 |
Correct |
421 ms |
774472 KB |
Output is correct |
50 |
Correct |
443 ms |
774472 KB |
Output is correct |
51 |
Correct |
462 ms |
774480 KB |
Output is correct |
52 |
Correct |
458 ms |
774576 KB |
Output is correct |
53 |
Correct |
458 ms |
774472 KB |
Output is correct |
54 |
Correct |
472 ms |
774472 KB |
Output is correct |
55 |
Correct |
467 ms |
774472 KB |
Output is correct |
56 |
Correct |
483 ms |
774472 KB |
Output is correct |
57 |
Correct |
485 ms |
774508 KB |
Output is correct |
58 |
Execution timed out |
514 ms |
774472 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |