#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 1e9;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
string s;
cin >> n >> s;
vector<int> a[3], b[3][3];
int X, Y, Z;
X = Y = Z = 1;
for (int l = 0; l < 3; l++) a[l].push_back(-1);
for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (i != j) b[i][j].push_back(-1);
for (int i = 0; i < n; i++) {
if (s[i] == 'R') {
a[0].push_back(i);
X += 1;
b[0][1].push_back(Y);
b[0][2].push_back(Z);
}
if (s[i] == 'G') {
a[1].push_back(i);
Y += 1;
b[1][0].push_back(X);
b[1][2].push_back(Z);
}
if (s[i] == 'Y') {
a[2].push_back(i);
Z += 1;
b[2][0].push_back(X);
b[2][1].push_back(Y);
}
}
vector<vector<vector<vector<int>>>> dp(X);
for (int i = 0; i < X; i++) {
dp[i].resize(Y);
for (int j = 0; j < Y; j++) {
dp[i][j].resize(Z, vector<int>(3, inf));
}
}
for (int l = 0; l < 3; l++) dp[0][0][0][l] = 0;
for (int i = 0; i < X; i++) {
for (int j = 0; j < Y; j++) {
for (int k = 0; k < Z; k++) {
int pos = i + j + k - 1;
if (i > 0) dp[i][j][k][0] = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + a[0][i] - pos + max(0, j - b[0][1][i] + 1) + max(0, k - b[0][2][i] + 1);
if (j > 0) dp[i][j][k][1] = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + a[1][j] - pos + max(0, i - b[1][0][j] + 1) + max(0, k - b[1][2][j] + 1);
if (k > 0) dp[i][j][k][2] = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + a[2][k] - pos + max(0, i - b[2][0][k] + 1) + max(0, j - b[2][1][k] + 1);
}
}
}
int ans = inf;
for (int l = 0; l < 3; l++) smin(ans, dp[X - 1][Y - 1][Z - 1][l]);
if (ans == inf) ans = -1;
cout << ans << en;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
316 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
316 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
832 KB |
Output is correct |
22 |
Correct |
1 ms |
708 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
700 KB |
Output is correct |
25 |
Correct |
1 ms |
448 KB |
Output is correct |
26 |
Correct |
1 ms |
444 KB |
Output is correct |
27 |
Correct |
1 ms |
700 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
836 KB |
Output is correct |
33 |
Correct |
1 ms |
448 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
708 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
38 |
Correct |
1 ms |
724 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
40 |
Correct |
1 ms |
412 KB |
Output is correct |
41 |
Correct |
1 ms |
440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
3796 KB |
Output is correct |
3 |
Correct |
6 ms |
3668 KB |
Output is correct |
4 |
Correct |
6 ms |
3796 KB |
Output is correct |
5 |
Correct |
6 ms |
3796 KB |
Output is correct |
6 |
Correct |
6 ms |
3796 KB |
Output is correct |
7 |
Correct |
6 ms |
3796 KB |
Output is correct |
8 |
Correct |
6 ms |
3716 KB |
Output is correct |
9 |
Correct |
6 ms |
3668 KB |
Output is correct |
10 |
Correct |
6 ms |
3796 KB |
Output is correct |
11 |
Correct |
6 ms |
3796 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
13 |
Correct |
3 ms |
1876 KB |
Output is correct |
14 |
Correct |
4 ms |
2708 KB |
Output is correct |
15 |
Correct |
7 ms |
3796 KB |
Output is correct |
16 |
Correct |
6 ms |
3796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
316 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
832 KB |
Output is correct |
22 |
Correct |
1 ms |
708 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
700 KB |
Output is correct |
25 |
Correct |
1 ms |
448 KB |
Output is correct |
26 |
Correct |
1 ms |
444 KB |
Output is correct |
27 |
Correct |
1 ms |
700 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
836 KB |
Output is correct |
33 |
Correct |
1 ms |
448 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
708 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
38 |
Correct |
1 ms |
724 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
40 |
Correct |
1 ms |
412 KB |
Output is correct |
41 |
Correct |
1 ms |
440 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
6 ms |
3796 KB |
Output is correct |
44 |
Correct |
6 ms |
3668 KB |
Output is correct |
45 |
Correct |
6 ms |
3796 KB |
Output is correct |
46 |
Correct |
6 ms |
3796 KB |
Output is correct |
47 |
Correct |
6 ms |
3796 KB |
Output is correct |
48 |
Correct |
6 ms |
3796 KB |
Output is correct |
49 |
Correct |
6 ms |
3716 KB |
Output is correct |
50 |
Correct |
6 ms |
3668 KB |
Output is correct |
51 |
Correct |
6 ms |
3796 KB |
Output is correct |
52 |
Correct |
6 ms |
3796 KB |
Output is correct |
53 |
Correct |
2 ms |
1236 KB |
Output is correct |
54 |
Correct |
3 ms |
1876 KB |
Output is correct |
55 |
Correct |
4 ms |
2708 KB |
Output is correct |
56 |
Correct |
7 ms |
3796 KB |
Output is correct |
57 |
Correct |
6 ms |
3796 KB |
Output is correct |
58 |
Correct |
168 ms |
132052 KB |
Output is correct |
59 |
Correct |
167 ms |
132376 KB |
Output is correct |
60 |
Correct |
164 ms |
131404 KB |
Output is correct |
61 |
Correct |
169 ms |
132740 KB |
Output is correct |
62 |
Correct |
18 ms |
12320 KB |
Output is correct |
63 |
Correct |
28 ms |
20564 KB |
Output is correct |
64 |
Correct |
91 ms |
66252 KB |
Output is correct |
65 |
Correct |
123 ms |
95036 KB |
Output is correct |
66 |
Correct |
163 ms |
130828 KB |
Output is correct |
67 |
Correct |
166 ms |
130532 KB |
Output is correct |
68 |
Correct |
168 ms |
132684 KB |
Output is correct |
69 |
Correct |
170 ms |
133656 KB |
Output is correct |
70 |
Correct |
166 ms |
133196 KB |
Output is correct |
71 |
Correct |
165 ms |
130164 KB |
Output is correct |
72 |
Correct |
47 ms |
33976 KB |
Output is correct |
73 |
Correct |
11 ms |
8788 KB |
Output is correct |