#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[401][401][401][3], prefix[400][3];
string plants;
int main() {
int n; cin >> n;
cin >> plants;
int rsum = 0, gsum = 0, ysum = 0;
for (char i: plants) {
if (i == 'R') {rsum++;}
if (i == 'G') {gsum++;}
if (i == 'Y') {ysum++;}
}
if (max(max(rsum, gsum), ysum) * 2 > rsum + gsum + ysum + 1) {
cout << -1; return 0;
}
vector<int> rvec, gvec, yvec;
int rcount = 0, gcount = 0, ycount = 0;
for (int i=0;i<n;i++) {
if (plants[i] == 'R') {
rcount++;
rvec.push_back(i);
}
if (plants[i] == 'G') {
gcount++;
gvec.push_back(i);
}
if (plants[i] == 'Y') {
ycount++;
yvec.push_back(i);
}
prefix[i][0] = rcount;
prefix[i][1] = gcount;
prefix[i][2] = ycount;
}
int rpos, gpos, ypos;
string curplants;
for (int i=0;i<rsum+1;i++) {
for (int j=0;j<gsum+1;j++) {
for (int k=0;k<ysum+1;k++) {
if (i == 0 && j == 0 && k == 0) {continue;}
if (i + j + k == 1) {
dp[i][j][k][0] = INT32_MAX; dp[i][j][k][1] = INT32_MAX; dp[i][j][k][2] = INT32_MAX;
if (i == 1) {
dp[i][j][k][0] = 0;
} else if (j == 1) {
dp[i][j][k][1] = 0;
} else {
dp[i][j][k][2] = 0;
}
//cout << i << ' ' << j << ' ' << k << ": " << dp[i][j][k][0] << ' ' << dp[i][j][k][1] << ' ' << dp[i][j][k][2] << '\n';
continue;
}
dp[i][j][k][0] = INT32_MAX; dp[i][j][k][1] = INT32_MAX; dp[i][j][k][2] = INT32_MAX;
if (i != 0) {
rpos = max((ll)0, j - prefix[rvec[i - 1]][1]) + max((ll)0, k - prefix[rvec[i - 1]][2]);
if (dp[i - 1][j][k][1] != -1) {
dp[i][j][k][0] = min(dp[i][j][k][0], rpos + dp[i - 1][j][k][1]);
}
if (dp[i - 1][j][k][2] != -1) {
dp[i][j][k][0] = min(dp[i][j][k][0], rpos + dp[i - 1][j][k][2]);
}
}
if (j != 0) {
gpos = max((ll)0, i - prefix[gvec[j -1]][0]) + max((ll)0, k - prefix[gvec[j - 1]][2]);
if (dp[i][j - 1][k][0] != -1) {
dp[i][j][k][1] = min(dp[i][j][k][1], gpos + dp[i][j - 1][k][0]);
}
if (dp[i][j - 1][k][2] != -1) {
dp[i][j][k][1] = min(dp[i][j][k][1], gpos + dp[i][j - 1][k][2]);
}
}
if (k != 0) {
ypos = max((ll)0, j - prefix[yvec[k -1]][1]) + max((ll)0, i - prefix[yvec[k - 1]][0]);
if (dp[i][j][k - 1][0] != -1) {
dp[i][j][k][2] = min(dp[i][j][k][2], ypos + dp[i][j][k - 1][0]);
}
if (dp[i][j][k - 1][1] != -1) {
dp[i][j][k][2] = min(dp[i][j][k][2], ypos + dp[i][j][k - 1][1]);
}
}
//cout << i << ' ' << j << ' ' << k << ": " << dp[i][j][k][0] << ' ' << dp[i][j][k][1] << ' ' << dp[i][j][k][2] << '\n';
}
}
}
ll ans = INT32_MAX;
ans = min(ans, min(dp[rsum][gsum][ysum][0], min(dp[rsum][gsum][ysum][1], dp[rsum][gsum][ysum][2])));
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14824 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14824 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
22360 KB |
Output is correct |
19 |
Correct |
2 ms |
19804 KB |
Output is correct |
20 |
Correct |
3 ms |
20316 KB |
Output is correct |
21 |
Correct |
3 ms |
26200 KB |
Output is correct |
22 |
Correct |
3 ms |
19716 KB |
Output is correct |
23 |
Correct |
2 ms |
20060 KB |
Output is correct |
24 |
Correct |
2 ms |
24924 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
28764 KB |
Output is correct |
28 |
Correct |
3 ms |
26204 KB |
Output is correct |
29 |
Correct |
3 ms |
22296 KB |
Output is correct |
30 |
Correct |
3 ms |
21852 KB |
Output is correct |
31 |
Correct |
3 ms |
25692 KB |
Output is correct |
32 |
Correct |
3 ms |
22108 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
3 ms |
26460 KB |
Output is correct |
36 |
Correct |
3 ms |
27740 KB |
Output is correct |
37 |
Correct |
3 ms |
25436 KB |
Output is correct |
38 |
Correct |
3 ms |
21596 KB |
Output is correct |
39 |
Correct |
2 ms |
20060 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
57 ms |
183608 KB |
Output is correct |
3 |
Correct |
61 ms |
182200 KB |
Output is correct |
4 |
Correct |
56 ms |
183124 KB |
Output is correct |
5 |
Correct |
56 ms |
183124 KB |
Output is correct |
6 |
Correct |
64 ms |
184844 KB |
Output is correct |
7 |
Correct |
57 ms |
182296 KB |
Output is correct |
8 |
Correct |
61 ms |
184080 KB |
Output is correct |
9 |
Correct |
58 ms |
183376 KB |
Output is correct |
10 |
Correct |
59 ms |
184912 KB |
Output is correct |
11 |
Correct |
62 ms |
184912 KB |
Output is correct |
12 |
Correct |
17 ms |
67368 KB |
Output is correct |
13 |
Correct |
31 ms |
99920 KB |
Output is correct |
14 |
Correct |
44 ms |
133972 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14824 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
22360 KB |
Output is correct |
19 |
Correct |
2 ms |
19804 KB |
Output is correct |
20 |
Correct |
3 ms |
20316 KB |
Output is correct |
21 |
Correct |
3 ms |
26200 KB |
Output is correct |
22 |
Correct |
3 ms |
19716 KB |
Output is correct |
23 |
Correct |
2 ms |
20060 KB |
Output is correct |
24 |
Correct |
2 ms |
24924 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
28764 KB |
Output is correct |
28 |
Correct |
3 ms |
26204 KB |
Output is correct |
29 |
Correct |
3 ms |
22296 KB |
Output is correct |
30 |
Correct |
3 ms |
21852 KB |
Output is correct |
31 |
Correct |
3 ms |
25692 KB |
Output is correct |
32 |
Correct |
3 ms |
22108 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
3 ms |
26460 KB |
Output is correct |
36 |
Correct |
3 ms |
27740 KB |
Output is correct |
37 |
Correct |
3 ms |
25436 KB |
Output is correct |
38 |
Correct |
3 ms |
21596 KB |
Output is correct |
39 |
Correct |
2 ms |
20060 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
57 ms |
183608 KB |
Output is correct |
44 |
Correct |
61 ms |
182200 KB |
Output is correct |
45 |
Correct |
56 ms |
183124 KB |
Output is correct |
46 |
Correct |
56 ms |
183124 KB |
Output is correct |
47 |
Correct |
64 ms |
184844 KB |
Output is correct |
48 |
Correct |
57 ms |
182296 KB |
Output is correct |
49 |
Correct |
61 ms |
184080 KB |
Output is correct |
50 |
Correct |
58 ms |
183376 KB |
Output is correct |
51 |
Correct |
59 ms |
184912 KB |
Output is correct |
52 |
Correct |
62 ms |
184912 KB |
Output is correct |
53 |
Correct |
17 ms |
67368 KB |
Output is correct |
54 |
Correct |
31 ms |
99920 KB |
Output is correct |
55 |
Correct |
44 ms |
133972 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
64 ms |
139604 KB |
Output is correct |
59 |
Correct |
72 ms |
157036 KB |
Output is correct |
60 |
Correct |
69 ms |
149584 KB |
Output is correct |
61 |
Correct |
67 ms |
147256 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
63 ms |
183588 KB |
Output is correct |
64 |
Correct |
72 ms |
185064 KB |
Output is correct |
65 |
Correct |
68 ms |
176720 KB |
Output is correct |
66 |
Correct |
64 ms |
147792 KB |
Output is correct |
67 |
Correct |
64 ms |
143444 KB |
Output is correct |
68 |
Correct |
63 ms |
150608 KB |
Output is correct |
69 |
Correct |
66 ms |
147028 KB |
Output is correct |
70 |
Correct |
76 ms |
153780 KB |
Output is correct |
71 |
Correct |
64 ms |
144524 KB |
Output is correct |
72 |
Correct |
0 ms |
344 KB |
Output is correct |
73 |
Correct |
0 ms |
344 KB |
Output is correct |