//0 1 1 0 1
//0 1 0 0 1
//1 0 0 1 1
//0 1 1 0 1
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
#define F first
#define S second
#define pb push_back
#define sze size()
#define all(x) x.begin() , x.end()
#define wall__ cout << "--------------------------------------\n";
#define kids int mid = (tl + tr) >> 1, cl = v << 1, cr = v << 1 | 1
#define file_io freopen("input.cpp", "r", stdin); freopen("output.cpp", "w", stdout);
typedef long long ll;
typedef long double dl;
typedef short int it;
typedef pair < int , int > pii;
typedef pair < int , ll > pil;
typedef pair < ll , int > pli;
typedef pair < ll , ll > pll;
typedef pair < int , pii > piii;
typedef pair < ll, pll > plll;
const ll N = 400 + 10;
const ll mod = 1e9 + 7;
const ll inf = 1000;
const ll rinf = -2e16;
const ll INF = 1e9 + 10;
const ll rINF = -1e9 - 10;
const ll lg = 32;
it dp[N][N][N][3], n, a[N], cnt[N][3], A, B, C;
string s;
vector < it > p[3];
inline it give (it l, it r, it t) {
if (l > r) return 0;
return cnt[r][t] - cnt[l - 1][t];
}
void solve () {
cin >> n;
cin >> s;
s = '.' + s; p[0].pb(0); p[1].pb(0); p[2].pb(0);
for (int i = 1; i <= n; i++) {
if (s[i] == 'R') {
p[0].pb(i); a[i] = 0; ++cnt[i][0];
} else if (s[i] == 'G') {
p[1].pb(i); a[i] = 1; ++cnt[i][1];
} else {
p[2].pb(i); a[i] = 2; ++cnt[i][2];
}
cnt[i][0] += cnt[i - 1][0];
cnt[i][1] += cnt[i - 1][1];
cnt[i][2] += cnt[i - 1][2];
}
A = p[0].sze - 1;
B = p[1].sze - 1;
C = p[2].sze - 1;
it c[3]; c[0] = 0; c[1] = 0; c[2] = 0;
for (; c[0] <= A; c[0]++) {
for (c[1] = 0; c[1] <= B; c[1]++) {
for (c[2] = 0; c[2] <= C; c[2]++) {
if (c[0] == 0 && c[1] == 0 && c[2] == 0) continue;
for (it t = 0; t < 3; t++) {
if (c[t] == 0) {
dp[c[0]][c[1]][c[2]][t] = 20000;
continue;
}
it m = max({p[0][c[0]], p[1][c[1]], p[2][c[2]]});
it l = p[t][c[t]];
it len = m - l;
len -= give(l + 1, m, t);
l = max(p[t][c[t]], p[(t + 1) % 3][c[(t + 1) % 3]]);
len -= give(l + 1, m, (t + 1) % 3);
l = max(p[t][c[t]], p[(t + 2) % 3][c[(t + 2) % 3]]);
len -= give(l + 1, m, (t + 2) % 3);
if (t == 0) dp[c[0]][c[1]][c[2]][t] = len + min(dp[c[0] - 1][c[1]][c[2]][(t + 1) % 3], dp[c[0] - 1][c[1]][c[2]][(t + 2) % 3]);
else if (t == 1) dp[c[0]][c[1]][c[2]][t] = len + min(dp[c[0]][c[1] - 1][c[2]][(t + 1) % 3], dp[c[0]][c[1] - 1][c[2]][(t + 2) % 3]);
else dp[c[0]][c[1]][c[2]][t] = len + min(dp[c[0]][c[1]][c[2] - 1][(t + 1) % 3], dp[c[0]][c[1]][c[2] - 1][(t + 2) % 3]);
}
}
}
}
if (min({dp[A][B][C][0], dp[A][B][C][1], dp[A][B][C][2]}) >= 20000) cout << -1 << '\n';
else cout << min({dp[A][B][C][0], dp[A][B][C][1], dp[A][B][C][2]}) << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {solve();}
return 0;
}
/*
*/
//shrek will AC this;
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
1492 KB |
Output is correct |
19 |
Correct |
1 ms |
1236 KB |
Output is correct |
20 |
Correct |
1 ms |
1480 KB |
Output is correct |
21 |
Correct |
1 ms |
1492 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
2516 KB |
Output is correct |
26 |
Correct |
1 ms |
2644 KB |
Output is correct |
27 |
Correct |
1 ms |
1876 KB |
Output is correct |
28 |
Correct |
1 ms |
1492 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
2 ms |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
1236 KB |
Output is correct |
32 |
Correct |
1 ms |
1492 KB |
Output is correct |
33 |
Correct |
2 ms |
2376 KB |
Output is correct |
34 |
Correct |
2 ms |
2256 KB |
Output is correct |
35 |
Correct |
1 ms |
1620 KB |
Output is correct |
36 |
Correct |
1 ms |
1236 KB |
Output is correct |
37 |
Correct |
1 ms |
1096 KB |
Output is correct |
38 |
Correct |
1 ms |
1352 KB |
Output is correct |
39 |
Correct |
1 ms |
1492 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
1360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
36 ms |
98032 KB |
Output is correct |
3 |
Correct |
39 ms |
97484 KB |
Output is correct |
4 |
Correct |
36 ms |
97988 KB |
Output is correct |
5 |
Correct |
37 ms |
97964 KB |
Output is correct |
6 |
Correct |
37 ms |
98032 KB |
Output is correct |
7 |
Correct |
36 ms |
97596 KB |
Output is correct |
8 |
Correct |
37 ms |
97584 KB |
Output is correct |
9 |
Correct |
37 ms |
97096 KB |
Output is correct |
10 |
Correct |
37 ms |
97996 KB |
Output is correct |
11 |
Correct |
40 ms |
98008 KB |
Output is correct |
12 |
Correct |
12 ms |
26568 KB |
Output is correct |
13 |
Correct |
18 ms |
46548 KB |
Output is correct |
14 |
Correct |
27 ms |
67040 KB |
Output is correct |
15 |
Correct |
41 ms |
98068 KB |
Output is correct |
16 |
Correct |
39 ms |
98044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
1492 KB |
Output is correct |
19 |
Correct |
1 ms |
1236 KB |
Output is correct |
20 |
Correct |
1 ms |
1480 KB |
Output is correct |
21 |
Correct |
1 ms |
1492 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
2516 KB |
Output is correct |
26 |
Correct |
1 ms |
2644 KB |
Output is correct |
27 |
Correct |
1 ms |
1876 KB |
Output is correct |
28 |
Correct |
1 ms |
1492 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
2 ms |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
1236 KB |
Output is correct |
32 |
Correct |
1 ms |
1492 KB |
Output is correct |
33 |
Correct |
2 ms |
2376 KB |
Output is correct |
34 |
Correct |
2 ms |
2256 KB |
Output is correct |
35 |
Correct |
1 ms |
1620 KB |
Output is correct |
36 |
Correct |
1 ms |
1236 KB |
Output is correct |
37 |
Correct |
1 ms |
1096 KB |
Output is correct |
38 |
Correct |
1 ms |
1352 KB |
Output is correct |
39 |
Correct |
1 ms |
1492 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
1360 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
36 ms |
98032 KB |
Output is correct |
44 |
Correct |
39 ms |
97484 KB |
Output is correct |
45 |
Correct |
36 ms |
97988 KB |
Output is correct |
46 |
Correct |
37 ms |
97964 KB |
Output is correct |
47 |
Correct |
37 ms |
98032 KB |
Output is correct |
48 |
Correct |
36 ms |
97596 KB |
Output is correct |
49 |
Correct |
37 ms |
97584 KB |
Output is correct |
50 |
Correct |
37 ms |
97096 KB |
Output is correct |
51 |
Correct |
37 ms |
97996 KB |
Output is correct |
52 |
Correct |
40 ms |
98008 KB |
Output is correct |
53 |
Correct |
12 ms |
26568 KB |
Output is correct |
54 |
Correct |
18 ms |
46548 KB |
Output is correct |
55 |
Correct |
27 ms |
67040 KB |
Output is correct |
56 |
Correct |
41 ms |
98068 KB |
Output is correct |
57 |
Correct |
39 ms |
98044 KB |
Output is correct |
58 |
Correct |
70 ms |
38988 KB |
Output is correct |
59 |
Correct |
76 ms |
49328 KB |
Output is correct |
60 |
Correct |
73 ms |
44884 KB |
Output is correct |
61 |
Correct |
71 ms |
43324 KB |
Output is correct |
62 |
Correct |
40 ms |
95564 KB |
Output is correct |
63 |
Correct |
44 ms |
94156 KB |
Output is correct |
64 |
Correct |
61 ms |
82240 KB |
Output is correct |
65 |
Correct |
76 ms |
71652 KB |
Output is correct |
66 |
Correct |
73 ms |
43980 KB |
Output is correct |
67 |
Correct |
70 ms |
42316 KB |
Output is correct |
68 |
Correct |
72 ms |
45372 KB |
Output is correct |
69 |
Correct |
72 ms |
43660 KB |
Output is correct |
70 |
Correct |
76 ms |
47184 KB |
Output is correct |
71 |
Correct |
73 ms |
42892 KB |
Output is correct |
72 |
Correct |
23 ms |
21716 KB |
Output is correct |
73 |
Correct |
5 ms |
1608 KB |
Output is correct |