// 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];
// vi pos[3];
int go(int i, int j, int k, int c) {
bool place0 = i < ns[0];
bool place1 = j < ns[1];
bool place2 = k < ns[2];
if (!place0 && !place1 && !place2)
return 0;
if (dp[i][j][k][c] != -1)
return dp[i][j][k][c];
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) + go(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) + go(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) + go(i, j, k + 1, 2));
}
return dp[i][j][k][c] = ans;
}
void solve() {
memset(dp, -1, sizeof(dp));
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++)
go(i, j, k, c);
int ans = min({go(0, 0, 0, 0), go(0, 0, 0, 1), go(0, 0, 0, 2)});
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 |
318 ms |
757312 KB |
Output is correct |
2 |
Correct |
336 ms |
757316 KB |
Output is correct |
3 |
Correct |
328 ms |
757324 KB |
Output is correct |
4 |
Correct |
328 ms |
757392 KB |
Output is correct |
5 |
Correct |
345 ms |
757408 KB |
Output is correct |
6 |
Correct |
320 ms |
757336 KB |
Output is correct |
7 |
Correct |
323 ms |
757444 KB |
Output is correct |
8 |
Correct |
312 ms |
757352 KB |
Output is correct |
9 |
Correct |
331 ms |
757380 KB |
Output is correct |
10 |
Correct |
310 ms |
757304 KB |
Output is correct |
11 |
Correct |
308 ms |
757336 KB |
Output is correct |
12 |
Correct |
315 ms |
757452 KB |
Output is correct |
13 |
Correct |
307 ms |
757316 KB |
Output is correct |
14 |
Correct |
310 ms |
757344 KB |
Output is correct |
15 |
Correct |
310 ms |
757416 KB |
Output is correct |
16 |
Correct |
308 ms |
757356 KB |
Output is correct |
17 |
Correct |
308 ms |
757412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
318 ms |
757312 KB |
Output is correct |
2 |
Correct |
336 ms |
757316 KB |
Output is correct |
3 |
Correct |
328 ms |
757324 KB |
Output is correct |
4 |
Correct |
328 ms |
757392 KB |
Output is correct |
5 |
Correct |
345 ms |
757408 KB |
Output is correct |
6 |
Correct |
320 ms |
757336 KB |
Output is correct |
7 |
Correct |
323 ms |
757444 KB |
Output is correct |
8 |
Correct |
312 ms |
757352 KB |
Output is correct |
9 |
Correct |
331 ms |
757380 KB |
Output is correct |
10 |
Correct |
310 ms |
757304 KB |
Output is correct |
11 |
Correct |
308 ms |
757336 KB |
Output is correct |
12 |
Correct |
315 ms |
757452 KB |
Output is correct |
13 |
Correct |
307 ms |
757316 KB |
Output is correct |
14 |
Correct |
310 ms |
757344 KB |
Output is correct |
15 |
Correct |
310 ms |
757416 KB |
Output is correct |
16 |
Correct |
308 ms |
757356 KB |
Output is correct |
17 |
Correct |
308 ms |
757412 KB |
Output is correct |
18 |
Correct |
332 ms |
757392 KB |
Output is correct |
19 |
Correct |
307 ms |
757484 KB |
Output is correct |
20 |
Correct |
315 ms |
757368 KB |
Output is correct |
21 |
Correct |
334 ms |
757304 KB |
Output is correct |
22 |
Correct |
314 ms |
757572 KB |
Output is correct |
23 |
Correct |
339 ms |
757368 KB |
Output is correct |
24 |
Correct |
311 ms |
757340 KB |
Output is correct |
25 |
Correct |
334 ms |
757304 KB |
Output is correct |
26 |
Correct |
317 ms |
757408 KB |
Output is correct |
27 |
Correct |
313 ms |
757380 KB |
Output is correct |
28 |
Correct |
324 ms |
757344 KB |
Output is correct |
29 |
Correct |
326 ms |
757352 KB |
Output is correct |
30 |
Correct |
331 ms |
757344 KB |
Output is correct |
31 |
Correct |
322 ms |
757316 KB |
Output is correct |
32 |
Correct |
327 ms |
757348 KB |
Output is correct |
33 |
Correct |
329 ms |
757316 KB |
Output is correct |
34 |
Correct |
312 ms |
757392 KB |
Output is correct |
35 |
Correct |
341 ms |
757368 KB |
Output is correct |
36 |
Correct |
308 ms |
757316 KB |
Output is correct |
37 |
Correct |
339 ms |
757316 KB |
Output is correct |
38 |
Correct |
338 ms |
757428 KB |
Output is correct |
39 |
Correct |
311 ms |
757444 KB |
Output is correct |
40 |
Correct |
324 ms |
757352 KB |
Output is correct |
41 |
Correct |
335 ms |
757324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
315 ms |
757396 KB |
Output is correct |
2 |
Correct |
318 ms |
757484 KB |
Output is correct |
3 |
Correct |
319 ms |
757336 KB |
Output is correct |
4 |
Correct |
347 ms |
757308 KB |
Output is correct |
5 |
Correct |
319 ms |
757472 KB |
Output is correct |
6 |
Correct |
320 ms |
757316 KB |
Output is correct |
7 |
Correct |
330 ms |
757316 KB |
Output is correct |
8 |
Correct |
321 ms |
757304 KB |
Output is correct |
9 |
Correct |
323 ms |
757532 KB |
Output is correct |
10 |
Correct |
318 ms |
757392 KB |
Output is correct |
11 |
Correct |
322 ms |
757316 KB |
Output is correct |
12 |
Correct |
347 ms |
757328 KB |
Output is correct |
13 |
Correct |
322 ms |
757396 KB |
Output is correct |
14 |
Correct |
332 ms |
757316 KB |
Output is correct |
15 |
Correct |
331 ms |
757364 KB |
Output is correct |
16 |
Correct |
340 ms |
757408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
318 ms |
757312 KB |
Output is correct |
2 |
Correct |
336 ms |
757316 KB |
Output is correct |
3 |
Correct |
328 ms |
757324 KB |
Output is correct |
4 |
Correct |
328 ms |
757392 KB |
Output is correct |
5 |
Correct |
345 ms |
757408 KB |
Output is correct |
6 |
Correct |
320 ms |
757336 KB |
Output is correct |
7 |
Correct |
323 ms |
757444 KB |
Output is correct |
8 |
Correct |
312 ms |
757352 KB |
Output is correct |
9 |
Correct |
331 ms |
757380 KB |
Output is correct |
10 |
Correct |
310 ms |
757304 KB |
Output is correct |
11 |
Correct |
308 ms |
757336 KB |
Output is correct |
12 |
Correct |
315 ms |
757452 KB |
Output is correct |
13 |
Correct |
307 ms |
757316 KB |
Output is correct |
14 |
Correct |
310 ms |
757344 KB |
Output is correct |
15 |
Correct |
310 ms |
757416 KB |
Output is correct |
16 |
Correct |
308 ms |
757356 KB |
Output is correct |
17 |
Correct |
308 ms |
757412 KB |
Output is correct |
18 |
Correct |
332 ms |
757392 KB |
Output is correct |
19 |
Correct |
307 ms |
757484 KB |
Output is correct |
20 |
Correct |
315 ms |
757368 KB |
Output is correct |
21 |
Correct |
334 ms |
757304 KB |
Output is correct |
22 |
Correct |
314 ms |
757572 KB |
Output is correct |
23 |
Correct |
339 ms |
757368 KB |
Output is correct |
24 |
Correct |
311 ms |
757340 KB |
Output is correct |
25 |
Correct |
334 ms |
757304 KB |
Output is correct |
26 |
Correct |
317 ms |
757408 KB |
Output is correct |
27 |
Correct |
313 ms |
757380 KB |
Output is correct |
28 |
Correct |
324 ms |
757344 KB |
Output is correct |
29 |
Correct |
326 ms |
757352 KB |
Output is correct |
30 |
Correct |
331 ms |
757344 KB |
Output is correct |
31 |
Correct |
322 ms |
757316 KB |
Output is correct |
32 |
Correct |
327 ms |
757348 KB |
Output is correct |
33 |
Correct |
329 ms |
757316 KB |
Output is correct |
34 |
Correct |
312 ms |
757392 KB |
Output is correct |
35 |
Correct |
341 ms |
757368 KB |
Output is correct |
36 |
Correct |
308 ms |
757316 KB |
Output is correct |
37 |
Correct |
339 ms |
757316 KB |
Output is correct |
38 |
Correct |
338 ms |
757428 KB |
Output is correct |
39 |
Correct |
311 ms |
757444 KB |
Output is correct |
40 |
Correct |
324 ms |
757352 KB |
Output is correct |
41 |
Correct |
335 ms |
757324 KB |
Output is correct |
42 |
Correct |
315 ms |
757396 KB |
Output is correct |
43 |
Correct |
318 ms |
757484 KB |
Output is correct |
44 |
Correct |
319 ms |
757336 KB |
Output is correct |
45 |
Correct |
347 ms |
757308 KB |
Output is correct |
46 |
Correct |
319 ms |
757472 KB |
Output is correct |
47 |
Correct |
320 ms |
757316 KB |
Output is correct |
48 |
Correct |
330 ms |
757316 KB |
Output is correct |
49 |
Correct |
321 ms |
757304 KB |
Output is correct |
50 |
Correct |
323 ms |
757532 KB |
Output is correct |
51 |
Correct |
318 ms |
757392 KB |
Output is correct |
52 |
Correct |
322 ms |
757316 KB |
Output is correct |
53 |
Correct |
347 ms |
757328 KB |
Output is correct |
54 |
Correct |
322 ms |
757396 KB |
Output is correct |
55 |
Correct |
332 ms |
757316 KB |
Output is correct |
56 |
Correct |
331 ms |
757364 KB |
Output is correct |
57 |
Correct |
340 ms |
757408 KB |
Output is correct |
58 |
Execution timed out |
515 ms |
757412 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |