#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SREP(i, a, b) for(ll i = a; i < b; i++)
#define NREP(i, a, b) for(ll i = a; i <= b; i++)
ll bSearch(vector<ll> &vll, ll target, ll h) {
ll l = -1;
while(h - l > 1) {
ll mid = (l + h) / 2;
if(vll[mid] > target) h = mid;
else l = mid;
}
return h;
}
int main() {
ll N, R = 0, Y = 0, G = 0; string S; cin >> N >> S;
vector<ll> Rpos, Ypos, Gpos;
SREP(i, 0, N) {
if(S[i] == 'R') {
Rpos.push_back(i); R++;
} else if(S[i] == 'Y') {
Ypos.push_back(i); Y++;
} else {
Gpos.push_back(i); G++;
}
}
ll dp[2][Y + 1][G + 1][3];
SREP(i, 0, 2) NREP(j, 0, Y) NREP(k, 0, G) SREP(l, 0, 3) dp[i][j][k][l] = 1e18;
SREP(i, 0, 3) dp[0][0][0][i] = 0;
NREP(i, 0, R) NREP(j, 0, Y) NREP(k, 0, G) {
if(i + j + k == 0) continue;
dp[i % 2][j][k][0] = 1e18;
dp[i % 2][j][k][1] = 1e18;
dp[i % 2][j][k][2] = 1e18;
if(i != 0) dp[i % 2][j][k][0] = min(dp[(i - 1) % 2][j][k][1], dp[(i - 1) % 2][j][k][2]) + j - bSearch(Ypos, Rpos[i - 1], j) + k - bSearch(Gpos, Rpos[i - 1], k);
if(j != 0) dp[i % 2][j][k][1] = min(dp[i % 2][j - 1][k][0], dp[i % 2][j - 1][k][2]) + i - bSearch(Rpos, Ypos[j - 1], i) + k - bSearch(Gpos, Ypos[j - 1], k);
if(k != 0) dp[i % 2][j][k][2] = min(dp[i % 2][j][k - 1][0], dp[i % 2][j][k - 1][1]) + i - bSearch(Rpos, Gpos[k - 1], i) + j - bSearch(Ypos, Gpos[k - 1], j);
}
ll ans = LONG_LONG_MAX;
SREP(i, 0, 3) ans = min(ans, dp[R % 2][Y][G][i]);
if(ans >= 1e12) cout << -1;
else cout << ans;
}
# |
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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
2 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
300 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
300 KB |
Output is correct |
32 |
Correct |
1 ms |
300 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
304 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
304 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
292 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
304 KB |
Output is correct |
15 |
Correct |
3 ms |
212 KB |
Output is correct |
16 |
Correct |
2 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
2 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
300 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
300 KB |
Output is correct |
32 |
Correct |
1 ms |
300 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
304 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
304 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
292 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
3 ms |
292 KB |
Output is correct |
44 |
Correct |
2 ms |
212 KB |
Output is correct |
45 |
Correct |
3 ms |
212 KB |
Output is correct |
46 |
Correct |
3 ms |
212 KB |
Output is correct |
47 |
Correct |
3 ms |
212 KB |
Output is correct |
48 |
Correct |
4 ms |
212 KB |
Output is correct |
49 |
Correct |
2 ms |
212 KB |
Output is correct |
50 |
Correct |
2 ms |
212 KB |
Output is correct |
51 |
Correct |
2 ms |
212 KB |
Output is correct |
52 |
Correct |
2 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
2 ms |
304 KB |
Output is correct |
56 |
Correct |
3 ms |
212 KB |
Output is correct |
57 |
Correct |
2 ms |
212 KB |
Output is correct |
58 |
Correct |
277 ms |
1192 KB |
Output is correct |
59 |
Correct |
239 ms |
1068 KB |
Output is correct |
60 |
Correct |
234 ms |
980 KB |
Output is correct |
61 |
Correct |
260 ms |
1136 KB |
Output is correct |
62 |
Correct |
15 ms |
344 KB |
Output is correct |
63 |
Correct |
31 ms |
296 KB |
Output is correct |
64 |
Correct |
107 ms |
596 KB |
Output is correct |
65 |
Correct |
165 ms |
792 KB |
Output is correct |
66 |
Correct |
206 ms |
980 KB |
Output is correct |
67 |
Correct |
213 ms |
1272 KB |
Output is correct |
68 |
Correct |
227 ms |
1120 KB |
Output is correct |
69 |
Correct |
240 ms |
1116 KB |
Output is correct |
70 |
Correct |
233 ms |
1116 KB |
Output is correct |
71 |
Correct |
230 ms |
1024 KB |
Output is correct |
72 |
Correct |
69 ms |
300 KB |
Output is correct |
73 |
Correct |
14 ms |
1760 KB |
Output is correct |