#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i=0; i<n; ++i)
#define rrep(i, n) for (int i=1; i<=n; ++i)
const int maxn = 400 + 10;
const int inf = int(1e9) + 10;
int n;
char s[maxn];
int ps[maxn][3];
int pos[3][maxn];
int dp[maxn][maxn][maxn][3];
int ma, mb, mc;
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n >> (s+1);
rrep(i, n) {
rep(j, 3) ps[i][j] = ps[i-1][j];
pos[s[i]%4-1][++ps[i][s[i]%4-1]] = i;
}
ma = ps[n][0];
mb = ps[n][1];
mc = ps[n][2];
rrep(S, n) {
for (int a=0; a<=S && a<=ma; ++a) {
for (int b=0; b<=S-a && b<=mb; ++b) {
int c = S-a-b; if (c>mc) continue;
int *d = dp[a][b][c]; rep(i, 3) d[i] = inf;
int is[3] = {a, b, c};
rep(ui, 3) { if (!is[ui]) continue;
int bm = inf;
{
--is[ui];
int *bef = dp[is[0]][is[1]][is[2]];
++is[ui];
rep(bi, 3) if (ui != bi && bef[bi] != inf) bm = min(bm, bef[bi]);
}
if (bm == inf) continue;
int df = 0;
int ci = pos[ui][is[ui]];
rep(oc, 3) if (oc != ui) {
int cu = ps[ci][oc];
int cd = is[oc];
df += abs(cu-cd);
}
d[ui] = min(d[ui], bm + df);
}
}
}
}
int *aa = dp[ma][mb][mc];
int ans = *min_element(aa, aa+3);
if (ans == inf) ans = -1;
else ans /= 2;
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
480 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
480 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2380 KB |
Output is correct |
19 |
Correct |
2 ms |
2508 KB |
Output is correct |
20 |
Correct |
2 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
1996 KB |
Output is correct |
22 |
Correct |
2 ms |
3020 KB |
Output is correct |
23 |
Correct |
1 ms |
1996 KB |
Output is correct |
24 |
Correct |
1 ms |
1484 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
1740 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
2 ms |
2380 KB |
Output is correct |
30 |
Correct |
2 ms |
1868 KB |
Output is correct |
31 |
Correct |
2 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
1868 KB |
Output is correct |
33 |
Correct |
1 ms |
588 KB |
Output is correct |
34 |
Correct |
1 ms |
716 KB |
Output is correct |
35 |
Correct |
2 ms |
1740 KB |
Output is correct |
36 |
Correct |
3 ms |
2508 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
1 ms |
1612 KB |
Output is correct |
39 |
Correct |
2 ms |
2124 KB |
Output is correct |
40 |
Correct |
1 ms |
588 KB |
Output is correct |
41 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
4 ms |
1228 KB |
Output is correct |
5 |
Correct |
3 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
2 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
16 |
Correct |
2 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
480 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2380 KB |
Output is correct |
19 |
Correct |
2 ms |
2508 KB |
Output is correct |
20 |
Correct |
2 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
1996 KB |
Output is correct |
22 |
Correct |
2 ms |
3020 KB |
Output is correct |
23 |
Correct |
1 ms |
1996 KB |
Output is correct |
24 |
Correct |
1 ms |
1484 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
1740 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
2 ms |
2380 KB |
Output is correct |
30 |
Correct |
2 ms |
1868 KB |
Output is correct |
31 |
Correct |
2 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
1868 KB |
Output is correct |
33 |
Correct |
1 ms |
588 KB |
Output is correct |
34 |
Correct |
1 ms |
716 KB |
Output is correct |
35 |
Correct |
2 ms |
1740 KB |
Output is correct |
36 |
Correct |
3 ms |
2508 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
1 ms |
1612 KB |
Output is correct |
39 |
Correct |
2 ms |
2124 KB |
Output is correct |
40 |
Correct |
1 ms |
588 KB |
Output is correct |
41 |
Correct |
1 ms |
588 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
2 ms |
1228 KB |
Output is correct |
44 |
Correct |
2 ms |
1228 KB |
Output is correct |
45 |
Correct |
4 ms |
1228 KB |
Output is correct |
46 |
Correct |
3 ms |
1228 KB |
Output is correct |
47 |
Correct |
2 ms |
1228 KB |
Output is correct |
48 |
Correct |
2 ms |
1228 KB |
Output is correct |
49 |
Correct |
2 ms |
1228 KB |
Output is correct |
50 |
Correct |
1 ms |
1228 KB |
Output is correct |
51 |
Correct |
2 ms |
1228 KB |
Output is correct |
52 |
Correct |
2 ms |
1228 KB |
Output is correct |
53 |
Correct |
1 ms |
716 KB |
Output is correct |
54 |
Correct |
1 ms |
972 KB |
Output is correct |
55 |
Correct |
1 ms |
1100 KB |
Output is correct |
56 |
Correct |
2 ms |
1228 KB |
Output is correct |
57 |
Correct |
2 ms |
1228 KB |
Output is correct |
58 |
Correct |
144 ms |
90932 KB |
Output is correct |
59 |
Correct |
116 ms |
83652 KB |
Output is correct |
60 |
Correct |
136 ms |
90120 KB |
Output is correct |
61 |
Correct |
122 ms |
88388 KB |
Output is correct |
62 |
Correct |
7 ms |
5452 KB |
Output is correct |
63 |
Correct |
12 ms |
8828 KB |
Output is correct |
64 |
Correct |
48 ms |
31980 KB |
Output is correct |
65 |
Correct |
89 ms |
45156 KB |
Output is correct |
66 |
Correct |
164 ms |
87748 KB |
Output is correct |
67 |
Correct |
125 ms |
75340 KB |
Output is correct |
68 |
Correct |
134 ms |
86384 KB |
Output is correct |
69 |
Correct |
172 ms |
90436 KB |
Output is correct |
70 |
Correct |
158 ms |
82480 KB |
Output is correct |
71 |
Correct |
158 ms |
99492 KB |
Output is correct |
72 |
Correct |
71 ms |
94516 KB |
Output is correct |
73 |
Correct |
9 ms |
8524 KB |
Output is correct |