// oooo
/*
har chi delet mikhad bebar ~
gitar o ba khodet nabar! ~
;Amoo_Hasan;
*/
#include<bits/stdc++.h>
//#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
//#pragma GCC target("avx2,fma")
using namespace std;
typedef long long ll;
typedef long double ld;
#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define wtf(x) cout<<#x <<" : " <<x
constexpr int N = 410, inf = 510510;
int dp[N][N][N][3], ind[3][N], tim[3], pr[N][3];
int main() {
ios :: sync_with_stdio(0), cin.tie(0);
int n; cin >>n;
string s; cin >>s;
s = '#' + s;
for(int i = 1; i <= n; i++) {
if(s[i] == 'R') s[i] = 'a';
else if(s[i] == 'G') s[i] = 'b';
else s[i] = 'c';
}
for(int i = 1; i <= n; i++) {
ind[s[i] - 'a'][++tim[s[i] - 'a']] = i;
}
int cnt0 = 0, cnt1 = 0, cnt2 = 0;
for(int i = 1; i <= n; i++) {
if(s[i] - 'a' == 0) cnt0++;
else if(s[i] - 'a' == 1) cnt1++;
else cnt2++;
}
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= 2; j++) pr[i][j] = pr[i - 1][j];
pr[i][s[i] - 'a']++;
}
for(int i = 0; i <= cnt0; i++) {
for(int j = 0; j <= cnt1; j++) {
for(int k = 0; k <= cnt2; k++) {
for(int f = 0; f <= 2; f++) {
dp[i][j][k][f] = inf;
}
}
}
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
int ans;
for(int i = 0; i <= cnt0; i++)
for(int j = 0; j <= cnt1; j++)
for(int k = 0; k <= cnt2; k++)
for(int l = 0; l <= 2; l++)
for(int cur = 0; cur <= 2; cur++) {
if(cur == l) continue;
if(cur == 0 && cnt0 == i) continue;
if(cur == 1 && cnt1 == j) continue;
if(cur == 2 && cnt2 == k) continue;
int pos1 = 0, pos2 = 0, pos3 = 0;
int res = 3 - l - cur;
if(l == 0) {
pos1 = ind[0][i];
if(cur == 1) pos2 = ind[1][j + 1], pos3 = ind[2][k];
else pos2 = ind[2][k + 1], pos3 = ind[1][j];
}
else if(l == 1) {
pos1 = ind[1][j];
if(cur == 0) pos2 = ind[0][i + 1], pos3 = ind[2][k];
else pos2 = ind[2][k + 1], pos3 = ind[0][i];
}
else if(l == 2) {
pos1 = ind[2][k];
if(cur == 0) pos2 = ind[0][i + 1], pos3 = ind[1][j];
else pos2 = ind[1][j + 1], pos3 = ind[0][i];
}
ans = 0;
ans += max(0, pr[pos2][l] - pr[pos1][l]);
ans += max(0, pr[pos2][res] - pr[pos3][res]);
ans += dp[i][j][k][l];
if(cur == 0) {
dp[i + 1][j][k][cur] = min(dp[i + 1][j][k][cur], ans);
}
else if(cur == 1) {
dp[i][j + 1][k][cur] = min(dp[i][j + 1][k][cur], ans);
}
else {
dp[i][j][k + 1][cur] = min(dp[i][j][k + 1][cur], ans);
}
}
ans = inf;
for(int i = 0; i < 3; i++) ans = min(ans, dp[cnt0][cnt1][cnt2][i]);
if(ans >= inf) cout<<-1;
else cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
1872 KB |
Output is correct |
20 |
Correct |
2 ms |
2252 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
2 ms |
2120 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
2 ms |
3908 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
2 ms |
2388 KB |
Output is correct |
29 |
Correct |
2 ms |
2260 KB |
Output is correct |
30 |
Correct |
2 ms |
2252 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
2 ms |
2376 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3528 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
2 ms |
2132 KB |
Output is correct |
39 |
Correct |
2 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
62 ms |
163008 KB |
Output is correct |
3 |
Correct |
67 ms |
162168 KB |
Output is correct |
4 |
Correct |
61 ms |
162884 KB |
Output is correct |
5 |
Correct |
58 ms |
162896 KB |
Output is correct |
6 |
Correct |
60 ms |
162980 KB |
Output is correct |
7 |
Correct |
62 ms |
162080 KB |
Output is correct |
8 |
Correct |
58 ms |
162124 KB |
Output is correct |
9 |
Correct |
61 ms |
161284 KB |
Output is correct |
10 |
Correct |
61 ms |
162956 KB |
Output is correct |
11 |
Correct |
59 ms |
162920 KB |
Output is correct |
12 |
Correct |
18 ms |
43988 KB |
Output is correct |
13 |
Correct |
29 ms |
77140 KB |
Output is correct |
14 |
Correct |
40 ms |
111368 KB |
Output is correct |
15 |
Correct |
61 ms |
162972 KB |
Output is correct |
16 |
Correct |
63 ms |
162896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
1872 KB |
Output is correct |
20 |
Correct |
2 ms |
2252 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
2 ms |
2120 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
2 ms |
3908 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
2 ms |
2388 KB |
Output is correct |
29 |
Correct |
2 ms |
2260 KB |
Output is correct |
30 |
Correct |
2 ms |
2252 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
2 ms |
2376 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3528 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
2 ms |
2132 KB |
Output is correct |
39 |
Correct |
2 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2260 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
62 ms |
163008 KB |
Output is correct |
44 |
Correct |
67 ms |
162168 KB |
Output is correct |
45 |
Correct |
61 ms |
162884 KB |
Output is correct |
46 |
Correct |
58 ms |
162896 KB |
Output is correct |
47 |
Correct |
60 ms |
162980 KB |
Output is correct |
48 |
Correct |
62 ms |
162080 KB |
Output is correct |
49 |
Correct |
58 ms |
162124 KB |
Output is correct |
50 |
Correct |
61 ms |
161284 KB |
Output is correct |
51 |
Correct |
61 ms |
162956 KB |
Output is correct |
52 |
Correct |
59 ms |
162920 KB |
Output is correct |
53 |
Correct |
18 ms |
43988 KB |
Output is correct |
54 |
Correct |
29 ms |
77140 KB |
Output is correct |
55 |
Correct |
40 ms |
111368 KB |
Output is correct |
56 |
Correct |
61 ms |
162972 KB |
Output is correct |
57 |
Correct |
63 ms |
162896 KB |
Output is correct |
58 |
Correct |
119 ms |
77172 KB |
Output is correct |
59 |
Correct |
129 ms |
97792 KB |
Output is correct |
60 |
Correct |
125 ms |
88904 KB |
Output is correct |
61 |
Correct |
137 ms |
86028 KB |
Output is correct |
62 |
Correct |
72 ms |
160636 KB |
Output is correct |
63 |
Correct |
75 ms |
160256 KB |
Output is correct |
64 |
Correct |
101 ms |
150148 KB |
Output is correct |
65 |
Correct |
137 ms |
138444 KB |
Output is correct |
66 |
Correct |
124 ms |
87172 KB |
Output is correct |
67 |
Correct |
124 ms |
83904 KB |
Output is correct |
68 |
Correct |
131 ms |
89812 KB |
Output is correct |
69 |
Correct |
124 ms |
86524 KB |
Output is correct |
70 |
Correct |
130 ms |
93632 KB |
Output is correct |
71 |
Correct |
119 ms |
85004 KB |
Output is correct |
72 |
Correct |
43 ms |
42128 KB |
Output is correct |
73 |
Correct |
7 ms |
2900 KB |
Output is correct |