#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
//#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int inf = 1e9 + 1;
const int N = 201;
int dp[N * 2][N][N][4];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
string s; cin >> s;
s = '#' + s;
map <char,int> id;
id['R'] = 0, id['G'] = 1, id['Y'] = 2;
vector <ar<int,3>> pf(n + 1);
for ( int i = 1; i <= n; i++ ){
pf[i] = pf[i - 1];
pf[i][id[s[i]]]++;
}
{ // modifying
int it = 0;
for ( int j = 1; j <= 2; j++ ){
if ( pf[n][j] >= pf[n][it] ){
it = j;
}
}
for ( int j = 0; j <= n; j++ ){
swap(pf[j][it], pf[j][2]);
}
}
vector <vector<int>> pos(3, vector <int> (n + 2, -1));
for ( int i = 0; i < 3; i++ ){
for ( int j = 0; j <= n; j++ ){
int l = 0, r = n;
while ( l < r ){
int md = (l + r) >> 1;
if ( pf[md][i] < j ){
l = md + 1;
} else r = md;
}
if ( pf[l][i] >= j ){
pos[i][j] = l;
}
}
}
for ( int i = 0; i <= n; i++ ){
for ( int j = 0; j <= pf[n][0]; j++ ){
for ( int k = 0; k <= pf[n][1]; k++ ){
for ( int lst = 0; lst <= 3; lst++ ){
dp[i][j][k][lst] = inf;
}
}
}
}
dp[0][0][0][3] = 0;
for ( int i = 0; i < n; i++ ){
for ( int j = 0; j <= pf[n][0]; j++ ){
for ( int k = 0; k <= pf[n][1]; k++ ){
for ( int lst = 0; lst < 3 + !i; lst++ ){
int &tmp = dp[i][j][k][lst];
if ( tmp == inf ){
continue;
}
int p = i - (j + k);
vector <int> a(3), t = {j, k, p};
for ( int it = 0; it < 3; it++ ){
int l = pos[it][t[it] + 1];
if ( l == -1 ){
a[it] = -1;
} else{
for ( int q = 0; q < 3; q++ ){
if ( q != it ){
a[it] += max(0, pf[l][q] - t[q]);
}
}
}
}
if ( a[0] != -1 && lst != 0 ){
chmin(dp[i + 1][j + 1][k][0], tmp + a[0]);
}
if ( a[1] != -1 && lst != 1 ){
chmin(dp[i + 1][j][k + 1][1], tmp + a[1]);
}
if ( a[2] != -1 && lst != 2 ){
chmin(dp[i + 1][j][k][2], tmp + a[2]);
}
}
}
}
}
int opt = inf;
for ( int i = 0; i < 3; i++ ){
chmin(opt, dp[n][pf[n][0]][pf[n][1]][i]);
}
if ( opt == inf ){
opt = -1;
}
cout << opt;
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
1 ms |
840 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
1 ms |
840 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
20 |
Correct |
2 ms |
4188 KB |
Output is correct |
21 |
Correct |
2 ms |
4444 KB |
Output is correct |
22 |
Correct |
4 ms |
5212 KB |
Output is correct |
23 |
Correct |
2 ms |
3932 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
1008 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
3 ms |
4440 KB |
Output is correct |
29 |
Correct |
3 ms |
4444 KB |
Output is correct |
30 |
Correct |
2 ms |
3916 KB |
Output is correct |
31 |
Correct |
2 ms |
3676 KB |
Output is correct |
32 |
Correct |
2 ms |
4188 KB |
Output is correct |
33 |
Correct |
3 ms |
5468 KB |
Output is correct |
34 |
Correct |
2 ms |
4700 KB |
Output is correct |
35 |
Correct |
2 ms |
3160 KB |
Output is correct |
36 |
Correct |
3 ms |
4440 KB |
Output is correct |
37 |
Correct |
2 ms |
2904 KB |
Output is correct |
38 |
Correct |
2 ms |
3420 KB |
Output is correct |
39 |
Correct |
4 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
88 ms |
253916 KB |
Output is correct |
3 |
Correct |
2 ms |
3676 KB |
Output is correct |
4 |
Correct |
91 ms |
254036 KB |
Output is correct |
5 |
Correct |
94 ms |
254032 KB |
Output is correct |
6 |
Correct |
86 ms |
253924 KB |
Output is correct |
7 |
Correct |
87 ms |
252500 KB |
Output is correct |
8 |
Correct |
87 ms |
252496 KB |
Output is correct |
9 |
Correct |
89 ms |
251804 KB |
Output is correct |
10 |
Correct |
89 ms |
253940 KB |
Output is correct |
11 |
Correct |
91 ms |
254016 KB |
Output is correct |
12 |
Correct |
31 ms |
68444 KB |
Output is correct |
13 |
Correct |
46 ms |
119960 KB |
Output is correct |
14 |
Correct |
62 ms |
173552 KB |
Output is correct |
15 |
Correct |
89 ms |
253152 KB |
Output is correct |
16 |
Correct |
86 ms |
253156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
1 ms |
840 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
20 |
Correct |
2 ms |
4188 KB |
Output is correct |
21 |
Correct |
2 ms |
4444 KB |
Output is correct |
22 |
Correct |
4 ms |
5212 KB |
Output is correct |
23 |
Correct |
2 ms |
3932 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
1008 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
3 ms |
4440 KB |
Output is correct |
29 |
Correct |
3 ms |
4444 KB |
Output is correct |
30 |
Correct |
2 ms |
3916 KB |
Output is correct |
31 |
Correct |
2 ms |
3676 KB |
Output is correct |
32 |
Correct |
2 ms |
4188 KB |
Output is correct |
33 |
Correct |
3 ms |
5468 KB |
Output is correct |
34 |
Correct |
2 ms |
4700 KB |
Output is correct |
35 |
Correct |
2 ms |
3160 KB |
Output is correct |
36 |
Correct |
3 ms |
4440 KB |
Output is correct |
37 |
Correct |
2 ms |
2904 KB |
Output is correct |
38 |
Correct |
2 ms |
3420 KB |
Output is correct |
39 |
Correct |
4 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
88 ms |
253916 KB |
Output is correct |
44 |
Correct |
2 ms |
3676 KB |
Output is correct |
45 |
Correct |
91 ms |
254036 KB |
Output is correct |
46 |
Correct |
94 ms |
254032 KB |
Output is correct |
47 |
Correct |
86 ms |
253924 KB |
Output is correct |
48 |
Correct |
87 ms |
252500 KB |
Output is correct |
49 |
Correct |
87 ms |
252496 KB |
Output is correct |
50 |
Correct |
89 ms |
251804 KB |
Output is correct |
51 |
Correct |
89 ms |
253940 KB |
Output is correct |
52 |
Correct |
91 ms |
254016 KB |
Output is correct |
53 |
Correct |
31 ms |
68444 KB |
Output is correct |
54 |
Correct |
46 ms |
119960 KB |
Output is correct |
55 |
Correct |
62 ms |
173552 KB |
Output is correct |
56 |
Correct |
89 ms |
253152 KB |
Output is correct |
57 |
Correct |
86 ms |
253156 KB |
Output is correct |
58 |
Correct |
238 ms |
158252 KB |
Output is correct |
59 |
Correct |
263 ms |
151792 KB |
Output is correct |
60 |
Correct |
264 ms |
164984 KB |
Output is correct |
61 |
Correct |
256 ms |
170416 KB |
Output is correct |
62 |
Correct |
7 ms |
8536 KB |
Output is correct |
63 |
Correct |
106 ms |
248148 KB |
Output is correct |
64 |
Correct |
70 ms |
46488 KB |
Output is correct |
65 |
Correct |
158 ms |
202620 KB |
Output is correct |
66 |
Correct |
267 ms |
167080 KB |
Output is correct |
67 |
Correct |
228 ms |
144448 KB |
Output is correct |
68 |
Correct |
259 ms |
165284 KB |
Output is correct |
69 |
Correct |
258 ms |
173180 KB |
Output is correct |
70 |
Correct |
250 ms |
173228 KB |
Output is correct |
71 |
Correct |
258 ms |
171588 KB |
Output is correct |
72 |
Correct |
48 ms |
91728 KB |
Output is correct |
73 |
Correct |
5 ms |
8024 KB |
Output is correct |