#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
const int INF = INT_MAX;
const int LNF = INF*INF;
const int MOD = 1000000007;
const int mod = 998244353;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
const int MAX = 410;
int n;
char arr[MAX];
int R, G, Y;
int rp[MAX], gp[MAX], yp[MAX];
int rpref[MAX], gpref[MAX], ypref[MAX];
vector<vector<vector<vi>>> dp;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
FOR(i, 1, n, 1){
cin>>arr[i];
if(arr[i] == 'R') rp[++R] = i, rpref[i] = 1;
if(arr[i] == 'G') gp[++G] = i, gpref[i] = 1;
if(arr[i] == 'Y') yp[++Y] = i, ypref[i] = 1;
}
FOR(i, 1, n, 1){
rpref[i] = rpref[i-1] + rpref[i];
gpref[i] = gpref[i-1] + gpref[i];
ypref[i] = ypref[i-1] + ypref[i];
}
if(max({R, G, Y}) > (n + 1) / 2){
cout<<-1<<'\n';
return 0;
}
dp.resize(3);
for(auto& i : dp){
i.resize(R+1);
for(auto& j : i){
j.resize(G+1);
for(auto& k : j){
k.resize(Y+1);
for(int& l : k){
l = INF;
}
}
}
}
dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0;
FOR(i, 0, R, 1){
FOR(j, 0, G, 1){
FOR(k, 0, Y, 1){
if(i > 0){
int dr = max(gpref[rp[i]] - j, (int)0) + max(ypref[rp[i]] - k, (int)0);
dp[0][i][j][k] = min({dp[0][i][j][k], dp[1][i-1][j][k] + dr, dp[2][i-1][j][k] + dr});
//cerr<<"dp[0]["<<i<<"]["<<j<<"]["<<k<<"] = "<<dp[0][i][j][k]<<" ";
}
if(j > 0){
int dg = max(rpref[gp[j]] - i, (int)0) + max(ypref[gp[j]] - k, (int)0);
dp[1][i][j][k] = min({dp[1][i][j][k], dp[0][i][j-1][k] + dg, dp[2][i][j-1][k] + dg});
//cerr<<"dp[1]["<<i<<"]["<<j<<"]["<<k<<"] = "<<dp[1][i][j][k]<<" ";
}
if(k > 0){
int dy = max(rpref[yp[k]] - i, (int)0) + max(gpref[yp[k]] - j, (int)0);
dp[2][i][j][k] = min({dp[2][i][j][k], dp[0][i][j][k-1] + dy, dp[1][i][j][k-1] + dy});
//cerr<<"dp[2]["<<i<<"]["<<j<<"]["<<k<<"] = "<<dp[2][i][j][k]<<" ";
}
//cerr<<endl;
}
//cerr<<endl;
}
}
int res = min({dp[0][R][G][Y], dp[1][R][G][Y], dp[2][R][G][Y]});
cout<<res<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
1 ms |
580 KB |
Output is correct |
22 |
Correct |
1 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
440 KB |
Output is correct |
25 |
Correct |
0 ms |
336 KB |
Output is correct |
26 |
Correct |
0 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
576 KB |
Output is correct |
30 |
Correct |
1 ms |
576 KB |
Output is correct |
31 |
Correct |
1 ms |
456 KB |
Output is correct |
32 |
Correct |
1 ms |
564 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
0 ms |
208 KB |
Output is correct |
35 |
Correct |
1 ms |
580 KB |
Output is correct |
36 |
Correct |
1 ms |
440 KB |
Output is correct |
37 |
Correct |
1 ms |
464 KB |
Output is correct |
38 |
Correct |
2 ms |
576 KB |
Output is correct |
39 |
Correct |
1 ms |
592 KB |
Output is correct |
40 |
Correct |
1 ms |
208 KB |
Output is correct |
41 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
9 ms |
6992 KB |
Output is correct |
3 |
Correct |
9 ms |
6860 KB |
Output is correct |
4 |
Correct |
9 ms |
6988 KB |
Output is correct |
5 |
Correct |
8 ms |
6880 KB |
Output is correct |
6 |
Correct |
8 ms |
6988 KB |
Output is correct |
7 |
Correct |
8 ms |
6860 KB |
Output is correct |
8 |
Correct |
11 ms |
6860 KB |
Output is correct |
9 |
Correct |
9 ms |
6972 KB |
Output is correct |
10 |
Correct |
9 ms |
6988 KB |
Output is correct |
11 |
Correct |
9 ms |
6908 KB |
Output is correct |
12 |
Correct |
2 ms |
2112 KB |
Output is correct |
13 |
Correct |
4 ms |
3404 KB |
Output is correct |
14 |
Correct |
6 ms |
4812 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
1 ms |
580 KB |
Output is correct |
22 |
Correct |
1 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
440 KB |
Output is correct |
25 |
Correct |
0 ms |
336 KB |
Output is correct |
26 |
Correct |
0 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
576 KB |
Output is correct |
30 |
Correct |
1 ms |
576 KB |
Output is correct |
31 |
Correct |
1 ms |
456 KB |
Output is correct |
32 |
Correct |
1 ms |
564 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
0 ms |
208 KB |
Output is correct |
35 |
Correct |
1 ms |
580 KB |
Output is correct |
36 |
Correct |
1 ms |
440 KB |
Output is correct |
37 |
Correct |
1 ms |
464 KB |
Output is correct |
38 |
Correct |
2 ms |
576 KB |
Output is correct |
39 |
Correct |
1 ms |
592 KB |
Output is correct |
40 |
Correct |
1 ms |
208 KB |
Output is correct |
41 |
Correct |
1 ms |
320 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
9 ms |
6992 KB |
Output is correct |
44 |
Correct |
9 ms |
6860 KB |
Output is correct |
45 |
Correct |
9 ms |
6988 KB |
Output is correct |
46 |
Correct |
8 ms |
6880 KB |
Output is correct |
47 |
Correct |
8 ms |
6988 KB |
Output is correct |
48 |
Correct |
8 ms |
6860 KB |
Output is correct |
49 |
Correct |
11 ms |
6860 KB |
Output is correct |
50 |
Correct |
9 ms |
6972 KB |
Output is correct |
51 |
Correct |
9 ms |
6988 KB |
Output is correct |
52 |
Correct |
9 ms |
6908 KB |
Output is correct |
53 |
Correct |
2 ms |
2112 KB |
Output is correct |
54 |
Correct |
4 ms |
3404 KB |
Output is correct |
55 |
Correct |
6 ms |
4812 KB |
Output is correct |
56 |
Correct |
1 ms |
332 KB |
Output is correct |
57 |
Correct |
0 ms |
312 KB |
Output is correct |
58 |
Correct |
66 ms |
58172 KB |
Output is correct |
59 |
Correct |
75 ms |
58548 KB |
Output is correct |
60 |
Correct |
67 ms |
58348 KB |
Output is correct |
61 |
Correct |
68 ms |
58816 KB |
Output is correct |
62 |
Correct |
0 ms |
336 KB |
Output is correct |
63 |
Correct |
17 ms |
12112 KB |
Output is correct |
64 |
Correct |
38 ms |
31352 KB |
Output is correct |
65 |
Correct |
53 ms |
43904 KB |
Output is correct |
66 |
Correct |
67 ms |
58084 KB |
Output is correct |
67 |
Correct |
67 ms |
57500 KB |
Output is correct |
68 |
Correct |
69 ms |
58916 KB |
Output is correct |
69 |
Correct |
71 ms |
59224 KB |
Output is correct |
70 |
Correct |
70 ms |
58824 KB |
Output is correct |
71 |
Correct |
69 ms |
57308 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
336 KB |
Output is correct |