#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
#define int long long
#define nl '\n'
#define io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
mt19937 rng((unsigned)chrono::steady_clock::now().time_since_epoch().count());
const int mod = 1000000007, mod2 = 998244353;
// change this
const int N = 405;
int n, si[3], arr[N], dp[N][N][N][3], pf[3][N];
vector<int> pos[3];
int sum(int i, int x, int y) {
// (x, y]
if (x > y) return 0;
return pf[i][y] - pf[i][x];
}
signed main() {
io;
cin >> n;
for (int i=0; i<3; i++) pos[i].push_back(0);
for (int i=1; i<=n; i++) {
char c;
cin >> c;
if (c == 'R') arr[i] = 0;
else if (c == 'G') arr[i] = 1;
else arr[i] = 2;
si[arr[i]]++; pf[arr[i]][i]++;
pos[arr[i]].push_back(i);
for (int j=0; j<3; j++) pf[j][i] += pf[j][i-1];
}
for (int i=0; i<=si[0]; i++) {
for (int j=0; j<=si[1]; j++) {
for (int k=0; k<=si[2]; k++) {
for (int tak=0; tak<3; tak++) {
if (!(i+j+k)) continue;
// i: R, j: G, k: Y, tak: which one to take
dp[i][j][k][tak] = 1e9;
for (int prev=0; prev<3; prev++) {
// what was the previous one?
if (prev == tak) continue; // collision
if (tak == 0) {
if (!i) break;
dp[i][j][k][tak] = min(dp[i][j][k][tak],
dp[i-1][j][k][prev]
+ sum(1, pos[1][j], pos[0][i])
+ sum(2, pos[2][k], pos[0][i])
);
} else if (tak == 1) {
if (!j) break;
dp[i][j][k][tak] = min(dp[i][j][k][tak],
dp[i][j-1][k][prev]
+ sum(0, pos[0][i], pos[1][j])
+ sum(2, pos[2][k], pos[1][j])
);
} else {
if (!k) break;
dp[i][j][k][tak] = min(dp[i][j][k][tak],
dp[i][j][k-1][prev]
+ sum(0, pos[0][i], pos[2][k])
+ sum(1, pos[1][j], pos[2][k])
);
}
}
}
}
}
}
int ans = 1e9;
for (int i=0; i<3; i++) ans = min(ans, dp[si[0]][si[1]][si[2]][i]);
cout << (ans >= 1e9 ? -1 : ans) << nl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
2540 KB |
Output is correct |
19 |
Correct |
2 ms |
2028 KB |
Output is correct |
20 |
Correct |
2 ms |
2432 KB |
Output is correct |
21 |
Correct |
2 ms |
2540 KB |
Output is correct |
22 |
Correct |
2 ms |
1772 KB |
Output is correct |
23 |
Correct |
2 ms |
2284 KB |
Output is correct |
24 |
Correct |
2 ms |
1772 KB |
Output is correct |
25 |
Correct |
2 ms |
4076 KB |
Output is correct |
26 |
Correct |
3 ms |
4204 KB |
Output is correct |
27 |
Correct |
3 ms |
3180 KB |
Output is correct |
28 |
Correct |
2 ms |
2540 KB |
Output is correct |
29 |
Correct |
2 ms |
2412 KB |
Output is correct |
30 |
Correct |
2 ms |
2412 KB |
Output is correct |
31 |
Correct |
2 ms |
2156 KB |
Output is correct |
32 |
Correct |
2 ms |
2540 KB |
Output is correct |
33 |
Correct |
2 ms |
3948 KB |
Output is correct |
34 |
Correct |
3 ms |
3692 KB |
Output is correct |
35 |
Correct |
2 ms |
2796 KB |
Output is correct |
36 |
Correct |
2 ms |
2028 KB |
Output is correct |
37 |
Correct |
2 ms |
1900 KB |
Output is correct |
38 |
Correct |
2 ms |
2284 KB |
Output is correct |
39 |
Correct |
2 ms |
2540 KB |
Output is correct |
40 |
Correct |
1 ms |
492 KB |
Output is correct |
41 |
Correct |
2 ms |
2284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
81 ms |
164076 KB |
Output is correct |
3 |
Correct |
81 ms |
163308 KB |
Output is correct |
4 |
Correct |
83 ms |
164076 KB |
Output is correct |
5 |
Correct |
83 ms |
164272 KB |
Output is correct |
6 |
Correct |
83 ms |
164076 KB |
Output is correct |
7 |
Correct |
83 ms |
163308 KB |
Output is correct |
8 |
Correct |
81 ms |
163308 KB |
Output is correct |
9 |
Correct |
80 ms |
162540 KB |
Output is correct |
10 |
Correct |
83 ms |
164076 KB |
Output is correct |
11 |
Correct |
89 ms |
164076 KB |
Output is correct |
12 |
Correct |
24 ms |
44396 KB |
Output is correct |
13 |
Correct |
42 ms |
77676 KB |
Output is correct |
14 |
Correct |
57 ms |
112236 KB |
Output is correct |
15 |
Correct |
82 ms |
164076 KB |
Output is correct |
16 |
Correct |
83 ms |
164076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
2540 KB |
Output is correct |
19 |
Correct |
2 ms |
2028 KB |
Output is correct |
20 |
Correct |
2 ms |
2432 KB |
Output is correct |
21 |
Correct |
2 ms |
2540 KB |
Output is correct |
22 |
Correct |
2 ms |
1772 KB |
Output is correct |
23 |
Correct |
2 ms |
2284 KB |
Output is correct |
24 |
Correct |
2 ms |
1772 KB |
Output is correct |
25 |
Correct |
2 ms |
4076 KB |
Output is correct |
26 |
Correct |
3 ms |
4204 KB |
Output is correct |
27 |
Correct |
3 ms |
3180 KB |
Output is correct |
28 |
Correct |
2 ms |
2540 KB |
Output is correct |
29 |
Correct |
2 ms |
2412 KB |
Output is correct |
30 |
Correct |
2 ms |
2412 KB |
Output is correct |
31 |
Correct |
2 ms |
2156 KB |
Output is correct |
32 |
Correct |
2 ms |
2540 KB |
Output is correct |
33 |
Correct |
2 ms |
3948 KB |
Output is correct |
34 |
Correct |
3 ms |
3692 KB |
Output is correct |
35 |
Correct |
2 ms |
2796 KB |
Output is correct |
36 |
Correct |
2 ms |
2028 KB |
Output is correct |
37 |
Correct |
2 ms |
1900 KB |
Output is correct |
38 |
Correct |
2 ms |
2284 KB |
Output is correct |
39 |
Correct |
2 ms |
2540 KB |
Output is correct |
40 |
Correct |
1 ms |
492 KB |
Output is correct |
41 |
Correct |
2 ms |
2284 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
81 ms |
164076 KB |
Output is correct |
44 |
Correct |
81 ms |
163308 KB |
Output is correct |
45 |
Correct |
83 ms |
164076 KB |
Output is correct |
46 |
Correct |
83 ms |
164272 KB |
Output is correct |
47 |
Correct |
83 ms |
164076 KB |
Output is correct |
48 |
Correct |
83 ms |
163308 KB |
Output is correct |
49 |
Correct |
81 ms |
163308 KB |
Output is correct |
50 |
Correct |
80 ms |
162540 KB |
Output is correct |
51 |
Correct |
83 ms |
164076 KB |
Output is correct |
52 |
Correct |
89 ms |
164076 KB |
Output is correct |
53 |
Correct |
24 ms |
44396 KB |
Output is correct |
54 |
Correct |
42 ms |
77676 KB |
Output is correct |
55 |
Correct |
57 ms |
112236 KB |
Output is correct |
56 |
Correct |
82 ms |
164076 KB |
Output is correct |
57 |
Correct |
83 ms |
164076 KB |
Output is correct |
58 |
Correct |
97 ms |
120684 KB |
Output is correct |
59 |
Correct |
107 ms |
137964 KB |
Output is correct |
60 |
Correct |
101 ms |
130156 KB |
Output is correct |
61 |
Correct |
100 ms |
128236 KB |
Output is correct |
62 |
Correct |
85 ms |
163692 KB |
Output is correct |
63 |
Correct |
88 ms |
164972 KB |
Output is correct |
64 |
Correct |
99 ms |
164588 KB |
Output is correct |
65 |
Correct |
105 ms |
159084 KB |
Output is correct |
66 |
Correct |
99 ms |
128364 KB |
Output is correct |
67 |
Correct |
99 ms |
125804 KB |
Output is correct |
68 |
Correct |
103 ms |
131436 KB |
Output is correct |
69 |
Correct |
100 ms |
129132 KB |
Output is correct |
70 |
Correct |
106 ms |
134892 KB |
Output is correct |
71 |
Correct |
98 ms |
126444 KB |
Output is correct |
72 |
Correct |
37 ms |
49772 KB |
Output is correct |
73 |
Correct |
6 ms |
5484 KB |
Output is correct |