/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 405, M = 1e5+10, K = 52, MX = 30;
int n, dp[N][N][N][3], pref[N][3];
string s, c = "RGY";
int m(char x){
if(x==c[0]) return 0;
if(x==c[1]) return 1;
return 2;
}
void solve(){
cin >> n >> s;
array<int, 3> x = {0, 0, 0};
for(int i = 0 ;i < n; ++i){
x[m(s[i])]++;
}
if(max({x[0], x[1], x[2]}) > (n+1)/2){
cout << -1;
return;
}
vector<int> v[3];
for(int i = 0; i < n; ++i){
v[m(s[i])].pb(i + 1);
}
// for(auto x: v[0]) cout << x << ' '; en;
// for(auto x: v[1]) cout << x << ' '; en;
// for(auto x: v[2]) cout << x << ' '; en;
for(int i = 0; i <= n; ++i) for(int j = 0; j <= n; ++j) for(int l = 0; l <= n; ++l) dp[i][j][l][0] = dp[i][j][l][1] = dp[i][j][l][2] = MOD;
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
pref[0][0] = pref[0][1] = pref[0][2] = 0;
for(int i = 1; i <= n; ++i){
pref[i][0] = pref[i - 1][0];
pref[i][1] = pref[i - 1][1];
pref[i][2] = pref[i - 1][2];
pref[i][m(s[i-1])]++;
}
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= min(i, x[0]); ++j){
for(int l = 0; l <= min(i - j, x[1]); ++l){
for(int x1 = 0; x1 < 3; ++x1){
array<int, 3> y = {j, l, i-j-l};
if(y[0] > x[0]) continue;
if(y[1] > x[1]) continue;
if(y[2] > x[2]) continue;
if(y[x1] == 0) continue;
int cost = v[x1][y[x1] - 1];
int add = 0;
for(int t = 0; t < 3; ++t){
add += min(pref[cost - 1][t], y[t] - (t == x1));
}
cost += i - add - 1;
if(x1 == 0) y[0]--;
if(x1 == 1) y[1]--;
if(x1 == 2) y[2]--;
for(int x2 = 0; x2 < 3; ++x2){
if(x1==x2) continue;
dp[i][j][l][x1] = min(dp[i][j][l][x1], dp[i - 1][y[0]][y[1]][x2] + cost - i);
}
}
// en;
}
}
}
cout << min({dp[n][x[0]][x[1]][0], dp[n][x[0]][x[1]][1], dp[n][x[0]][x[1]][2]});
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:88:15: warning: unused variable 'aa' [-Wunused-variable]
88 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
3 ms |
20824 KB |
Output is correct |
5 |
Correct |
5 ms |
31064 KB |
Output is correct |
6 |
Correct |
5 ms |
31320 KB |
Output is correct |
7 |
Correct |
4 ms |
31064 KB |
Output is correct |
8 |
Correct |
5 ms |
31068 KB |
Output is correct |
9 |
Correct |
5 ms |
31320 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
31064 KB |
Output is correct |
12 |
Correct |
5 ms |
31320 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
4 ms |
29020 KB |
Output is correct |
15 |
Correct |
5 ms |
31064 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
3 ms |
20824 KB |
Output is correct |
5 |
Correct |
5 ms |
31064 KB |
Output is correct |
6 |
Correct |
5 ms |
31320 KB |
Output is correct |
7 |
Correct |
4 ms |
31064 KB |
Output is correct |
8 |
Correct |
5 ms |
31068 KB |
Output is correct |
9 |
Correct |
5 ms |
31320 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
31064 KB |
Output is correct |
12 |
Correct |
5 ms |
31320 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
4 ms |
29020 KB |
Output is correct |
15 |
Correct |
5 ms |
31064 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
21 ms |
117848 KB |
Output is correct |
19 |
Correct |
16 ms |
117328 KB |
Output is correct |
20 |
Correct |
16 ms |
117596 KB |
Output is correct |
21 |
Correct |
17 ms |
117596 KB |
Output is correct |
22 |
Correct |
16 ms |
117592 KB |
Output is correct |
23 |
Correct |
16 ms |
117592 KB |
Output is correct |
24 |
Correct |
16 ms |
117592 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
16 ms |
117592 KB |
Output is correct |
28 |
Correct |
16 ms |
117592 KB |
Output is correct |
29 |
Correct |
16 ms |
117592 KB |
Output is correct |
30 |
Correct |
16 ms |
117592 KB |
Output is correct |
31 |
Correct |
17 ms |
117592 KB |
Output is correct |
32 |
Correct |
17 ms |
117592 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
16 ms |
113496 KB |
Output is correct |
36 |
Correct |
16 ms |
115544 KB |
Output is correct |
37 |
Correct |
15 ms |
111192 KB |
Output is correct |
38 |
Correct |
16 ms |
117592 KB |
Output is correct |
39 |
Correct |
16 ms |
117592 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
310 ms |
769368 KB |
Output is correct |
3 |
Correct |
291 ms |
766568 KB |
Output is correct |
4 |
Correct |
281 ms |
769364 KB |
Output is correct |
5 |
Correct |
278 ms |
769360 KB |
Output is correct |
6 |
Correct |
284 ms |
769472 KB |
Output is correct |
7 |
Correct |
279 ms |
766548 KB |
Output is correct |
8 |
Correct |
284 ms |
766588 KB |
Output is correct |
9 |
Correct |
275 ms |
763728 KB |
Output is correct |
10 |
Correct |
280 ms |
769624 KB |
Output is correct |
11 |
Correct |
273 ms |
769616 KB |
Output is correct |
12 |
Correct |
68 ms |
388688 KB |
Output is correct |
13 |
Correct |
119 ms |
482128 KB |
Output is correct |
14 |
Correct |
187 ms |
591184 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
3 ms |
20824 KB |
Output is correct |
5 |
Correct |
5 ms |
31064 KB |
Output is correct |
6 |
Correct |
5 ms |
31320 KB |
Output is correct |
7 |
Correct |
4 ms |
31064 KB |
Output is correct |
8 |
Correct |
5 ms |
31068 KB |
Output is correct |
9 |
Correct |
5 ms |
31320 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
31064 KB |
Output is correct |
12 |
Correct |
5 ms |
31320 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
4 ms |
29020 KB |
Output is correct |
15 |
Correct |
5 ms |
31064 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
21 ms |
117848 KB |
Output is correct |
19 |
Correct |
16 ms |
117328 KB |
Output is correct |
20 |
Correct |
16 ms |
117596 KB |
Output is correct |
21 |
Correct |
17 ms |
117596 KB |
Output is correct |
22 |
Correct |
16 ms |
117592 KB |
Output is correct |
23 |
Correct |
16 ms |
117592 KB |
Output is correct |
24 |
Correct |
16 ms |
117592 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
16 ms |
117592 KB |
Output is correct |
28 |
Correct |
16 ms |
117592 KB |
Output is correct |
29 |
Correct |
16 ms |
117592 KB |
Output is correct |
30 |
Correct |
16 ms |
117592 KB |
Output is correct |
31 |
Correct |
17 ms |
117592 KB |
Output is correct |
32 |
Correct |
17 ms |
117592 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
16 ms |
113496 KB |
Output is correct |
36 |
Correct |
16 ms |
115544 KB |
Output is correct |
37 |
Correct |
15 ms |
111192 KB |
Output is correct |
38 |
Correct |
16 ms |
117592 KB |
Output is correct |
39 |
Correct |
16 ms |
117592 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
6488 KB |
Output is correct |
43 |
Correct |
310 ms |
769368 KB |
Output is correct |
44 |
Correct |
291 ms |
766568 KB |
Output is correct |
45 |
Correct |
281 ms |
769364 KB |
Output is correct |
46 |
Correct |
278 ms |
769360 KB |
Output is correct |
47 |
Correct |
284 ms |
769472 KB |
Output is correct |
48 |
Correct |
279 ms |
766548 KB |
Output is correct |
49 |
Correct |
284 ms |
766588 KB |
Output is correct |
50 |
Correct |
275 ms |
763728 KB |
Output is correct |
51 |
Correct |
280 ms |
769624 KB |
Output is correct |
52 |
Correct |
273 ms |
769616 KB |
Output is correct |
53 |
Correct |
68 ms |
388688 KB |
Output is correct |
54 |
Correct |
119 ms |
482128 KB |
Output is correct |
55 |
Correct |
187 ms |
591184 KB |
Output is correct |
56 |
Correct |
0 ms |
344 KB |
Output is correct |
57 |
Correct |
0 ms |
344 KB |
Output is correct |
58 |
Correct |
344 ms |
769656 KB |
Output is correct |
59 |
Correct |
336 ms |
769568 KB |
Output is correct |
60 |
Correct |
323 ms |
763728 KB |
Output is correct |
61 |
Correct |
345 ms |
767056 KB |
Output is correct |
62 |
Correct |
1 ms |
344 KB |
Output is correct |
63 |
Correct |
295 ms |
769628 KB |
Output is correct |
64 |
Correct |
316 ms |
769496 KB |
Output is correct |
65 |
Correct |
318 ms |
766732 KB |
Output is correct |
66 |
Correct |
328 ms |
760980 KB |
Output is correct |
67 |
Correct |
329 ms |
769816 KB |
Output is correct |
68 |
Correct |
333 ms |
766544 KB |
Output is correct |
69 |
Correct |
333 ms |
769616 KB |
Output is correct |
70 |
Correct |
333 ms |
769616 KB |
Output is correct |
71 |
Correct |
326 ms |
766548 KB |
Output is correct |
72 |
Correct |
0 ms |
344 KB |
Output is correct |
73 |
Correct |
0 ms |
344 KB |
Output is correct |