#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]);
}
}
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), it = 0;
vector <int> a(3), t = {j, k, p};
for ( auto u: t ){
int l = 0, r = n;
while ( l < r ){
int md = (l + r) >> 1;
if ( pf[md][it] < u + 1 ){
l = md + 1;
} else r = md;
}
if ( pf[r][it] < u + 1 ){
a[it] = -1;
} else{
for ( int q = 0; q < 3; q++ ){
if ( q != it ){
a[it] += max(0, pf[l][q] - t[q]);
}
}
} it++;
}
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 |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10600 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10600 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
5 ms |
22620 KB |
Output is correct |
19 |
Correct |
5 ms |
22620 KB |
Output is correct |
20 |
Correct |
6 ms |
22620 KB |
Output is correct |
21 |
Correct |
5 ms |
22876 KB |
Output is correct |
22 |
Correct |
5 ms |
23128 KB |
Output is correct |
23 |
Correct |
5 ms |
22620 KB |
Output is correct |
24 |
Correct |
4 ms |
23796 KB |
Output is correct |
25 |
Correct |
3 ms |
25180 KB |
Output is correct |
26 |
Correct |
3 ms |
25072 KB |
Output is correct |
27 |
Correct |
4 ms |
23896 KB |
Output is correct |
28 |
Correct |
5 ms |
26460 KB |
Output is correct |
29 |
Correct |
5 ms |
24668 KB |
Output is correct |
30 |
Correct |
6 ms |
26200 KB |
Output is correct |
31 |
Correct |
5 ms |
24156 KB |
Output is correct |
32 |
Correct |
5 ms |
26204 KB |
Output is correct |
33 |
Correct |
4 ms |
26716 KB |
Output is correct |
34 |
Correct |
4 ms |
24668 KB |
Output is correct |
35 |
Correct |
6 ms |
25948 KB |
Output is correct |
36 |
Correct |
5 ms |
24664 KB |
Output is correct |
37 |
Correct |
5 ms |
23896 KB |
Output is correct |
38 |
Correct |
5 ms |
24156 KB |
Output is correct |
39 |
Correct |
5 ms |
24664 KB |
Output is correct |
40 |
Correct |
3 ms |
25180 KB |
Output is correct |
41 |
Correct |
4 ms |
25692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
91 ms |
254068 KB |
Output is correct |
3 |
Correct |
6 ms |
32088 KB |
Output is correct |
4 |
Correct |
85 ms |
253868 KB |
Output is correct |
5 |
Correct |
82 ms |
253988 KB |
Output is correct |
6 |
Correct |
78 ms |
254040 KB |
Output is correct |
7 |
Correct |
76 ms |
252520 KB |
Output is correct |
8 |
Correct |
81 ms |
252384 KB |
Output is correct |
9 |
Correct |
77 ms |
251880 KB |
Output is correct |
10 |
Correct |
76 ms |
254032 KB |
Output is correct |
11 |
Correct |
78 ms |
254032 KB |
Output is correct |
12 |
Correct |
25 ms |
83096 KB |
Output is correct |
13 |
Correct |
39 ms |
128848 KB |
Output is correct |
14 |
Correct |
52 ms |
178496 KB |
Output is correct |
15 |
Correct |
76 ms |
253008 KB |
Output is correct |
16 |
Correct |
80 ms |
253164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10600 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
5 ms |
22620 KB |
Output is correct |
19 |
Correct |
5 ms |
22620 KB |
Output is correct |
20 |
Correct |
6 ms |
22620 KB |
Output is correct |
21 |
Correct |
5 ms |
22876 KB |
Output is correct |
22 |
Correct |
5 ms |
23128 KB |
Output is correct |
23 |
Correct |
5 ms |
22620 KB |
Output is correct |
24 |
Correct |
4 ms |
23796 KB |
Output is correct |
25 |
Correct |
3 ms |
25180 KB |
Output is correct |
26 |
Correct |
3 ms |
25072 KB |
Output is correct |
27 |
Correct |
4 ms |
23896 KB |
Output is correct |
28 |
Correct |
5 ms |
26460 KB |
Output is correct |
29 |
Correct |
5 ms |
24668 KB |
Output is correct |
30 |
Correct |
6 ms |
26200 KB |
Output is correct |
31 |
Correct |
5 ms |
24156 KB |
Output is correct |
32 |
Correct |
5 ms |
26204 KB |
Output is correct |
33 |
Correct |
4 ms |
26716 KB |
Output is correct |
34 |
Correct |
4 ms |
24668 KB |
Output is correct |
35 |
Correct |
6 ms |
25948 KB |
Output is correct |
36 |
Correct |
5 ms |
24664 KB |
Output is correct |
37 |
Correct |
5 ms |
23896 KB |
Output is correct |
38 |
Correct |
5 ms |
24156 KB |
Output is correct |
39 |
Correct |
5 ms |
24664 KB |
Output is correct |
40 |
Correct |
3 ms |
25180 KB |
Output is correct |
41 |
Correct |
4 ms |
25692 KB |
Output is correct |
42 |
Correct |
1 ms |
2392 KB |
Output is correct |
43 |
Correct |
91 ms |
254068 KB |
Output is correct |
44 |
Correct |
6 ms |
32088 KB |
Output is correct |
45 |
Correct |
85 ms |
253868 KB |
Output is correct |
46 |
Correct |
82 ms |
253988 KB |
Output is correct |
47 |
Correct |
78 ms |
254040 KB |
Output is correct |
48 |
Correct |
76 ms |
252520 KB |
Output is correct |
49 |
Correct |
81 ms |
252384 KB |
Output is correct |
50 |
Correct |
77 ms |
251880 KB |
Output is correct |
51 |
Correct |
76 ms |
254032 KB |
Output is correct |
52 |
Correct |
78 ms |
254032 KB |
Output is correct |
53 |
Correct |
25 ms |
83096 KB |
Output is correct |
54 |
Correct |
39 ms |
128848 KB |
Output is correct |
55 |
Correct |
52 ms |
178496 KB |
Output is correct |
56 |
Correct |
76 ms |
253008 KB |
Output is correct |
57 |
Correct |
80 ms |
253164 KB |
Output is correct |
58 |
Execution timed out |
534 ms |
169028 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |