#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 405;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
string s;
deque<int> ves[MAXN];
pii say[MAXN];
int pre[MAXN][3];
int yer[3][MAXN];
int dp[202][202][202][3];
int d(int r, int g, int y, int st){
if(r < 0 || g < 0 || y < 0) return mod;
if(r + g + y == 0) return 0;
int &ans = dp[r][g][y][st];
if(ans != -1){
return ans;
}
ans = mod;
if(st == 0){
int yr = yer[st][r];
int rr = min(r, pre[yr][0]);
int gg = min(g, pre[yr][1]);
int yy = min(y, pre[yr][2]);
ans = min(ans, d(r - 1, g, y, 1) + max(0, yr - rr - gg - yy));
ans = min(ans, d(r - 1, g, y, 2) + max(0, yr - rr - gg - yy));
}else if(st == 1){
int yr = yer[st][g];
int rr = min(r, pre[yr][0]);
int gg = min(g, pre[yr][1]);
int yy = min(y, pre[yr][2]);
ans = min(ans, d(r, g - 1, y, 0) + max(0, yr - rr - gg - yy));
ans = min(ans, d(r, g - 1, y, 2) + max(0, yr - rr - gg - yy));
}else {
int yr = yer[st][y];
int rr = min(r, pre[yr][0]);
int gg = min(g, pre[yr][1]);
int yy = min(y, pre[yr][2]);
ans = min(ans, d(r, g, y - 1, 0) + max(0, yr - rr - gg - yy));
ans = min(ans, d(r, g, y - 1, 1) + max(0, yr - rr - gg - yy));
}
return ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
#ifdef Local
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
#endif
memset(dp, -1, sizeof(dp));
cin>>n>>s;
int r = 0, g = 0, y = 0;
int idx = 1;
for(char i: s){
if(i == 'R'){
pre[idx][0]++;
r++;
yer[0][r] = idx;
}else if(i == 'G'){
pre[idx][1]++;
g++;
yer[1][g] = idx;
}else{
pre[idx][2]++;
y++;
yer[2][y] = idx;
}
if(idx)
pre[idx][0] += pre[idx - 1][0], pre[idx][1] += pre[idx - 1][1], pre[idx][2] += pre[idx - 1][2];
idx++;
}
if(max({r, g, y}) > (n + 1) / 2) {
cout<<-1<<endl;
return 0;
}
cout<<min({d(r, g, y, 0), d(r, g, y, 1), d(r, g , y, 2)});
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
97400 KB |
Output is correct |
2 |
Correct |
55 ms |
97528 KB |
Output is correct |
3 |
Correct |
53 ms |
97400 KB |
Output is correct |
4 |
Correct |
52 ms |
97400 KB |
Output is correct |
5 |
Correct |
54 ms |
97400 KB |
Output is correct |
6 |
Correct |
51 ms |
97400 KB |
Output is correct |
7 |
Correct |
54 ms |
97400 KB |
Output is correct |
8 |
Correct |
54 ms |
97400 KB |
Output is correct |
9 |
Correct |
51 ms |
97400 KB |
Output is correct |
10 |
Correct |
50 ms |
97404 KB |
Output is correct |
11 |
Correct |
59 ms |
97400 KB |
Output is correct |
12 |
Correct |
58 ms |
97400 KB |
Output is correct |
13 |
Correct |
51 ms |
97400 KB |
Output is correct |
14 |
Correct |
50 ms |
97400 KB |
Output is correct |
15 |
Correct |
53 ms |
97400 KB |
Output is correct |
16 |
Correct |
53 ms |
97400 KB |
Output is correct |
17 |
Correct |
57 ms |
97420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
97400 KB |
Output is correct |
2 |
Correct |
55 ms |
97528 KB |
Output is correct |
3 |
Correct |
53 ms |
97400 KB |
Output is correct |
4 |
Correct |
52 ms |
97400 KB |
Output is correct |
5 |
Correct |
54 ms |
97400 KB |
Output is correct |
6 |
Correct |
51 ms |
97400 KB |
Output is correct |
7 |
Correct |
54 ms |
97400 KB |
Output is correct |
8 |
Correct |
54 ms |
97400 KB |
Output is correct |
9 |
Correct |
51 ms |
97400 KB |
Output is correct |
10 |
Correct |
50 ms |
97404 KB |
Output is correct |
11 |
Correct |
59 ms |
97400 KB |
Output is correct |
12 |
Correct |
58 ms |
97400 KB |
Output is correct |
13 |
Correct |
51 ms |
97400 KB |
Output is correct |
14 |
Correct |
50 ms |
97400 KB |
Output is correct |
15 |
Correct |
53 ms |
97400 KB |
Output is correct |
16 |
Correct |
53 ms |
97400 KB |
Output is correct |
17 |
Correct |
57 ms |
97420 KB |
Output is correct |
18 |
Correct |
52 ms |
97400 KB |
Output is correct |
19 |
Correct |
56 ms |
97548 KB |
Output is correct |
20 |
Correct |
59 ms |
97380 KB |
Output is correct |
21 |
Correct |
80 ms |
97400 KB |
Output is correct |
22 |
Correct |
56 ms |
97400 KB |
Output is correct |
23 |
Correct |
51 ms |
97412 KB |
Output is correct |
24 |
Correct |
51 ms |
97400 KB |
Output is correct |
25 |
Correct |
59 ms |
97400 KB |
Output is correct |
26 |
Correct |
51 ms |
97400 KB |
Output is correct |
27 |
Correct |
58 ms |
97404 KB |
Output is correct |
28 |
Correct |
51 ms |
97400 KB |
Output is correct |
29 |
Correct |
60 ms |
97400 KB |
Output is correct |
30 |
Correct |
52 ms |
97528 KB |
Output is correct |
31 |
Correct |
50 ms |
97400 KB |
Output is correct |
32 |
Correct |
52 ms |
97400 KB |
Output is correct |
33 |
Correct |
51 ms |
97400 KB |
Output is correct |
34 |
Correct |
51 ms |
97400 KB |
Output is correct |
35 |
Correct |
51 ms |
97416 KB |
Output is correct |
36 |
Correct |
57 ms |
97528 KB |
Output is correct |
37 |
Correct |
52 ms |
97400 KB |
Output is correct |
38 |
Correct |
52 ms |
97400 KB |
Output is correct |
39 |
Correct |
52 ms |
97400 KB |
Output is correct |
40 |
Correct |
60 ms |
97400 KB |
Output is correct |
41 |
Correct |
52 ms |
97400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
97400 KB |
Output is correct |
2 |
Correct |
58 ms |
97400 KB |
Output is correct |
3 |
Correct |
51 ms |
97400 KB |
Output is correct |
4 |
Correct |
51 ms |
97452 KB |
Output is correct |
5 |
Correct |
51 ms |
97400 KB |
Output is correct |
6 |
Correct |
54 ms |
97476 KB |
Output is correct |
7 |
Correct |
57 ms |
97400 KB |
Output is correct |
8 |
Correct |
51 ms |
97528 KB |
Output is correct |
9 |
Correct |
52 ms |
97400 KB |
Output is correct |
10 |
Correct |
51 ms |
97400 KB |
Output is correct |
11 |
Correct |
60 ms |
97400 KB |
Output is correct |
12 |
Correct |
52 ms |
97400 KB |
Output is correct |
13 |
Correct |
51 ms |
97400 KB |
Output is correct |
14 |
Correct |
58 ms |
97432 KB |
Output is correct |
15 |
Correct |
58 ms |
97400 KB |
Output is correct |
16 |
Correct |
52 ms |
97400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
97400 KB |
Output is correct |
2 |
Correct |
55 ms |
97528 KB |
Output is correct |
3 |
Correct |
53 ms |
97400 KB |
Output is correct |
4 |
Correct |
52 ms |
97400 KB |
Output is correct |
5 |
Correct |
54 ms |
97400 KB |
Output is correct |
6 |
Correct |
51 ms |
97400 KB |
Output is correct |
7 |
Correct |
54 ms |
97400 KB |
Output is correct |
8 |
Correct |
54 ms |
97400 KB |
Output is correct |
9 |
Correct |
51 ms |
97400 KB |
Output is correct |
10 |
Correct |
50 ms |
97404 KB |
Output is correct |
11 |
Correct |
59 ms |
97400 KB |
Output is correct |
12 |
Correct |
58 ms |
97400 KB |
Output is correct |
13 |
Correct |
51 ms |
97400 KB |
Output is correct |
14 |
Correct |
50 ms |
97400 KB |
Output is correct |
15 |
Correct |
53 ms |
97400 KB |
Output is correct |
16 |
Correct |
53 ms |
97400 KB |
Output is correct |
17 |
Correct |
57 ms |
97420 KB |
Output is correct |
18 |
Correct |
52 ms |
97400 KB |
Output is correct |
19 |
Correct |
56 ms |
97548 KB |
Output is correct |
20 |
Correct |
59 ms |
97380 KB |
Output is correct |
21 |
Correct |
80 ms |
97400 KB |
Output is correct |
22 |
Correct |
56 ms |
97400 KB |
Output is correct |
23 |
Correct |
51 ms |
97412 KB |
Output is correct |
24 |
Correct |
51 ms |
97400 KB |
Output is correct |
25 |
Correct |
59 ms |
97400 KB |
Output is correct |
26 |
Correct |
51 ms |
97400 KB |
Output is correct |
27 |
Correct |
58 ms |
97404 KB |
Output is correct |
28 |
Correct |
51 ms |
97400 KB |
Output is correct |
29 |
Correct |
60 ms |
97400 KB |
Output is correct |
30 |
Correct |
52 ms |
97528 KB |
Output is correct |
31 |
Correct |
50 ms |
97400 KB |
Output is correct |
32 |
Correct |
52 ms |
97400 KB |
Output is correct |
33 |
Correct |
51 ms |
97400 KB |
Output is correct |
34 |
Correct |
51 ms |
97400 KB |
Output is correct |
35 |
Correct |
51 ms |
97416 KB |
Output is correct |
36 |
Correct |
57 ms |
97528 KB |
Output is correct |
37 |
Correct |
52 ms |
97400 KB |
Output is correct |
38 |
Correct |
52 ms |
97400 KB |
Output is correct |
39 |
Correct |
52 ms |
97400 KB |
Output is correct |
40 |
Correct |
60 ms |
97400 KB |
Output is correct |
41 |
Correct |
52 ms |
97400 KB |
Output is correct |
42 |
Correct |
52 ms |
97400 KB |
Output is correct |
43 |
Correct |
58 ms |
97400 KB |
Output is correct |
44 |
Correct |
51 ms |
97400 KB |
Output is correct |
45 |
Correct |
51 ms |
97452 KB |
Output is correct |
46 |
Correct |
51 ms |
97400 KB |
Output is correct |
47 |
Correct |
54 ms |
97476 KB |
Output is correct |
48 |
Correct |
57 ms |
97400 KB |
Output is correct |
49 |
Correct |
51 ms |
97528 KB |
Output is correct |
50 |
Correct |
52 ms |
97400 KB |
Output is correct |
51 |
Correct |
51 ms |
97400 KB |
Output is correct |
52 |
Correct |
60 ms |
97400 KB |
Output is correct |
53 |
Correct |
52 ms |
97400 KB |
Output is correct |
54 |
Correct |
51 ms |
97400 KB |
Output is correct |
55 |
Correct |
58 ms |
97432 KB |
Output is correct |
56 |
Correct |
58 ms |
97400 KB |
Output is correct |
57 |
Correct |
52 ms |
97400 KB |
Output is correct |
58 |
Correct |
150 ms |
97528 KB |
Output is correct |
59 |
Correct |
150 ms |
97408 KB |
Output is correct |
60 |
Correct |
148 ms |
97400 KB |
Output is correct |
61 |
Correct |
154 ms |
97400 KB |
Output is correct |
62 |
Correct |
54 ms |
97400 KB |
Output is correct |
63 |
Correct |
54 ms |
97400 KB |
Output is correct |
64 |
Correct |
75 ms |
97400 KB |
Output is correct |
65 |
Correct |
83 ms |
97400 KB |
Output is correct |
66 |
Correct |
165 ms |
97528 KB |
Output is correct |
67 |
Correct |
170 ms |
97528 KB |
Output is correct |
68 |
Correct |
256 ms |
97404 KB |
Output is correct |
69 |
Correct |
193 ms |
97528 KB |
Output is correct |
70 |
Correct |
147 ms |
97404 KB |
Output is correct |
71 |
Correct |
140 ms |
97400 KB |
Output is correct |
72 |
Correct |
51 ms |
97400 KB |
Output is correct |
73 |
Correct |
51 ms |
97400 KB |
Output is correct |