#include <bits/stdc++.h>
using namespace std;
const long long INFll = (long long) 1e18 + 10;
const int INFii = (int) 1e9 + 10;
typedef long long ll;
typedef int ii;
typedef long double dbl;
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define maxn 401
ii n, dp[maxn][maxn][maxn][4];
ii qtd1, qtd2, qtd3;
ii pos1[maxn], pos2[maxn], pos3[maxn];
ii cnt1[maxn], cnt2[maxn], cnt3[maxn];
string s;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
//freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
cin >> n >> s;
for(ii i = 1; i <= n; i++) {
cnt1[i] = cnt1[i-1];
cnt2[i] = cnt2[i-1];
cnt3[i] = cnt3[i-1];
if(s[i-1] == 'R') {
cnt1[i]++;
pos1[++qtd1] = i;
}
if(s[i-1] == 'G') {
cnt2[i]++;
pos2[++qtd2] = i;
}
if(s[i-1] == 'Y') {
cnt3[i]++;
pos3[++qtd3] = i;
}
}
if(qtd1 > (n+1)/2 || qtd2 > (n+1)/2 || qtd3 > (n+1)/2) {
cout << -1 << endl;
return 0;
}
for(ii c1 = qtd1; c1 >= 0; c1--) {
for(ii c2 = qtd2; c2 >= 0; c2--) {
for(ii c3 = qtd3; c3 >= 0; c3--) {
for(ii ant = 1; ant <= 3; ant++) {
if(c1 == qtd1 && c2 == qtd2 && c3 == qtd3) {
dp[c1][c2][c3][ant] = 0;
continue;
}
ii ans = INFii;
if(c1 != qtd1 && ant != 1) {
ans = min(ans, dp[c1+1][c2][c3][1] + max(0,cnt2[pos2[c2]]-cnt2[pos1[c1+1]]) + max(0,cnt3[pos3[c3]]-cnt3[pos1[c1+1]]));
}
if(c2 != qtd2 && ant != 2) {
ans = min(ans, dp[c1][c2+1][c3][2] + max(0,cnt1[pos1[c1]]-cnt1[pos2[c2+1]]) + max(0,cnt3[pos3[c3]]-cnt3[pos2[c2+1]]));
}
if(c3 != qtd3 && ant != 3) {
ans = min(ans, dp[c1][c2][c3+1][3] + max(0,cnt2[pos2[c2]]-cnt2[pos3[c3+1]]) + max(0,cnt1[pos1[c1]]-cnt1[pos3[c3+1]]));
}
dp[c1][c2][c3][ant] = ans;
}
}
}
}
cout << min({dp[0][0][0][1],dp[0][0][0][2],dp[0][0][0][3]}) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
576 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
576 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2368 KB |
Output is correct |
19 |
Correct |
1 ms |
1868 KB |
Output is correct |
20 |
Correct |
1 ms |
2252 KB |
Output is correct |
21 |
Correct |
1 ms |
2380 KB |
Output is correct |
22 |
Correct |
1 ms |
1740 KB |
Output is correct |
23 |
Correct |
1 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
1 ms |
2380 KB |
Output is correct |
29 |
Correct |
1 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
2252 KB |
Output is correct |
31 |
Correct |
1 ms |
1996 KB |
Output is correct |
32 |
Correct |
1 ms |
2380 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
1868 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
1 ms |
2124 KB |
Output is correct |
39 |
Correct |
2 ms |
2380 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
65 ms |
162852 KB |
Output is correct |
3 |
Correct |
61 ms |
162060 KB |
Output is correct |
4 |
Correct |
56 ms |
162884 KB |
Output is correct |
5 |
Correct |
56 ms |
162884 KB |
Output is correct |
6 |
Correct |
60 ms |
162792 KB |
Output is correct |
7 |
Correct |
58 ms |
162076 KB |
Output is correct |
8 |
Correct |
64 ms |
162004 KB |
Output is correct |
9 |
Correct |
70 ms |
161248 KB |
Output is correct |
10 |
Correct |
56 ms |
162784 KB |
Output is correct |
11 |
Correct |
55 ms |
162772 KB |
Output is correct |
12 |
Correct |
15 ms |
44064 KB |
Output is correct |
13 |
Correct |
25 ms |
77068 KB |
Output is correct |
14 |
Correct |
36 ms |
111296 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
576 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2368 KB |
Output is correct |
19 |
Correct |
1 ms |
1868 KB |
Output is correct |
20 |
Correct |
1 ms |
2252 KB |
Output is correct |
21 |
Correct |
1 ms |
2380 KB |
Output is correct |
22 |
Correct |
1 ms |
1740 KB |
Output is correct |
23 |
Correct |
1 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
1 ms |
2380 KB |
Output is correct |
29 |
Correct |
1 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
2252 KB |
Output is correct |
31 |
Correct |
1 ms |
1996 KB |
Output is correct |
32 |
Correct |
1 ms |
2380 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
1868 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
1 ms |
2124 KB |
Output is correct |
39 |
Correct |
2 ms |
2380 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
332 KB |
Output is correct |
43 |
Correct |
65 ms |
162852 KB |
Output is correct |
44 |
Correct |
61 ms |
162060 KB |
Output is correct |
45 |
Correct |
56 ms |
162884 KB |
Output is correct |
46 |
Correct |
56 ms |
162884 KB |
Output is correct |
47 |
Correct |
60 ms |
162792 KB |
Output is correct |
48 |
Correct |
58 ms |
162076 KB |
Output is correct |
49 |
Correct |
64 ms |
162004 KB |
Output is correct |
50 |
Correct |
70 ms |
161248 KB |
Output is correct |
51 |
Correct |
56 ms |
162784 KB |
Output is correct |
52 |
Correct |
55 ms |
162772 KB |
Output is correct |
53 |
Correct |
15 ms |
44064 KB |
Output is correct |
54 |
Correct |
25 ms |
77068 KB |
Output is correct |
55 |
Correct |
36 ms |
111296 KB |
Output is correct |
56 |
Correct |
0 ms |
204 KB |
Output is correct |
57 |
Correct |
0 ms |
204 KB |
Output is correct |
58 |
Correct |
77 ms |
100332 KB |
Output is correct |
59 |
Correct |
81 ms |
118684 KB |
Output is correct |
60 |
Correct |
77 ms |
111096 KB |
Output is correct |
61 |
Correct |
77 ms |
108940 KB |
Output is correct |
62 |
Correct |
0 ms |
332 KB |
Output is correct |
63 |
Correct |
62 ms |
161324 KB |
Output is correct |
64 |
Correct |
73 ms |
154564 KB |
Output is correct |
65 |
Correct |
82 ms |
145144 KB |
Output is correct |
66 |
Correct |
76 ms |
109500 KB |
Output is correct |
67 |
Correct |
78 ms |
106616 KB |
Output is correct |
68 |
Correct |
77 ms |
112224 KB |
Output is correct |
69 |
Correct |
78 ms |
109680 KB |
Output is correct |
70 |
Correct |
80 ms |
115544 KB |
Output is correct |
71 |
Correct |
79 ms |
107512 KB |
Output is correct |
72 |
Correct |
0 ms |
204 KB |
Output is correct |
73 |
Correct |
0 ms |
332 KB |
Output is correct |