#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 407;
constexpr int oo = 1e9;
int sumpref[MAXN][3];
int POS[MAXN][MAXN][MAXN][3];
int dp[2][MAXN][MAXN][3];
vector<int> idx[3];
int R,G,Y;
int n;
int check(int i, int j, int k, int v){
int ith, sum = 0;
if(v == 0)
ith = idx[v][i-1];
if(v == 1)
ith = idx[v][j-1];
if(v == 2)
ith = idx[v][k-1];
sum += max(sumpref[ith][0], i);
sum += max(sumpref[ith][1], j);
sum += max(sumpref[ith][2], k);
return sum;
}
void computePos(){
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
for(int k = 1; k <= n; k++){
if(k <= R && i <= G && j <= Y)
POS[k][i][j][0] = check(k, i, j, 0);
if(i <= R && k <= G && j <= Y)
POS[i][k][j][1] = check(i, k, j, 1);
if(i <= R && j <= G && k <= Y)
POS[i][j][k][2] = check(i, j, k, 2);
}
}
}
}
void solve(){
for(int i = 0; i < 2; i++)
for(int j = 0; j <= n; j++)
for(int k = 0; k <= n; k++)
dp[i][j][k][0] = dp[i][j][k][1] = dp[i][j][k][2] = oo;
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
//dp[i][j][k][v]
// i -> pozycja
// j -> ile R
// k -> ile G
for(int i = 1; i <= n; i++){
for(int j = 0; j <= min(R, i); j++){
for(int k = 0; k <= min(G, i); k++){
//dla 0 -> {1, 2}
//dla 1 -> {0, 2}
//dla 2 -> {1, 2}
//na pozycji itej stawiamy R
if(j && j+k <= i)
dp[i%2][j][k][0] = min(dp[(i-1)%2][j-1][k][1], dp[(i-1)%2][j-1][k][2]) + abs(i - POS[j][k][i - (j+k)][0]);
//na pozycji itej stawiamy G
if(k && j+k <= i)
dp[i%2][j][k][1] = min(dp[(i-1)%2][j][k-1][0], dp[(i-1)%2][j][k-1][2]) + abs(i - POS[j][k][i - (j+k)][1]);
//na pozycji itej stawiamy Y
if(i >= j+k && (i - (j+k) <= Y))
dp[i%2][j][k][2] = min(dp[(i-1)%2][j][k][0], dp[(i-1)%2][j][k][1]) + abs(i - POS[j][k][i - (j+k)][2]);
}
}
for(int j = 0; j <= min(R, i+1); j++)
for(int k = 0; k <= min(G, i+1); k++)
dp[(i+1)%2][j][k][0] = dp[(i+1)%2][j][k][1] = dp[(i+1)%2][j][k][2] = oo;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
char c;
cin >> c;
if(c == 'R'){
idx[0].push_back(i);
R++;
}
else if(c == 'G'){
idx[1].push_back(i);
G++;
}
else if(c == 'Y'){
idx[2].push_back(i);
Y++;
}
sumpref[i][0] = R;
sumpref[i][1] = G;
sumpref[i][2] = Y;
}
computePos();
solve();
int res = min({dp[n%2][R][G][0], dp[n%2][R][G][1], dp[n%2][R][G][2]});
if(res >= oo) cout << "-1 \n";
else cout << res << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
4572 KB |
Output is correct |
4 |
Correct |
1 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
18776 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
5 ms |
24924 KB |
Output is correct |
11 |
Correct |
2 ms |
14804 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
26972 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
4572 KB |
Output is correct |
4 |
Correct |
1 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
18776 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
5 ms |
24924 KB |
Output is correct |
11 |
Correct |
2 ms |
14804 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
26972 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
6 ms |
49756 KB |
Output is correct |
19 |
Correct |
5 ms |
41816 KB |
Output is correct |
20 |
Correct |
8 ms |
53796 KB |
Output is correct |
21 |
Correct |
6 ms |
43608 KB |
Output is correct |
22 |
Correct |
6 ms |
51804 KB |
Output is correct |
23 |
Correct |
5 ms |
37468 KB |
Output is correct |
24 |
Correct |
3 ms |
25080 KB |
Output is correct |
25 |
Correct |
10 ms |
76548 KB |
Output is correct |
26 |
Correct |
7 ms |
66140 KB |
Output is correct |
27 |
Correct |
8 ms |
57948 KB |
Output is correct |
28 |
Correct |
5 ms |
45656 KB |
Output is correct |
29 |
Correct |
6 ms |
47708 KB |
Output is correct |
30 |
Correct |
5 ms |
37468 KB |
Output is correct |
31 |
Correct |
4 ms |
35420 KB |
Output is correct |
32 |
Correct |
5 ms |
39516 KB |
Output is correct |
33 |
Correct |
6 ms |
53852 KB |
Output is correct |
34 |
Correct |
7 ms |
47704 KB |
Output is correct |
35 |
Correct |
6 ms |
49756 KB |
Output is correct |
36 |
Correct |
6 ms |
43788 KB |
Output is correct |
37 |
Correct |
4 ms |
31324 KB |
Output is correct |
38 |
Correct |
4 ms |
33372 KB |
Output is correct |
39 |
Correct |
5 ms |
41564 KB |
Output is correct |
40 |
Correct |
1 ms |
4700 KB |
Output is correct |
41 |
Correct |
3 ms |
23132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8736 KB |
Output is correct |
2 |
Correct |
284 ms |
394420 KB |
Output is correct |
3 |
Correct |
238 ms |
394684 KB |
Output is correct |
4 |
Correct |
235 ms |
394604 KB |
Output is correct |
5 |
Correct |
236 ms |
394580 KB |
Output is correct |
6 |
Correct |
237 ms |
394612 KB |
Output is correct |
7 |
Correct |
286 ms |
392552 KB |
Output is correct |
8 |
Correct |
264 ms |
392552 KB |
Output is correct |
9 |
Correct |
251 ms |
392532 KB |
Output is correct |
10 |
Correct |
238 ms |
394584 KB |
Output is correct |
11 |
Correct |
269 ms |
394544 KB |
Output is correct |
12 |
Correct |
50 ms |
205648 KB |
Output is correct |
13 |
Correct |
94 ms |
271444 KB |
Output is correct |
14 |
Correct |
143 ms |
326736 KB |
Output is correct |
15 |
Correct |
255 ms |
392528 KB |
Output is correct |
16 |
Correct |
239 ms |
392436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
4572 KB |
Output is correct |
4 |
Correct |
1 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
18776 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
5 ms |
24924 KB |
Output is correct |
11 |
Correct |
2 ms |
14804 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
26972 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
6 ms |
49756 KB |
Output is correct |
19 |
Correct |
5 ms |
41816 KB |
Output is correct |
20 |
Correct |
8 ms |
53796 KB |
Output is correct |
21 |
Correct |
6 ms |
43608 KB |
Output is correct |
22 |
Correct |
6 ms |
51804 KB |
Output is correct |
23 |
Correct |
5 ms |
37468 KB |
Output is correct |
24 |
Correct |
3 ms |
25080 KB |
Output is correct |
25 |
Correct |
10 ms |
76548 KB |
Output is correct |
26 |
Correct |
7 ms |
66140 KB |
Output is correct |
27 |
Correct |
8 ms |
57948 KB |
Output is correct |
28 |
Correct |
5 ms |
45656 KB |
Output is correct |
29 |
Correct |
6 ms |
47708 KB |
Output is correct |
30 |
Correct |
5 ms |
37468 KB |
Output is correct |
31 |
Correct |
4 ms |
35420 KB |
Output is correct |
32 |
Correct |
5 ms |
39516 KB |
Output is correct |
33 |
Correct |
6 ms |
53852 KB |
Output is correct |
34 |
Correct |
7 ms |
47704 KB |
Output is correct |
35 |
Correct |
6 ms |
49756 KB |
Output is correct |
36 |
Correct |
6 ms |
43788 KB |
Output is correct |
37 |
Correct |
4 ms |
31324 KB |
Output is correct |
38 |
Correct |
4 ms |
33372 KB |
Output is correct |
39 |
Correct |
5 ms |
41564 KB |
Output is correct |
40 |
Correct |
1 ms |
4700 KB |
Output is correct |
41 |
Correct |
3 ms |
23132 KB |
Output is correct |
42 |
Correct |
1 ms |
8736 KB |
Output is correct |
43 |
Correct |
284 ms |
394420 KB |
Output is correct |
44 |
Correct |
238 ms |
394684 KB |
Output is correct |
45 |
Correct |
235 ms |
394604 KB |
Output is correct |
46 |
Correct |
236 ms |
394580 KB |
Output is correct |
47 |
Correct |
237 ms |
394612 KB |
Output is correct |
48 |
Correct |
286 ms |
392552 KB |
Output is correct |
49 |
Correct |
264 ms |
392552 KB |
Output is correct |
50 |
Correct |
251 ms |
392532 KB |
Output is correct |
51 |
Correct |
238 ms |
394584 KB |
Output is correct |
52 |
Correct |
269 ms |
394544 KB |
Output is correct |
53 |
Correct |
50 ms |
205648 KB |
Output is correct |
54 |
Correct |
94 ms |
271444 KB |
Output is correct |
55 |
Correct |
143 ms |
326736 KB |
Output is correct |
56 |
Correct |
255 ms |
392528 KB |
Output is correct |
57 |
Correct |
239 ms |
392436 KB |
Output is correct |
58 |
Correct |
184 ms |
244812 KB |
Output is correct |
59 |
Correct |
214 ms |
285776 KB |
Output is correct |
60 |
Correct |
223 ms |
281560 KB |
Output is correct |
61 |
Correct |
221 ms |
265156 KB |
Output is correct |
62 |
Correct |
251 ms |
423252 KB |
Output is correct |
63 |
Correct |
237 ms |
386396 KB |
Output is correct |
64 |
Correct |
231 ms |
369744 KB |
Output is correct |
65 |
Correct |
223 ms |
314584 KB |
Output is correct |
66 |
Correct |
238 ms |
271568 KB |
Output is correct |
67 |
Correct |
203 ms |
224300 KB |
Output is correct |
68 |
Correct |
197 ms |
271488 KB |
Output is correct |
69 |
Correct |
203 ms |
271564 KB |
Output is correct |
70 |
Correct |
198 ms |
269400 KB |
Output is correct |
71 |
Correct |
197 ms |
300216 KB |
Output is correct |
72 |
Correct |
222 ms |
591564 KB |
Output is correct |
73 |
Correct |
97 ms |
12760 KB |
Output is correct |