// I'm a paragraph. Click here to add your own text and edit me. It’s easy.
// Just click “Edit Text” or double click me to add your own content
// and make changes to the font. Feel free to drag and drop me anywhere
// you like on your page. I’m a great place for you to tell a story
// and let your users know a little more about you.
#include <bits/stdc++.h>
using namespace std;
#define filename ""
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define xx first
#define yy second
#define sz(x) ((int) size(x))
#define all(x) begin(x), end(x)
#define rep(i, a, b) for(int i = a; i < b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
const int oo = 1e7;
const int N = 400 + 1;
int dp[N][N][N][3];
int idx[3][N];
int ns[3];
int pos[3][N];
void solve() {
int n;
string s;
cin >> n >> s;
ns[0] = ns[1] = ns[2] = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'R') pos[0][ns[0]++] = i;
if (s[i] == 'G') pos[1][ns[1]++] = i;
if (s[i] == 'Y') pos[2][ns[2]++] = i;
for (int j = 0; j < 3; j++)
idx[j][i] = ns[j];
}
for (int i = ns[0]; i >= 0; i--)
for (int j = ns[1]; j >= 0; j--)
for (int k = ns[2]; k >= 0; k--)
for (int c = 0; c < 3; c++) {
bool place0 = i < ns[0];
bool place1 = j < ns[1];
bool place2 = k < ns[2];
if (!place0 && !place1 && !place2) {
dp[i][j][k][c] = 0;
continue;
}
int first = oo;
if (place0) first = min(first, pos[0][i]);
if (place1) first = min(first, pos[1][j]);
if (place2) first = min(first, pos[2][k]);
place0 &= c != 0;
place1 &= c != 1;
place2 &= c != 2;
int p = i + j + k;
int ans = oo;
if (place0) {
int to = pos[0][i];
int shift = max(0, j - idx[1][to]) + max(0, k - idx[2][to]);
to += shift;
ans = min(ans, abs(p - to) + dp[i + 1][j][k][0]);
}
if (place1) {
int to = pos[1][j];
int shift = max(0, i - idx[0][to]) + max(0, k - idx[2][to]);
to += shift;
ans = min(ans, abs(p - to) + dp[i][j + 1][k][1]);
}
if (place2) {
int to = pos[2][k];
int shift = max(0, j - idx[1][to]) + max(0, i - idx[0][to]);
to += shift;
ans = min(ans, abs(p - to) + dp[i][j][k + 1][2]);
}
dp[i][j][k][c] = ans;
}
int ans = min(dp[0][0][0][0], dp[0][0][0][1]);
if (ans >= oo)
ans = -1;
cout << ans << "\n";
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
// freopen(filename ".in", "r", stdin); freopen(filename ".out", "w", stdout);
// int t; cin >> t; while (t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
2252 KB |
Output is correct |
19 |
Correct |
1 ms |
1868 KB |
Output is correct |
20 |
Correct |
1 ms |
2252 KB |
Output is correct |
21 |
Correct |
1 ms |
2380 KB |
Output is correct |
22 |
Correct |
1 ms |
1612 KB |
Output is correct |
23 |
Correct |
1 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
2 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
4172 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
1 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
2252 KB |
Output is correct |
31 |
Correct |
1 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
3 ms |
3788 KB |
Output is correct |
34 |
Correct |
3 ms |
3532 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
1 ms |
1868 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
1 ms |
2124 KB |
Output is correct |
39 |
Correct |
2 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
2124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
71 ms |
162884 KB |
Output is correct |
3 |
Correct |
75 ms |
162116 KB |
Output is correct |
4 |
Correct |
78 ms |
162964 KB |
Output is correct |
5 |
Correct |
72 ms |
162960 KB |
Output is correct |
6 |
Correct |
71 ms |
162884 KB |
Output is correct |
7 |
Correct |
73 ms |
162116 KB |
Output is correct |
8 |
Correct |
70 ms |
162116 KB |
Output is correct |
9 |
Correct |
69 ms |
161240 KB |
Output is correct |
10 |
Correct |
81 ms |
162920 KB |
Output is correct |
11 |
Correct |
78 ms |
162940 KB |
Output is correct |
12 |
Correct |
24 ms |
44032 KB |
Output is correct |
13 |
Correct |
33 ms |
77116 KB |
Output is correct |
14 |
Correct |
52 ms |
111328 KB |
Output is correct |
15 |
Correct |
77 ms |
162916 KB |
Output is correct |
16 |
Correct |
70 ms |
162884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
2252 KB |
Output is correct |
19 |
Correct |
1 ms |
1868 KB |
Output is correct |
20 |
Correct |
1 ms |
2252 KB |
Output is correct |
21 |
Correct |
1 ms |
2380 KB |
Output is correct |
22 |
Correct |
1 ms |
1612 KB |
Output is correct |
23 |
Correct |
1 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
2 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
4172 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
1 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
2252 KB |
Output is correct |
31 |
Correct |
1 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
3 ms |
3788 KB |
Output is correct |
34 |
Correct |
3 ms |
3532 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
1 ms |
1868 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
1 ms |
2124 KB |
Output is correct |
39 |
Correct |
2 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
2124 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
71 ms |
162884 KB |
Output is correct |
44 |
Correct |
75 ms |
162116 KB |
Output is correct |
45 |
Correct |
78 ms |
162964 KB |
Output is correct |
46 |
Correct |
72 ms |
162960 KB |
Output is correct |
47 |
Correct |
71 ms |
162884 KB |
Output is correct |
48 |
Correct |
73 ms |
162116 KB |
Output is correct |
49 |
Correct |
70 ms |
162116 KB |
Output is correct |
50 |
Correct |
69 ms |
161240 KB |
Output is correct |
51 |
Correct |
81 ms |
162920 KB |
Output is correct |
52 |
Correct |
78 ms |
162940 KB |
Output is correct |
53 |
Correct |
24 ms |
44032 KB |
Output is correct |
54 |
Correct |
33 ms |
77116 KB |
Output is correct |
55 |
Correct |
52 ms |
111328 KB |
Output is correct |
56 |
Correct |
77 ms |
162916 KB |
Output is correct |
57 |
Correct |
70 ms |
162884 KB |
Output is correct |
58 |
Correct |
94 ms |
75404 KB |
Output is correct |
59 |
Correct |
99 ms |
95568 KB |
Output is correct |
60 |
Correct |
102 ms |
86844 KB |
Output is correct |
61 |
Correct |
96 ms |
84064 KB |
Output is correct |
62 |
Correct |
72 ms |
160576 KB |
Output is correct |
63 |
Correct |
76 ms |
160196 KB |
Output is correct |
64 |
Correct |
101 ms |
150072 KB |
Output is correct |
65 |
Correct |
104 ms |
138560 KB |
Output is correct |
66 |
Correct |
121 ms |
85192 KB |
Output is correct |
67 |
Correct |
95 ms |
82052 KB |
Output is correct |
68 |
Correct |
95 ms |
87708 KB |
Output is correct |
69 |
Correct |
94 ms |
84676 KB |
Output is correct |
70 |
Correct |
100 ms |
91580 KB |
Output is correct |
71 |
Correct |
93 ms |
83084 KB |
Output is correct |
72 |
Correct |
34 ms |
41156 KB |
Output is correct |
73 |
Correct |
5 ms |
2764 KB |
Output is correct |