// Be nam KHODA
#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
typedef pair<int, int> pii;
const int N = 400 + 10, inf = 1e9 + 10;
int n;
int a[N];
int cnt[N][3];
int pd[N][N][3];
int main(){
cin.tie(0), ios::sync_with_stdio(0);
cin >> n;
for(int i = 0; i < n; i++){
char c;
cin >> c;
if(c == 'R')
a[i] = 0;
else if(c == 'G')
a[i] = 1;
else a[i] = 2;
}
for(int j = 0; j < 3; j++){
int cn = 0;
for(int i = 0; i < n; i++){
if(a[i] == j){
cn++;
cnt[cn][j] = i;
}
}
}
for(int i = 0; i < n; i++)
for(int j = i; j < n; j++)
for(int z = i; z <= j; z++)
for(int d = 0; d < 3; d++)
if(a[z] == d)
pd[i][j][d]++;
int z = count(a, a + n, 0);
int t = count(a, a + n, 1);
int tw = count(a, a + n, 2);
vector<vector<vector<vector<int>>>> dp(z + 1, vector<vector<vector<int>>>(t + 1, vector<vector<int>>(tw + 1, vector<int>(4, inf))));
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for(int i = 0; i <= z; i++)
for(int j = 0; j <= t; j++)
for(int z = 0; z <= tw; z++)
for(int q = 0; q < 3; q++){
int ar[3] = {i, j, z};
if(ar[q] == 0 or i + j + z == 0){
continue;
}
int mx = max({cnt[i][0], cnt[j][1], cnt[z][2]});
int qq = a[mx];
int op = 1 ^ 2 ^ q ^ qq;
int cost = mx - cnt[ar[q]][q], want = max(0, ar[op] - pd[0][cnt[ar[q]][q]][op]);
cost -= pd[cnt[ar[q]][q]][mx][op] - want + pd[cnt[ar[q]][q] + 1][mx][q];
ar[q]--;
if(q == qq) cost = 0;
for(int qq = 0; qq < 3; qq++) if(qq != q){
dp[i][j][z][q] = min(dp[i][j][z][q], dp[ar[0]][ar[1]][ar[2]][qq] + cost);
}
}
int ans = inf;
for(int i = 0; i < 3; i++)
ans = min(ans, dp[z][t][tw][i]);
if(ans >= inf) ans = -1;
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
2 ms |
1108 KB |
Output is correct |
20 |
Correct |
2 ms |
980 KB |
Output is correct |
21 |
Correct |
2 ms |
1092 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
1096 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
980 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
1 ms |
1108 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
31 |
Correct |
1 ms |
980 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
724 KB |
Output is correct |
35 |
Correct |
1 ms |
980 KB |
Output is correct |
36 |
Correct |
2 ms |
980 KB |
Output is correct |
37 |
Correct |
1 ms |
980 KB |
Output is correct |
38 |
Correct |
1 ms |
980 KB |
Output is correct |
39 |
Correct |
2 ms |
1108 KB |
Output is correct |
40 |
Correct |
1 ms |
584 KB |
Output is correct |
41 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
27 ms |
5656 KB |
Output is correct |
3 |
Correct |
28 ms |
5580 KB |
Output is correct |
4 |
Correct |
35 ms |
5708 KB |
Output is correct |
5 |
Correct |
34 ms |
5592 KB |
Output is correct |
6 |
Correct |
33 ms |
5580 KB |
Output is correct |
7 |
Correct |
33 ms |
5636 KB |
Output is correct |
8 |
Correct |
38 ms |
5604 KB |
Output is correct |
9 |
Correct |
36 ms |
5584 KB |
Output is correct |
10 |
Correct |
33 ms |
5584 KB |
Output is correct |
11 |
Correct |
34 ms |
5636 KB |
Output is correct |
12 |
Correct |
6 ms |
2144 KB |
Output is correct |
13 |
Correct |
12 ms |
3284 KB |
Output is correct |
14 |
Correct |
20 ms |
4228 KB |
Output is correct |
15 |
Correct |
35 ms |
5620 KB |
Output is correct |
16 |
Correct |
33 ms |
5580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
2 ms |
1108 KB |
Output is correct |
20 |
Correct |
2 ms |
980 KB |
Output is correct |
21 |
Correct |
2 ms |
1092 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
1096 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
980 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
1 ms |
1108 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
31 |
Correct |
1 ms |
980 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
724 KB |
Output is correct |
35 |
Correct |
1 ms |
980 KB |
Output is correct |
36 |
Correct |
2 ms |
980 KB |
Output is correct |
37 |
Correct |
1 ms |
980 KB |
Output is correct |
38 |
Correct |
1 ms |
980 KB |
Output is correct |
39 |
Correct |
2 ms |
1108 KB |
Output is correct |
40 |
Correct |
1 ms |
584 KB |
Output is correct |
41 |
Correct |
1 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
27 ms |
5656 KB |
Output is correct |
44 |
Correct |
28 ms |
5580 KB |
Output is correct |
45 |
Correct |
35 ms |
5708 KB |
Output is correct |
46 |
Correct |
34 ms |
5592 KB |
Output is correct |
47 |
Correct |
33 ms |
5580 KB |
Output is correct |
48 |
Correct |
33 ms |
5636 KB |
Output is correct |
49 |
Correct |
38 ms |
5604 KB |
Output is correct |
50 |
Correct |
36 ms |
5584 KB |
Output is correct |
51 |
Correct |
33 ms |
5584 KB |
Output is correct |
52 |
Correct |
34 ms |
5636 KB |
Output is correct |
53 |
Correct |
6 ms |
2144 KB |
Output is correct |
54 |
Correct |
12 ms |
3284 KB |
Output is correct |
55 |
Correct |
20 ms |
4228 KB |
Output is correct |
56 |
Correct |
35 ms |
5620 KB |
Output is correct |
57 |
Correct |
33 ms |
5580 KB |
Output is correct |
58 |
Correct |
252 ms |
134988 KB |
Output is correct |
59 |
Correct |
242 ms |
135160 KB |
Output is correct |
60 |
Correct |
241 ms |
134176 KB |
Output is correct |
61 |
Correct |
240 ms |
135628 KB |
Output is correct |
62 |
Correct |
43 ms |
14156 KB |
Output is correct |
63 |
Correct |
64 ms |
22648 KB |
Output is correct |
64 |
Correct |
139 ms |
68484 KB |
Output is correct |
65 |
Correct |
183 ms |
97508 KB |
Output is correct |
66 |
Correct |
230 ms |
133660 KB |
Output is correct |
67 |
Correct |
241 ms |
133564 KB |
Output is correct |
68 |
Correct |
230 ms |
135640 KB |
Output is correct |
69 |
Correct |
232 ms |
136572 KB |
Output is correct |
70 |
Correct |
232 ms |
136016 KB |
Output is correct |
71 |
Correct |
235 ms |
132908 KB |
Output is correct |
72 |
Correct |
85 ms |
35916 KB |
Output is correct |
73 |
Correct |
45 ms |
12356 KB |
Output is correct |