#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)/2)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii, int> ipii;
const int MAXN = 410;
const int MAXA = 1e6+10;
const int INF = 1e9+10;
// const int INF2 = 1e18+10;
const int LOG = 19;
const int MOD = 1e9+7;
const int SQRT = 450;
const vector<int> NOL = {};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
void chmn(int &a, int b){ a = min(a, b); }
void chmx(int &a, int b){ a = max(a, b); }
int n;
string s;
int a[MAXN], siz[3];
int dp[MAXN][MAXN][MAXN][3];
vector <int> vec[3];
signed main(){
cin >> n;
cin >> s; s = '.'+s;
vec[0].pb(-1); vec[1].pb(-1); vec[2].pb(-1);
for(int i=1; i<=n; i++){
if(s[i]=='R') a[i] = 0;
else if(s[i]=='G') a[i] = 1;
else a[i] = 2; // 'Y'
vec[a[i]].pb(i);
}
for(int i=0; i<=2; i++) siz[i] = vec[i].size()-1;
for(int i=0; i<=siz[0]; i++)
for(int j=0; j<=siz[1]; j++)
for(int k=0; k<=siz[2]; k++)
for(int p=0; p<=2; p++) dp[i][j][k][p] = INF;
for(int i=0; i<=2; i++) dp[0][0][0][i] = 0;
for(int idx=1; idx<=n; idx++){
for(int x=0; x<=idx; x++){
for(int y=0; y<=idx-x; y++){
int z = idx-x-y;
if(x>siz[0] || y>siz[1] || z>siz[2]) continue;
int cost = INF;
if(x!=0){ // ambil X
int id = vec[0][x];
int yi = lower_bound(vec[1].begin(), vec[1].end(), id) - vec[1].begin() - 1;
int zi = lower_bound(vec[2].begin(), vec[2].end(), id) - vec[2].begin() - 1;
id += y-min(y,yi); id += z-min(z,zi);
cost = abs(x+y+z - id);
chmn(dp[x][y][z][0], min(dp[x-1][y][z][1], dp[x-1][y][z][2])+cost);
}
if(y!=0){
int id = vec[1][y];
int xi = lower_bound(vec[0].begin(), vec[0].end(), id) - vec[0].begin() - 1;
int zi = lower_bound(vec[2].begin(), vec[2].end(), id) - vec[2].begin() - 1;
id += x-min(x,xi); id += z-min(z,zi);
cost = abs(x+y+z - id);
chmn(dp[x][y][z][1], min(dp[x][y-1][z][0], dp[x][y-1][z][2])+cost);
}
if(z!=0){
int id = vec[2][z];
int xi = lower_bound(vec[0].begin(), vec[0].end(), id) - vec[0].begin() - 1;
int yi = lower_bound(vec[1].begin(), vec[1].end(), id) - vec[1].begin() - 1;
id += x-min(x,xi); id += y-min(y,yi);
cost = abs(x+y+z - id);
chmn(dp[x][y][z][2], min(dp[x][y][z-1][0], dp[x][y][z-1][1])+cost);
}
// cout << x << ' ' << y << ' ' << z << " xyz\n";
// for(int i=0; i<3; i++){
// cout << i << ' ' << dp[x][y][z][i] << " dp\n";
// }
}
}
}
int ANS = min(min(dp[siz[0]][siz[1]][siz[2]][0], dp[siz[0]][siz[1]][siz[2]][1]),
dp[siz[0]][siz[1]][siz[2]][2]);
cout << (ANS>=INF ? -1 : ANS) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
452 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
452 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
2392 KB |
Output is correct |
19 |
Correct |
2 ms |
2140 KB |
Output is correct |
20 |
Correct |
2 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2648 KB |
Output is correct |
22 |
Correct |
2 ms |
3916 KB |
Output is correct |
23 |
Correct |
1 ms |
2404 KB |
Output is correct |
24 |
Correct |
1 ms |
3676 KB |
Output is correct |
25 |
Correct |
2 ms |
3932 KB |
Output is correct |
26 |
Correct |
3 ms |
4188 KB |
Output is correct |
27 |
Correct |
2 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
4040 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
3928 KB |
Output is correct |
34 |
Correct |
3 ms |
5724 KB |
Output is correct |
35 |
Correct |
2 ms |
2904 KB |
Output is correct |
36 |
Correct |
1 ms |
3932 KB |
Output is correct |
37 |
Correct |
1 ms |
1884 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
65 ms |
164180 KB |
Output is correct |
3 |
Correct |
63 ms |
163408 KB |
Output is correct |
4 |
Correct |
61 ms |
164180 KB |
Output is correct |
5 |
Correct |
77 ms |
164176 KB |
Output is correct |
6 |
Correct |
64 ms |
164056 KB |
Output is correct |
7 |
Correct |
64 ms |
163412 KB |
Output is correct |
8 |
Correct |
60 ms |
164724 KB |
Output is correct |
9 |
Correct |
61 ms |
163760 KB |
Output is correct |
10 |
Correct |
64 ms |
165464 KB |
Output is correct |
11 |
Correct |
60 ms |
165396 KB |
Output is correct |
12 |
Correct |
16 ms |
46172 KB |
Output is correct |
13 |
Correct |
29 ms |
79300 KB |
Output is correct |
14 |
Correct |
49 ms |
113500 KB |
Output is correct |
15 |
Correct |
61 ms |
165520 KB |
Output is correct |
16 |
Correct |
60 ms |
165460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
452 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
2392 KB |
Output is correct |
19 |
Correct |
2 ms |
2140 KB |
Output is correct |
20 |
Correct |
2 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2648 KB |
Output is correct |
22 |
Correct |
2 ms |
3916 KB |
Output is correct |
23 |
Correct |
1 ms |
2404 KB |
Output is correct |
24 |
Correct |
1 ms |
3676 KB |
Output is correct |
25 |
Correct |
2 ms |
3932 KB |
Output is correct |
26 |
Correct |
3 ms |
4188 KB |
Output is correct |
27 |
Correct |
2 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
4040 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
3928 KB |
Output is correct |
34 |
Correct |
3 ms |
5724 KB |
Output is correct |
35 |
Correct |
2 ms |
2904 KB |
Output is correct |
36 |
Correct |
1 ms |
3932 KB |
Output is correct |
37 |
Correct |
1 ms |
1884 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
65 ms |
164180 KB |
Output is correct |
44 |
Correct |
63 ms |
163408 KB |
Output is correct |
45 |
Correct |
61 ms |
164180 KB |
Output is correct |
46 |
Correct |
77 ms |
164176 KB |
Output is correct |
47 |
Correct |
64 ms |
164056 KB |
Output is correct |
48 |
Correct |
64 ms |
163412 KB |
Output is correct |
49 |
Correct |
60 ms |
164724 KB |
Output is correct |
50 |
Correct |
61 ms |
163760 KB |
Output is correct |
51 |
Correct |
64 ms |
165464 KB |
Output is correct |
52 |
Correct |
60 ms |
165396 KB |
Output is correct |
53 |
Correct |
16 ms |
46172 KB |
Output is correct |
54 |
Correct |
29 ms |
79300 KB |
Output is correct |
55 |
Correct |
49 ms |
113500 KB |
Output is correct |
56 |
Correct |
61 ms |
165520 KB |
Output is correct |
57 |
Correct |
60 ms |
165460 KB |
Output is correct |
58 |
Correct |
240 ms |
121940 KB |
Output is correct |
59 |
Correct |
271 ms |
139124 KB |
Output is correct |
60 |
Correct |
239 ms |
131304 KB |
Output is correct |
61 |
Correct |
219 ms |
129388 KB |
Output is correct |
62 |
Correct |
69 ms |
164960 KB |
Output is correct |
63 |
Correct |
78 ms |
166352 KB |
Output is correct |
64 |
Correct |
134 ms |
165716 KB |
Output is correct |
65 |
Correct |
203 ms |
159884 KB |
Output is correct |
66 |
Correct |
184 ms |
129364 KB |
Output is correct |
67 |
Correct |
163 ms |
126804 KB |
Output is correct |
68 |
Correct |
172 ms |
132692 KB |
Output is correct |
69 |
Correct |
173 ms |
130280 KB |
Output is correct |
70 |
Correct |
195 ms |
136188 KB |
Output is correct |
71 |
Correct |
213 ms |
127828 KB |
Output is correct |
72 |
Correct |
66 ms |
51544 KB |
Output is correct |
73 |
Correct |
14 ms |
6748 KB |
Output is correct |