/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 400;
int N;
string S;
int A[N_MAX + 2][3], cnta;
int B[N_MAX + 2][3], cntb;
int C[N_MAX + 2][3], cntc;
int dp[N_MAX + 2][N_MAX + 2][N_MAX + 2][3];
void update (int &x, const int &y) {
if (y < x) {
x = y;
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
cin >> S;
for (int i = 0; i < N; i++) {
if (S[i] == 'R') {
cnta++;
A[cnta][1] = cntb;
A[cnta][2] = cntc;
} else if (S[i] == 'G') {
cntb++;
B[cntb][2] = cntc;
B[cntb][0] = cnta;
} else {
cntc++;
C[cntc][0] = cnta;
C[cntc][1] = cntb;
}
}
for (int a = 0; a <= cnta; a++) {
for (int b = 0; b <= cntb; b++) {
for (int c = 0; c <= cntc; c++) {
dp[a][b][c][0] = INT_MAX / 2;
dp[a][b][c][1] = INT_MAX / 2;
dp[a][b][c][2] = INT_MAX / 2;
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (int a = 0; a <= cnta; a++) {
for (int b = 0; b <= cntb; b++) {
for (int c = 0; c <= cntc; c++) {
if (a < cnta) {
update(dp[a + 1][b][c][0], min(dp[a][b][c][2], dp[a][b][c][1]) + abs(b - A[a + 1][1]) + abs(c - A[a + 1][2]));
}
if (b < cntb) {
update(dp[a][b + 1][c][1], min(dp[a][b][c][0], dp[a][b][c][2]) + abs(c - B[b + 1][2]) + abs(a - B[b + 1][0]));
}
if (c < cntc) {
update(dp[a][b][c + 1][2], min(dp[a][b][c][1], dp[a][b][c][0]) + abs(a - C[c + 1][0]) + abs(b - C[c + 1][1]));
}
}
}
}
int answer = INT_MAX / 2;
answer = min(answer, dp[cnta][cntb][cntc][0]);
answer = min(answer, dp[cnta][cntb][cntc][1]);
answer = min(answer, dp[cnta][cntb][cntc][2]);
cout << (answer != INT_MAX / 2 ? answer / 2 : -1) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1864 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4052 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
2248 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2372 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3524 KB |
Output is correct |
35 |
Correct |
2 ms |
2632 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2244 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
70 ms |
162976 KB |
Output is correct |
3 |
Correct |
60 ms |
162080 KB |
Output is correct |
4 |
Correct |
66 ms |
162888 KB |
Output is correct |
5 |
Correct |
64 ms |
162872 KB |
Output is correct |
6 |
Correct |
66 ms |
162896 KB |
Output is correct |
7 |
Correct |
59 ms |
162140 KB |
Output is correct |
8 |
Correct |
59 ms |
162088 KB |
Output is correct |
9 |
Correct |
62 ms |
161284 KB |
Output is correct |
10 |
Correct |
62 ms |
162924 KB |
Output is correct |
11 |
Correct |
62 ms |
162912 KB |
Output is correct |
12 |
Correct |
17 ms |
43992 KB |
Output is correct |
13 |
Correct |
31 ms |
77108 KB |
Output is correct |
14 |
Correct |
40 ms |
111340 KB |
Output is correct |
15 |
Correct |
64 ms |
162904 KB |
Output is correct |
16 |
Correct |
62 ms |
162852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1864 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4052 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
2248 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2372 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3524 KB |
Output is correct |
35 |
Correct |
2 ms |
2632 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2244 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
70 ms |
162976 KB |
Output is correct |
44 |
Correct |
60 ms |
162080 KB |
Output is correct |
45 |
Correct |
66 ms |
162888 KB |
Output is correct |
46 |
Correct |
64 ms |
162872 KB |
Output is correct |
47 |
Correct |
66 ms |
162896 KB |
Output is correct |
48 |
Correct |
59 ms |
162140 KB |
Output is correct |
49 |
Correct |
59 ms |
162088 KB |
Output is correct |
50 |
Correct |
62 ms |
161284 KB |
Output is correct |
51 |
Correct |
62 ms |
162924 KB |
Output is correct |
52 |
Correct |
62 ms |
162912 KB |
Output is correct |
53 |
Correct |
17 ms |
43992 KB |
Output is correct |
54 |
Correct |
31 ms |
77108 KB |
Output is correct |
55 |
Correct |
40 ms |
111340 KB |
Output is correct |
56 |
Correct |
64 ms |
162904 KB |
Output is correct |
57 |
Correct |
62 ms |
162852 KB |
Output is correct |
58 |
Correct |
54 ms |
75676 KB |
Output is correct |
59 |
Correct |
55 ms |
95792 KB |
Output is correct |
60 |
Correct |
54 ms |
87196 KB |
Output is correct |
61 |
Correct |
66 ms |
84188 KB |
Output is correct |
62 |
Correct |
62 ms |
160628 KB |
Output is correct |
63 |
Correct |
66 ms |
160140 KB |
Output is correct |
64 |
Correct |
68 ms |
150092 KB |
Output is correct |
65 |
Correct |
73 ms |
138424 KB |
Output is correct |
66 |
Correct |
53 ms |
85492 KB |
Output is correct |
67 |
Correct |
52 ms |
82260 KB |
Output is correct |
68 |
Correct |
53 ms |
88024 KB |
Output is correct |
69 |
Correct |
55 ms |
84840 KB |
Output is correct |
70 |
Correct |
63 ms |
91824 KB |
Output is correct |
71 |
Correct |
51 ms |
83304 KB |
Output is correct |
72 |
Correct |
26 ms |
41324 KB |
Output is correct |
73 |
Correct |
3 ms |
2884 KB |
Output is correct |