#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define fr first
#define sc second
#define vi vector < int >
#define vl vector < ll >
#define pi pair < int, int >
#define pii pair < int, pi >
#define vii vector < pi >
const int N = 1507;
const int INF = 1e9 + 7;
int posr[N], posg[N], posy[N];
int pr[N], pg[N], py[N];
int r, g, y;
int n;
string s;
main(){
cin >> n >> s;
s = ' ' + s;
for (int i = 1; i <= n; i++){
pr[i] = pr[i - 1];
pg[i] = pg[i - 1];
py[i] = py[i - 1];
if (s[i] == 'R'){
r++;
posr[r] = i;
pr[i]++;
}
else if (s[i] == 'Y'){
y++;
posy[y] = i;
py[i]++;
}
else {
g++;
posg[g] = i;
pg[i]++;
}
}
int dp[r + 1][y + 1][g + 1][3];
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (int i = 0; i <= r; i++){
for (int j = 0; j <= y; j++){
for (int c = 0; c <= g; c++){
if (i == 0 && j == 0 && c == 0)
continue;
dp[i][j][c][0] =
dp[i][j][c][1] =
dp[i][j][c][2] = INF;
if (i){
int p = posr[i];
dp[i][j][c][1] = min(dp[i][j][c][1], dp[i - 1][j][c][0] + abs(i + j + c - (p + max(0, j - py[p]) + max(0, c - pg[p]))));
dp[i][j][c][2] = min(dp[i][j][c][2], dp[i - 1][j][c][0] + abs(i + j + c - (p + max(0, j - py[p]) + max(0, c - pg[p]))));
}
if (j){
int p = posy[j];
dp[i][j][c][0] = min(dp[i][j][c][0], dp[i][j - 1][c][1] + abs(i + j + c - (p + max(0, i - pr[p]) + max(0, c - pg[p]))));
dp[i][j][c][2] = min(dp[i][j][c][2], dp[i][j - 1][c][1] + abs(i + j + c - (p + max(0, i - pr[p]) + max(0, c - pg[p]))));
}
if (c){
int p = posg[c];
dp[i][j][c][0] = min(dp[i][j][c][0], dp[i][j][c - 1][2] + abs(i + j + c - (p + max(0, i - pr[p]) + max(0, j - py[p]))));
dp[i][j][c][1] = min(dp[i][j][c][1], dp[i][j][c - 1][2] + abs(i + j + c - (p + max(0, i - pr[p]) + max(0, j - py[p]))));
}
}
}
}
int ans = min(dp[r][y][g][0], min(dp[r][y][g][1], dp[r][y][g][2]));
if (ans == INF)
puts("-1");
else
cout << ans;
}
Compilation message
joi2019_ho_t3.cpp:24:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
2 ms |
384 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
2 ms |
384 KB |
Output is correct |
37 |
Correct |
2 ms |
384 KB |
Output is correct |
38 |
Correct |
2 ms |
384 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
896 KB |
Output is correct |
4 |
Correct |
3 ms |
896 KB |
Output is correct |
5 |
Correct |
3 ms |
896 KB |
Output is correct |
6 |
Correct |
3 ms |
896 KB |
Output is correct |
7 |
Correct |
3 ms |
896 KB |
Output is correct |
8 |
Correct |
3 ms |
896 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
3 ms |
896 KB |
Output is correct |
11 |
Correct |
2 ms |
768 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
768 KB |
Output is correct |
16 |
Correct |
3 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
2 ms |
384 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
2 ms |
384 KB |
Output is correct |
37 |
Correct |
2 ms |
384 KB |
Output is correct |
38 |
Correct |
2 ms |
384 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
3 ms |
384 KB |
Output is correct |
43 |
Correct |
5 ms |
768 KB |
Output is correct |
44 |
Correct |
3 ms |
896 KB |
Output is correct |
45 |
Correct |
3 ms |
896 KB |
Output is correct |
46 |
Correct |
3 ms |
896 KB |
Output is correct |
47 |
Correct |
3 ms |
896 KB |
Output is correct |
48 |
Correct |
3 ms |
896 KB |
Output is correct |
49 |
Correct |
3 ms |
896 KB |
Output is correct |
50 |
Correct |
3 ms |
768 KB |
Output is correct |
51 |
Correct |
3 ms |
896 KB |
Output is correct |
52 |
Correct |
2 ms |
768 KB |
Output is correct |
53 |
Correct |
2 ms |
512 KB |
Output is correct |
54 |
Correct |
3 ms |
512 KB |
Output is correct |
55 |
Correct |
3 ms |
640 KB |
Output is correct |
56 |
Correct |
2 ms |
768 KB |
Output is correct |
57 |
Correct |
3 ms |
896 KB |
Output is correct |
58 |
Correct |
46 ms |
28536 KB |
Output is correct |
59 |
Correct |
46 ms |
28460 KB |
Output is correct |
60 |
Correct |
45 ms |
28280 KB |
Output is correct |
61 |
Correct |
46 ms |
28544 KB |
Output is correct |
62 |
Correct |
6 ms |
2664 KB |
Output is correct |
63 |
Correct |
8 ms |
4480 KB |
Output is correct |
64 |
Correct |
23 ms |
14208 KB |
Output is correct |
65 |
Correct |
33 ms |
20352 KB |
Output is correct |
66 |
Correct |
45 ms |
28152 KB |
Output is correct |
67 |
Correct |
47 ms |
28152 KB |
Output is correct |
68 |
Correct |
70 ms |
28548 KB |
Output is correct |
69 |
Correct |
52 ms |
28792 KB |
Output is correct |
70 |
Correct |
49 ms |
28664 KB |
Output is correct |
71 |
Correct |
45 ms |
27996 KB |
Output is correct |
72 |
Correct |
13 ms |
7552 KB |
Output is correct |
73 |
Correct |
4 ms |
2176 KB |
Output is correct |