#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <deque>
#include <map>
#include <set>
#include <complex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <random>
#define ft first
#define sc second
#define pb push_back
#define len(v) (int)v.size()
// #define int ll
using namespace std;
typedef long long ll;
int inf = 1e9 + 100;
const int maxn = 401;
int posa[maxn];
int posb[maxn];
int posc[maxn];
vector<int> a, b, c;
int maxa = 0, maxb = 0, maxc = 0;
int cost(int a1, int b1, int c1, int num) {
int pos = 0;
if(num == 0)
pos = a[a1];
if(num == 1)
pos = b[b1];
if(num == 2)
pos = c[c1];
int pos1 = pos;
pos += max(0, posa[pos1] - (maxa - a1));
pos += max(0, posb[pos1] - (maxb - b1));
pos += max(0, posc[pos1] - (maxc - c1));
// cout << a1 << " " << b1 << " " << c1 << " " << (pos - (a1 + b1 + c1)) << " " << pos1 << " " << maxc << " " << posc[pos1] << endl;
return pos - (a1 + b1 + c1);
}
signed main() {
#ifdef PC
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < len(s); i++) {
if(s[i] == 'R')
s[i] = 'a';
if(s[i] == 'G')
s[i] = 'b';
if(s[i] == 'Y')
s[i] = 'c';
posa[i] = posb[i] = posc[i] = 0;
}
for (int i = 0; i < len(s); i++) {
if(s[i] == 'a')
a.pb(i);
if(s[i] == 'b')
b.pb(i);
if(s[i] == 'c')
c.pb(i);
}
for (int i = len(s) - 1; i >= 0; i--) {
posa[i] = posa[i + 1];
posb[i] = posb[i + 1];
posc[i] = posc[i + 1];
if(s[i] == 'a')
posa[i]++;
if(s[i] == 'b')
posb[i]++;
if(s[i] == 'c')
posc[i]++;
}
maxa = len(a), maxb = len(b), maxc = len(c);
// cout << maxa << " " << maxb << " " << maxc << endl;
int dp[maxa + 1][maxb + 1][maxc + 1][3];
for (int i = 0; i <= maxa; i++) {
for (int j = 0; j <= maxb; j++) {
for (int z = 0; z <= maxc; z++) {
for (int pr = 0; pr < 3; pr++)
dp[i][j][z][pr] = inf;
}
}
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int i = 0; i <= maxa; i++) {
for (int j = 0; j <= maxb; j++) {
for (int z = 0; z <= maxc; z++) {
for (int pr = 0; pr < 3; pr++) {
// cout << i << " " << j << " " << z << " " << pr << " " << dp[i][j][z][pr] << endl;
for (char nxt = 'a'; nxt <= 'c'; nxt++) {
if(nxt - 'a' == pr)
continue;
if(nxt == 'a' && i == maxa)
continue;
if(nxt == 'b' && j == maxb)
continue;
if(nxt == 'c' && z == maxc)
continue;
if(nxt == 'a') {
dp[i + 1][j][z][0] = min(dp[i + 1][j][z][0], dp[i][j][z][pr] + cost(i, j, z, 0));
}
if(nxt == 'b') {
dp[i][j + 1][z][1] = min(dp[i][j + 1][z][1], dp[i][j][z][pr] + cost(i, j, z, 1));
}
if(nxt == 'c') {
dp[i][j][z + 1][2] = min(dp[i][j][z + 1][2], dp[i][j][z][pr] + cost(i, j, z, 2));
}
}
}
}
}
}
int ans = min(dp[maxa][maxb][maxc][0], min(dp[maxa][maxb][maxc][1], dp[maxa][maxb][maxc][2]));
if(ans == inf)
ans = -1;
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
5 ms |
384 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
768 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Output is correct |
4 |
Correct |
6 ms |
768 KB |
Output is correct |
5 |
Correct |
6 ms |
768 KB |
Output is correct |
6 |
Correct |
6 ms |
768 KB |
Output is correct |
7 |
Correct |
6 ms |
768 KB |
Output is correct |
8 |
Correct |
6 ms |
768 KB |
Output is correct |
9 |
Correct |
6 ms |
768 KB |
Output is correct |
10 |
Correct |
6 ms |
768 KB |
Output is correct |
11 |
Correct |
6 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
640 KB |
Output is correct |
15 |
Correct |
6 ms |
768 KB |
Output is correct |
16 |
Correct |
6 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
5 ms |
384 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |
43 |
Correct |
6 ms |
768 KB |
Output is correct |
44 |
Correct |
6 ms |
768 KB |
Output is correct |
45 |
Correct |
6 ms |
768 KB |
Output is correct |
46 |
Correct |
6 ms |
768 KB |
Output is correct |
47 |
Correct |
6 ms |
768 KB |
Output is correct |
48 |
Correct |
6 ms |
768 KB |
Output is correct |
49 |
Correct |
6 ms |
768 KB |
Output is correct |
50 |
Correct |
6 ms |
768 KB |
Output is correct |
51 |
Correct |
6 ms |
768 KB |
Output is correct |
52 |
Correct |
6 ms |
768 KB |
Output is correct |
53 |
Correct |
5 ms |
512 KB |
Output is correct |
54 |
Correct |
5 ms |
512 KB |
Output is correct |
55 |
Correct |
6 ms |
640 KB |
Output is correct |
56 |
Correct |
6 ms |
768 KB |
Output is correct |
57 |
Correct |
6 ms |
768 KB |
Output is correct |
58 |
Correct |
111 ms |
28416 KB |
Output is correct |
59 |
Correct |
118 ms |
28544 KB |
Output is correct |
60 |
Correct |
109 ms |
28288 KB |
Output is correct |
61 |
Correct |
110 ms |
28544 KB |
Output is correct |
62 |
Correct |
14 ms |
2688 KB |
Output is correct |
63 |
Correct |
20 ms |
4480 KB |
Output is correct |
64 |
Correct |
60 ms |
14328 KB |
Output is correct |
65 |
Correct |
84 ms |
20436 KB |
Output is correct |
66 |
Correct |
117 ms |
28160 KB |
Output is correct |
67 |
Correct |
114 ms |
28160 KB |
Output is correct |
68 |
Correct |
121 ms |
28664 KB |
Output is correct |
69 |
Correct |
114 ms |
28800 KB |
Output is correct |
70 |
Correct |
118 ms |
28672 KB |
Output is correct |
71 |
Correct |
109 ms |
28024 KB |
Output is correct |
72 |
Correct |
32 ms |
7424 KB |
Output is correct |
73 |
Correct |
12 ms |
2176 KB |
Output is correct |