#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned;
using i16 = short;
using u16 = unsigned short;
using ld = long double;
using ii = pair<int, int>;
const int N = 401, INF = 1e9;
int dp[N][N][N][3];
void solve() {
int n, r, g, y;
string s, p = "RGY";
vector<int> pos[3];
cin >> n >> s;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < 3; ++j) {
if(s[i] == p[j])
pos[j].push_back(i);
}
}
r = pos[0].size();
g = pos[1].size();
y = pos[2].size();
for(int i = 0; i <= r; ++i)
for(int j = 0; j <= g; ++j)
for(int k = 0; k <= y; ++k)
for(int l = 0; l < 3; ++l)
dp[i][j][k][l] = INF;
for(int l = 0; l < 3; ++l) dp[0][0][0][l] = 0;
for(int i = 0; i <= r; ++i) {
for(int j = 0; j <= g; ++j) {
for(int k = 0; k <= y; ++k) {
int hi[3][2] = {{0}};
if(i > 0) {
int p = pos[0][i - 1];
int& a = hi[0][0];
int& b = hi[0][1];
while(a < g && pos[1][a] < p) ++a;
while(b < y && pos[2][b] < p) ++b;
int moves = max(0, a - j) + max(0, b - k);
for(int l : {1, 2})
dp[i][j][k][0] = min(dp[i][j][k][0], dp[i - 1][j][k][l] + moves);
}
if(j > 0) {
int p = pos[1][j - 1];
int& a = hi[1][0];
int& b = hi[1][1];
while(a < r && pos[0][a] < p) ++a;
while(b < y && pos[2][b] < p) ++b;
int moves = max(0, a - i) + max(0, b - k);
for(int l : {0, 2})
dp[i][j][k][1] = min(dp[i][j][k][1], dp[i][j - 1][k][l] + moves);
}
if(k > 0) {
int p = pos[2][k - 1];
int& a = hi[2][0];
int& b = hi[2][1];
while(a < r && pos[0][a] < p) ++a;
while(b < g && pos[1][b] < p) ++b;
int moves = max(0, a - i) + max(0, b - j);
for(int l : {0, 1})
dp[i][j][k][2] = min(dp[i][j][k][2], dp[i][j][k - 1][l] + moves);
}
}
}
}
int ans = min({dp[r][g][y][0], dp[r][g][y][1], dp[r][g][y][2]});
if(ans == INF) ans = -1;
cout << ans << '\n';
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
448 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
448 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 |
312 KB |
Output is correct |
16 |
Correct |
0 ms |
448 KB |
Output is correct |
17 |
Correct |
0 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
448 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
448 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 |
312 KB |
Output is correct |
16 |
Correct |
0 ms |
448 KB |
Output is correct |
17 |
Correct |
0 ms |
324 KB |
Output is correct |
18 |
Correct |
2 ms |
2244 KB |
Output is correct |
19 |
Correct |
2 ms |
1868 KB |
Output is correct |
20 |
Correct |
2 ms |
2252 KB |
Output is correct |
21 |
Correct |
2 ms |
2344 KB |
Output is correct |
22 |
Correct |
2 ms |
1580 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1604 KB |
Output is correct |
25 |
Correct |
2 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
4044 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
2 ms |
2124 KB |
Output is correct |
30 |
Correct |
2 ms |
2192 KB |
Output is correct |
31 |
Correct |
1 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
2 ms |
3788 KB |
Output is correct |
34 |
Correct |
2 ms |
3532 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
1848 KB |
Output is correct |
37 |
Correct |
1 ms |
1704 KB |
Output is correct |
38 |
Correct |
2 ms |
2112 KB |
Output is correct |
39 |
Correct |
2 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
304 KB |
Output is correct |
41 |
Correct |
2 ms |
2124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
65 ms |
162856 KB |
Output is correct |
3 |
Correct |
63 ms |
162120 KB |
Output is correct |
4 |
Correct |
61 ms |
162884 KB |
Output is correct |
5 |
Correct |
62 ms |
162908 KB |
Output is correct |
6 |
Correct |
68 ms |
162912 KB |
Output is correct |
7 |
Correct |
82 ms |
162048 KB |
Output is correct |
8 |
Correct |
64 ms |
162104 KB |
Output is correct |
9 |
Correct |
62 ms |
161324 KB |
Output is correct |
10 |
Correct |
65 ms |
162836 KB |
Output is correct |
11 |
Correct |
62 ms |
162884 KB |
Output is correct |
12 |
Correct |
18 ms |
43980 KB |
Output is correct |
13 |
Correct |
31 ms |
77128 KB |
Output is correct |
14 |
Correct |
45 ms |
111292 KB |
Output is correct |
15 |
Correct |
73 ms |
162856 KB |
Output is correct |
16 |
Correct |
62 ms |
162832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
448 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
448 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 |
312 KB |
Output is correct |
16 |
Correct |
0 ms |
448 KB |
Output is correct |
17 |
Correct |
0 ms |
324 KB |
Output is correct |
18 |
Correct |
2 ms |
2244 KB |
Output is correct |
19 |
Correct |
2 ms |
1868 KB |
Output is correct |
20 |
Correct |
2 ms |
2252 KB |
Output is correct |
21 |
Correct |
2 ms |
2344 KB |
Output is correct |
22 |
Correct |
2 ms |
1580 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1604 KB |
Output is correct |
25 |
Correct |
2 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
4044 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
2 ms |
2124 KB |
Output is correct |
30 |
Correct |
2 ms |
2192 KB |
Output is correct |
31 |
Correct |
1 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
2 ms |
3788 KB |
Output is correct |
34 |
Correct |
2 ms |
3532 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
1848 KB |
Output is correct |
37 |
Correct |
1 ms |
1704 KB |
Output is correct |
38 |
Correct |
2 ms |
2112 KB |
Output is correct |
39 |
Correct |
2 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
304 KB |
Output is correct |
41 |
Correct |
2 ms |
2124 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
65 ms |
162856 KB |
Output is correct |
44 |
Correct |
63 ms |
162120 KB |
Output is correct |
45 |
Correct |
61 ms |
162884 KB |
Output is correct |
46 |
Correct |
62 ms |
162908 KB |
Output is correct |
47 |
Correct |
68 ms |
162912 KB |
Output is correct |
48 |
Correct |
82 ms |
162048 KB |
Output is correct |
49 |
Correct |
64 ms |
162104 KB |
Output is correct |
50 |
Correct |
62 ms |
161324 KB |
Output is correct |
51 |
Correct |
65 ms |
162836 KB |
Output is correct |
52 |
Correct |
62 ms |
162884 KB |
Output is correct |
53 |
Correct |
18 ms |
43980 KB |
Output is correct |
54 |
Correct |
31 ms |
77128 KB |
Output is correct |
55 |
Correct |
45 ms |
111292 KB |
Output is correct |
56 |
Correct |
73 ms |
162856 KB |
Output is correct |
57 |
Correct |
62 ms |
162832 KB |
Output is correct |
58 |
Execution timed out |
753 ms |
75476 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |