#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int r, g, b;
const int MXN = 405;
int dp[MXN][MXN][MXN][4];
vector<int> rs, gs, bs;
int pfr[MXN];
int pfg[MXN];
int pfb[MXN];
signed main() {
cin >> n >> s;
int rc = 0, bc = 0, gc = 0;
for (int i=0; i<n; i++) {
if (s[i] == 'R') {
rs.push_back(i+1);
r++;
rc++;
}
if (s[i] == 'G') {
gs.push_back(i+1);
g++;
gc++;
}
if (s[i] == 'Y') {
bs.push_back(i+1);
b++;
bc++;
}
pfr[i+1] = r;
pfg[i+1] = g;
pfb[i+1] = b;
}
s = ' ' + s;
for (int ind=1; ind<=n; ind++) {
for (int r=0; r<=rc; r++) {
for (int g=0; g<=gc; g++) {
for (int b=0; b<=bc; b++) {
if (r + g + b == ind) {
for (int whi=1; whi<=3; whi++) {
int re = INT_MAX / 2;
if (whi != 1 && r > 0) {
int h = dp[ind-1][r-1][g][1];
int l = rs[r-1];
h += max(l + max(g - pfg[l], 0) + max(b - pfb[l], 0) - ind, 0);
re = min(re, h);
}
if (whi != 2 && g > 0) {
int h = dp[ind-1][r][g-1][2];
int l = gs[g-1];
h += max(l + max(b - pfb[l], 0) + max(r - pfr[l], 0) - ind, 0);
re = min(re, h);
}
if (whi != 3 && b > 0) {
int h = dp[ind-1][r][g][3];
int l = bs[b-1];
h += max(l + max(g - pfg[l], 0) + max(r - pfr[l], 0) - ind, 0);
re = min(re, h);
}
dp[ind][r][g][whi] = re;
}
}
}
}
}
}
int re = dp[n][r][g][1];
for (int i=1; i<=3; i++) re = min(re, dp[n][r][g][i]);
if (re > n * n * 2) {
cout << -1 << endl;
}
else {
cout << re << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
768 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
768 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
4352 KB |
Output is correct |
19 |
Correct |
3 ms |
4096 KB |
Output is correct |
20 |
Correct |
3 ms |
4480 KB |
Output is correct |
21 |
Correct |
3 ms |
4096 KB |
Output is correct |
22 |
Correct |
3 ms |
4352 KB |
Output is correct |
23 |
Correct |
3 ms |
3840 KB |
Output is correct |
24 |
Correct |
2 ms |
2944 KB |
Output is correct |
25 |
Correct |
3 ms |
4224 KB |
Output is correct |
26 |
Correct |
3 ms |
4480 KB |
Output is correct |
27 |
Correct |
4 ms |
4480 KB |
Output is correct |
28 |
Correct |
3 ms |
4224 KB |
Output is correct |
29 |
Correct |
3 ms |
4352 KB |
Output is correct |
30 |
Correct |
3 ms |
3840 KB |
Output is correct |
31 |
Correct |
3 ms |
3584 KB |
Output is correct |
32 |
Correct |
3 ms |
3968 KB |
Output is correct |
33 |
Correct |
3 ms |
4224 KB |
Output is correct |
34 |
Correct |
4 ms |
4096 KB |
Output is correct |
35 |
Correct |
3 ms |
4096 KB |
Output is correct |
36 |
Correct |
3 ms |
4096 KB |
Output is correct |
37 |
Correct |
2 ms |
3200 KB |
Output is correct |
38 |
Correct |
3 ms |
3456 KB |
Output is correct |
39 |
Correct |
4 ms |
4096 KB |
Output is correct |
40 |
Correct |
1 ms |
768 KB |
Output is correct |
41 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
142 ms |
163828 KB |
Output is correct |
3 |
Correct |
130 ms |
163064 KB |
Output is correct |
4 |
Correct |
130 ms |
163832 KB |
Output is correct |
5 |
Correct |
132 ms |
163832 KB |
Output is correct |
6 |
Correct |
132 ms |
163836 KB |
Output is correct |
7 |
Correct |
133 ms |
163064 KB |
Output is correct |
8 |
Correct |
133 ms |
163064 KB |
Output is correct |
9 |
Correct |
130 ms |
162340 KB |
Output is correct |
10 |
Correct |
130 ms |
163832 KB |
Output is correct |
11 |
Correct |
131 ms |
163832 KB |
Output is correct |
12 |
Correct |
31 ms |
44536 KB |
Output is correct |
13 |
Correct |
54 ms |
77816 KB |
Output is correct |
14 |
Correct |
83 ms |
112120 KB |
Output is correct |
15 |
Correct |
134 ms |
163832 KB |
Output is correct |
16 |
Correct |
131 ms |
163832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
768 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
4352 KB |
Output is correct |
19 |
Correct |
3 ms |
4096 KB |
Output is correct |
20 |
Correct |
3 ms |
4480 KB |
Output is correct |
21 |
Correct |
3 ms |
4096 KB |
Output is correct |
22 |
Correct |
3 ms |
4352 KB |
Output is correct |
23 |
Correct |
3 ms |
3840 KB |
Output is correct |
24 |
Correct |
2 ms |
2944 KB |
Output is correct |
25 |
Correct |
3 ms |
4224 KB |
Output is correct |
26 |
Correct |
3 ms |
4480 KB |
Output is correct |
27 |
Correct |
4 ms |
4480 KB |
Output is correct |
28 |
Correct |
3 ms |
4224 KB |
Output is correct |
29 |
Correct |
3 ms |
4352 KB |
Output is correct |
30 |
Correct |
3 ms |
3840 KB |
Output is correct |
31 |
Correct |
3 ms |
3584 KB |
Output is correct |
32 |
Correct |
3 ms |
3968 KB |
Output is correct |
33 |
Correct |
3 ms |
4224 KB |
Output is correct |
34 |
Correct |
4 ms |
4096 KB |
Output is correct |
35 |
Correct |
3 ms |
4096 KB |
Output is correct |
36 |
Correct |
3 ms |
4096 KB |
Output is correct |
37 |
Correct |
2 ms |
3200 KB |
Output is correct |
38 |
Correct |
3 ms |
3456 KB |
Output is correct |
39 |
Correct |
4 ms |
4096 KB |
Output is correct |
40 |
Correct |
1 ms |
768 KB |
Output is correct |
41 |
Correct |
2 ms |
2688 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
142 ms |
163828 KB |
Output is correct |
44 |
Correct |
130 ms |
163064 KB |
Output is correct |
45 |
Correct |
130 ms |
163832 KB |
Output is correct |
46 |
Correct |
132 ms |
163832 KB |
Output is correct |
47 |
Correct |
132 ms |
163836 KB |
Output is correct |
48 |
Correct |
133 ms |
163064 KB |
Output is correct |
49 |
Correct |
133 ms |
163064 KB |
Output is correct |
50 |
Correct |
130 ms |
162340 KB |
Output is correct |
51 |
Correct |
130 ms |
163832 KB |
Output is correct |
52 |
Correct |
131 ms |
163832 KB |
Output is correct |
53 |
Correct |
31 ms |
44536 KB |
Output is correct |
54 |
Correct |
54 ms |
77816 KB |
Output is correct |
55 |
Correct |
83 ms |
112120 KB |
Output is correct |
56 |
Correct |
134 ms |
163832 KB |
Output is correct |
57 |
Correct |
131 ms |
163832 KB |
Output is correct |
58 |
Execution timed out |
803 ms |
177252 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |