#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 = 1e15 + 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(0LL, 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';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
21084 KB |
Output is correct |
6 |
Correct |
3 ms |
21100 KB |
Output is correct |
7 |
Correct |
3 ms |
20948 KB |
Output is correct |
8 |
Correct |
3 ms |
20828 KB |
Output is correct |
9 |
Correct |
3 ms |
20828 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
3 ms |
20828 KB |
Output is correct |
12 |
Correct |
3 ms |
20992 KB |
Output is correct |
13 |
Correct |
2 ms |
20824 KB |
Output is correct |
14 |
Correct |
3 ms |
18780 KB |
Output is correct |
15 |
Correct |
4 ms |
20824 KB |
Output is correct |
16 |
Correct |
4 ms |
20824 KB |
Output is correct |
17 |
Correct |
4 ms |
18780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
21084 KB |
Output is correct |
6 |
Correct |
3 ms |
21100 KB |
Output is correct |
7 |
Correct |
3 ms |
20948 KB |
Output is correct |
8 |
Correct |
3 ms |
20828 KB |
Output is correct |
9 |
Correct |
3 ms |
20828 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
3 ms |
20828 KB |
Output is correct |
12 |
Correct |
3 ms |
20992 KB |
Output is correct |
13 |
Correct |
2 ms |
20824 KB |
Output is correct |
14 |
Correct |
3 ms |
18780 KB |
Output is correct |
15 |
Correct |
4 ms |
20824 KB |
Output is correct |
16 |
Correct |
4 ms |
20824 KB |
Output is correct |
17 |
Correct |
4 ms |
18780 KB |
Output is correct |
18 |
Correct |
11 ms |
36440 KB |
Output is correct |
19 |
Correct |
8 ms |
34652 KB |
Output is correct |
20 |
Correct |
6 ms |
36444 KB |
Output is correct |
21 |
Correct |
7 ms |
36636 KB |
Output is correct |
22 |
Correct |
6 ms |
35160 KB |
Output is correct |
23 |
Correct |
6 ms |
34396 KB |
Output is correct |
24 |
Correct |
5 ms |
33124 KB |
Output is correct |
25 |
Correct |
4 ms |
31580 KB |
Output is correct |
26 |
Correct |
4 ms |
33496 KB |
Output is correct |
27 |
Correct |
6 ms |
33328 KB |
Output is correct |
28 |
Correct |
7 ms |
34648 KB |
Output is correct |
29 |
Correct |
6 ms |
34768 KB |
Output is correct |
30 |
Correct |
6 ms |
36188 KB |
Output is correct |
31 |
Correct |
6 ms |
33884 KB |
Output is correct |
32 |
Correct |
7 ms |
36188 KB |
Output is correct |
33 |
Correct |
5 ms |
36952 KB |
Output is correct |
34 |
Correct |
6 ms |
34652 KB |
Output is correct |
35 |
Correct |
6 ms |
35368 KB |
Output is correct |
36 |
Correct |
6 ms |
36444 KB |
Output is correct |
37 |
Correct |
8 ms |
35420 KB |
Output is correct |
38 |
Correct |
6 ms |
33792 KB |
Output is correct |
39 |
Correct |
7 ms |
34652 KB |
Output is correct |
40 |
Correct |
4 ms |
31836 KB |
Output is correct |
41 |
Correct |
5 ms |
34648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
111 ms |
334804 KB |
Output is correct |
3 |
Correct |
6 ms |
35932 KB |
Output is correct |
4 |
Correct |
105 ms |
335888 KB |
Output is correct |
5 |
Correct |
102 ms |
337028 KB |
Output is correct |
6 |
Correct |
101 ms |
336468 KB |
Output is correct |
7 |
Correct |
100 ms |
334048 KB |
Output is correct |
8 |
Correct |
100 ms |
334776 KB |
Output is correct |
9 |
Correct |
102 ms |
333188 KB |
Output is correct |
10 |
Correct |
104 ms |
337232 KB |
Output is correct |
11 |
Correct |
98 ms |
337748 KB |
Output is correct |
12 |
Correct |
30 ms |
113100 KB |
Output is correct |
13 |
Correct |
50 ms |
174676 KB |
Output is correct |
14 |
Correct |
70 ms |
239444 KB |
Output is correct |
15 |
Correct |
111 ms |
336452 KB |
Output is correct |
16 |
Correct |
114 ms |
336356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
21084 KB |
Output is correct |
6 |
Correct |
3 ms |
21100 KB |
Output is correct |
7 |
Correct |
3 ms |
20948 KB |
Output is correct |
8 |
Correct |
3 ms |
20828 KB |
Output is correct |
9 |
Correct |
3 ms |
20828 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
3 ms |
20828 KB |
Output is correct |
12 |
Correct |
3 ms |
20992 KB |
Output is correct |
13 |
Correct |
2 ms |
20824 KB |
Output is correct |
14 |
Correct |
3 ms |
18780 KB |
Output is correct |
15 |
Correct |
4 ms |
20824 KB |
Output is correct |
16 |
Correct |
4 ms |
20824 KB |
Output is correct |
17 |
Correct |
4 ms |
18780 KB |
Output is correct |
18 |
Correct |
11 ms |
36440 KB |
Output is correct |
19 |
Correct |
8 ms |
34652 KB |
Output is correct |
20 |
Correct |
6 ms |
36444 KB |
Output is correct |
21 |
Correct |
7 ms |
36636 KB |
Output is correct |
22 |
Correct |
6 ms |
35160 KB |
Output is correct |
23 |
Correct |
6 ms |
34396 KB |
Output is correct |
24 |
Correct |
5 ms |
33124 KB |
Output is correct |
25 |
Correct |
4 ms |
31580 KB |
Output is correct |
26 |
Correct |
4 ms |
33496 KB |
Output is correct |
27 |
Correct |
6 ms |
33328 KB |
Output is correct |
28 |
Correct |
7 ms |
34648 KB |
Output is correct |
29 |
Correct |
6 ms |
34768 KB |
Output is correct |
30 |
Correct |
6 ms |
36188 KB |
Output is correct |
31 |
Correct |
6 ms |
33884 KB |
Output is correct |
32 |
Correct |
7 ms |
36188 KB |
Output is correct |
33 |
Correct |
5 ms |
36952 KB |
Output is correct |
34 |
Correct |
6 ms |
34652 KB |
Output is correct |
35 |
Correct |
6 ms |
35368 KB |
Output is correct |
36 |
Correct |
6 ms |
36444 KB |
Output is correct |
37 |
Correct |
8 ms |
35420 KB |
Output is correct |
38 |
Correct |
6 ms |
33792 KB |
Output is correct |
39 |
Correct |
7 ms |
34652 KB |
Output is correct |
40 |
Correct |
4 ms |
31836 KB |
Output is correct |
41 |
Correct |
5 ms |
34648 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
111 ms |
334804 KB |
Output is correct |
44 |
Correct |
6 ms |
35932 KB |
Output is correct |
45 |
Correct |
105 ms |
335888 KB |
Output is correct |
46 |
Correct |
102 ms |
337028 KB |
Output is correct |
47 |
Correct |
101 ms |
336468 KB |
Output is correct |
48 |
Correct |
100 ms |
334048 KB |
Output is correct |
49 |
Correct |
100 ms |
334776 KB |
Output is correct |
50 |
Correct |
102 ms |
333188 KB |
Output is correct |
51 |
Correct |
104 ms |
337232 KB |
Output is correct |
52 |
Correct |
98 ms |
337748 KB |
Output is correct |
53 |
Correct |
30 ms |
113100 KB |
Output is correct |
54 |
Correct |
50 ms |
174676 KB |
Output is correct |
55 |
Correct |
70 ms |
239444 KB |
Output is correct |
56 |
Correct |
111 ms |
336452 KB |
Output is correct |
57 |
Correct |
114 ms |
336356 KB |
Output is correct |
58 |
Execution timed out |
544 ms |
329340 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |