#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
int pref[401][3];
int dp[401][401][401][3];
vector <int> vec[3];
int main(){
int n; cin >> n;
char c[n + 1];
for(int i = 1; i <= n;i ++) cin >> c[i];
for(int i = 1; i <= n; i++){
for(int j = 0; j < 3; j++) pref[i][j] = pref[i - 1][j];
if(c[i] == 'R')
pref[i][0]++, vec[0].pb(i);
if(c[i] == 'G')
pref[i][1]++, vec[1].pb(i);
if(c[i] == 'Y')
pref[i][2]++, vec[2].pb(i);
}
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
for(int x= 0; x <= n; x++){
for(int k = 0; k < 3; k++){
dp[i][j][x][k] = 1e9;
}
}
}
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for(int i = 0; i <= (int)vec[0].size(); i++){
for(int j = 0; j <= (int)vec[1].size(); j++){
for(int k = 0; k <= (int)vec[2].size(); k++){
if(i){
int x = vec[0][i - 1];
int v = max(pref[x][1] - j, 0);
int u = max(pref[x][2] - k, 0);
dp[i][j][k][0] = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + u + v;
}
if(j){
int x = vec[1][j - 1];
int v = max(pref[x][0] - i, 0);
int u = max(pref[x][2] - k, 0);
dp[i][j][k][1] = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + u + v;
}
if(k){
int x = vec[2][k - 1];
int v = max(pref[x][0] - i, 0);
int u = max(pref[x][1] - j, 0);
dp[i][j][k][2] = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + u + v;
}
}
}
}
int l1 = vec[0].size();
int l2 = vec[1].size();
int l3 = vec[2].size();
int x = min({dp[l1][l2][l3][0], dp[l1][l2][l3][1], dp[l1][l2][l3][2]});
if(x >= 1e9){
cout << -1 << "\n";
}
else cout << x;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1336 KB |
Output is correct |
10 |
Correct |
1 ms |
1432 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1328 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1336 KB |
Output is correct |
10 |
Correct |
1 ms |
1432 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1328 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
8 ms |
17940 KB |
Output is correct |
19 |
Correct |
8 ms |
17952 KB |
Output is correct |
20 |
Correct |
7 ms |
18004 KB |
Output is correct |
21 |
Correct |
7 ms |
17972 KB |
Output is correct |
22 |
Correct |
8 ms |
18004 KB |
Output is correct |
23 |
Correct |
8 ms |
18004 KB |
Output is correct |
24 |
Correct |
8 ms |
18004 KB |
Output is correct |
25 |
Correct |
11 ms |
17968 KB |
Output is correct |
26 |
Correct |
8 ms |
18048 KB |
Output is correct |
27 |
Correct |
8 ms |
18004 KB |
Output is correct |
28 |
Correct |
10 ms |
17968 KB |
Output is correct |
29 |
Correct |
8 ms |
17972 KB |
Output is correct |
30 |
Correct |
8 ms |
18004 KB |
Output is correct |
31 |
Correct |
7 ms |
18004 KB |
Output is correct |
32 |
Correct |
8 ms |
18004 KB |
Output is correct |
33 |
Correct |
8 ms |
17364 KB |
Output is correct |
34 |
Correct |
8 ms |
17336 KB |
Output is correct |
35 |
Correct |
8 ms |
16776 KB |
Output is correct |
36 |
Correct |
8 ms |
17364 KB |
Output is correct |
37 |
Correct |
9 ms |
16212 KB |
Output is correct |
38 |
Correct |
8 ms |
18004 KB |
Output is correct |
39 |
Correct |
8 ms |
18004 KB |
Output is correct |
40 |
Correct |
8 ms |
17364 KB |
Output is correct |
41 |
Correct |
8 ms |
17972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
383 ms |
757340 KB |
Output is correct |
3 |
Correct |
332 ms |
755128 KB |
Output is correct |
4 |
Correct |
348 ms |
757392 KB |
Output is correct |
5 |
Correct |
332 ms |
757348 KB |
Output is correct |
6 |
Correct |
338 ms |
757428 KB |
Output is correct |
7 |
Correct |
337 ms |
755152 KB |
Output is correct |
8 |
Correct |
333 ms |
755232 KB |
Output is correct |
9 |
Correct |
330 ms |
751508 KB |
Output is correct |
10 |
Correct |
356 ms |
757392 KB |
Output is correct |
11 |
Correct |
338 ms |
757332 KB |
Output is correct |
12 |
Correct |
90 ms |
202700 KB |
Output is correct |
13 |
Correct |
157 ms |
357384 KB |
Output is correct |
14 |
Correct |
231 ms |
517364 KB |
Output is correct |
15 |
Correct |
336 ms |
757324 KB |
Output is correct |
16 |
Correct |
332 ms |
757564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1336 KB |
Output is correct |
10 |
Correct |
1 ms |
1432 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1328 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
8 ms |
17940 KB |
Output is correct |
19 |
Correct |
8 ms |
17952 KB |
Output is correct |
20 |
Correct |
7 ms |
18004 KB |
Output is correct |
21 |
Correct |
7 ms |
17972 KB |
Output is correct |
22 |
Correct |
8 ms |
18004 KB |
Output is correct |
23 |
Correct |
8 ms |
18004 KB |
Output is correct |
24 |
Correct |
8 ms |
18004 KB |
Output is correct |
25 |
Correct |
11 ms |
17968 KB |
Output is correct |
26 |
Correct |
8 ms |
18048 KB |
Output is correct |
27 |
Correct |
8 ms |
18004 KB |
Output is correct |
28 |
Correct |
10 ms |
17968 KB |
Output is correct |
29 |
Correct |
8 ms |
17972 KB |
Output is correct |
30 |
Correct |
8 ms |
18004 KB |
Output is correct |
31 |
Correct |
7 ms |
18004 KB |
Output is correct |
32 |
Correct |
8 ms |
18004 KB |
Output is correct |
33 |
Correct |
8 ms |
17364 KB |
Output is correct |
34 |
Correct |
8 ms |
17336 KB |
Output is correct |
35 |
Correct |
8 ms |
16776 KB |
Output is correct |
36 |
Correct |
8 ms |
17364 KB |
Output is correct |
37 |
Correct |
9 ms |
16212 KB |
Output is correct |
38 |
Correct |
8 ms |
18004 KB |
Output is correct |
39 |
Correct |
8 ms |
18004 KB |
Output is correct |
40 |
Correct |
8 ms |
17364 KB |
Output is correct |
41 |
Correct |
8 ms |
17972 KB |
Output is correct |
42 |
Correct |
1 ms |
308 KB |
Output is correct |
43 |
Correct |
383 ms |
757340 KB |
Output is correct |
44 |
Correct |
332 ms |
755128 KB |
Output is correct |
45 |
Correct |
348 ms |
757392 KB |
Output is correct |
46 |
Correct |
332 ms |
757348 KB |
Output is correct |
47 |
Correct |
338 ms |
757428 KB |
Output is correct |
48 |
Correct |
337 ms |
755152 KB |
Output is correct |
49 |
Correct |
333 ms |
755232 KB |
Output is correct |
50 |
Correct |
330 ms |
751508 KB |
Output is correct |
51 |
Correct |
356 ms |
757392 KB |
Output is correct |
52 |
Correct |
338 ms |
757332 KB |
Output is correct |
53 |
Correct |
90 ms |
202700 KB |
Output is correct |
54 |
Correct |
157 ms |
357384 KB |
Output is correct |
55 |
Correct |
231 ms |
517364 KB |
Output is correct |
56 |
Correct |
336 ms |
757324 KB |
Output is correct |
57 |
Correct |
332 ms |
757564 KB |
Output is correct |
58 |
Correct |
361 ms |
757324 KB |
Output is correct |
59 |
Correct |
365 ms |
757304 KB |
Output is correct |
60 |
Correct |
348 ms |
751424 KB |
Output is correct |
61 |
Correct |
453 ms |
755276 KB |
Output is correct |
62 |
Correct |
342 ms |
757364 KB |
Output is correct |
63 |
Correct |
346 ms |
757384 KB |
Output is correct |
64 |
Correct |
361 ms |
757376 KB |
Output is correct |
65 |
Correct |
337 ms |
755180 KB |
Output is correct |
66 |
Correct |
343 ms |
747720 KB |
Output is correct |
67 |
Correct |
344 ms |
757316 KB |
Output is correct |
68 |
Correct |
348 ms |
755208 KB |
Output is correct |
69 |
Correct |
368 ms |
757304 KB |
Output is correct |
70 |
Correct |
353 ms |
757396 KB |
Output is correct |
71 |
Correct |
345 ms |
755160 KB |
Output is correct |
72 |
Correct |
344 ms |
755316 KB |
Output is correct |
73 |
Correct |
329 ms |
757400 KB |
Output is correct |