#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
const ll N = 105;
const ll inf = 1e9 + 37;
#define endl '\n'
/*
- have fun!
- monovariants?
- invariants?
- bs on ans?
- phrase it differently?
- quirky constraints?
- dp?
- greedy?
- find slow solution and try to optimize?
*/
void solve(void){
int n;
string s;
cin >> n >> s;
s = "0" + s;
for(int i = 1; i <= n; ++i){
if(s[i] == 'R') s[i] = 0;
else if(s[i] == 'G') s[i] = 1;
else if(s[i] == 'Y') s[i] = 2;
}
vector<int> k[3];
k[0].pb(0);
k[1].pb(0);
k[2].pb(0);
for(int i = 1; i <= n; ++i)
k[(int)s[i]].push_back(i);
vector<array<int, 3> > pre(n + 1, array<int, 3>{0, 0, 0});
for(int i = 1; i <= n; ++i){
pre[i] = pre[i - 1];
pre[i][s[i]]++;
}
int dp[n + 1][n + 1][n + 1][3];
for(int r = 0; r <= n; ++r)
for(int g = 0; g <= n; ++g)
for(int y = 0; y <= n; ++y)
for(int c = 0; c <3; ++c)
dp[r][g][y][c] = inf;
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
int c[3] = {0};
// cout << pre[n][0] << ' ' << pre[n][1] << ' ' << pre[n][2] << endl;
for(c[0] = 0; c[0] <= pre[n][0]; ++c[0]){
for(c[1] = 0; c[1] <= pre[n][1]; ++c[1]){
for(c[2] = 0; c[2] <= pre[n][2]; ++c[2]){
for(int col = 0; col <3; ++col){
if(!c[0] and !c[1] and !c[2])
continue;
int& ans = dp[c[0]][c[1]][c[2]][col];
if(c[col] <= 0){
// cout << c[0] << ' ' << c[1] << ' ' << c[2] << ' ' << col << endl;
continue;
}
// cout << c[0] << ' ' << c[1] << ' ' << c[2] << ' ' << col << endl;
int i = c[0] + c[1] + c[2];
int ni = k[col][c[col]], d = 0;
for(int j = 0; j <3 ; ++j){
if(j == col)
continue;
d += (pre[ni][j] >= c[j] ? 0 : c[j] - pre[ni][j]);
}
ni += d;
for(int j = 0; j <3; ++j){
if(j == col)
continue;
ans = min(ans, dp[c[0] - (col == 0)][c[1] - (col == 1)][c[2] - (col == 2)][j] + ni - i);
}
}
}
}
}
int ans = inf;
for(int i = 0; i < 3; ++i){
ans = min(ans, dp[pre[n][0]][pre[n][1]][pre[n][2]][i]);
// cout << dp[pre[n][0]][pre[n][1]][pre[n][2]][i] << ' ';
}
if(ans == inf)
ans = -1;
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// signed t; cin >> t; while(t--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
3112 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2904 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2904 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2908 KB |
Output is correct |
34 |
Correct |
2 ms |
2904 KB |
Output is correct |
35 |
Correct |
2 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
3 ms |
2908 KB |
Output is correct |
40 |
Correct |
1 ms |
2908 KB |
Output is correct |
41 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
446 ms |
757376 KB |
Output is correct |
3 |
Correct |
404 ms |
751828 KB |
Output is correct |
4 |
Correct |
397 ms |
757328 KB |
Output is correct |
5 |
Correct |
407 ms |
757620 KB |
Output is correct |
6 |
Correct |
399 ms |
757332 KB |
Output is correct |
7 |
Correct |
397 ms |
751700 KB |
Output is correct |
8 |
Correct |
394 ms |
751700 KB |
Output is correct |
9 |
Correct |
392 ms |
746224 KB |
Output is correct |
10 |
Correct |
402 ms |
757332 KB |
Output is correct |
11 |
Correct |
424 ms |
757328 KB |
Output is correct |
12 |
Correct |
69 ms |
104532 KB |
Output is correct |
13 |
Correct |
142 ms |
244560 KB |
Output is correct |
14 |
Correct |
238 ms |
426144 KB |
Output is correct |
15 |
Correct |
438 ms |
757508 KB |
Output is correct |
16 |
Correct |
405 ms |
757464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
3112 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2904 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2904 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2908 KB |
Output is correct |
34 |
Correct |
2 ms |
2904 KB |
Output is correct |
35 |
Correct |
2 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
3 ms |
2908 KB |
Output is correct |
40 |
Correct |
1 ms |
2908 KB |
Output is correct |
41 |
Correct |
2 ms |
2908 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
446 ms |
757376 KB |
Output is correct |
44 |
Correct |
404 ms |
751828 KB |
Output is correct |
45 |
Correct |
397 ms |
757328 KB |
Output is correct |
46 |
Correct |
407 ms |
757620 KB |
Output is correct |
47 |
Correct |
399 ms |
757332 KB |
Output is correct |
48 |
Correct |
397 ms |
751700 KB |
Output is correct |
49 |
Correct |
394 ms |
751700 KB |
Output is correct |
50 |
Correct |
392 ms |
746224 KB |
Output is correct |
51 |
Correct |
402 ms |
757332 KB |
Output is correct |
52 |
Correct |
424 ms |
757328 KB |
Output is correct |
53 |
Correct |
69 ms |
104532 KB |
Output is correct |
54 |
Correct |
142 ms |
244560 KB |
Output is correct |
55 |
Correct |
238 ms |
426144 KB |
Output is correct |
56 |
Correct |
438 ms |
757508 KB |
Output is correct |
57 |
Correct |
405 ms |
757464 KB |
Output is correct |
58 |
Correct |
428 ms |
757812 KB |
Output is correct |
59 |
Correct |
427 ms |
757404 KB |
Output is correct |
60 |
Correct |
424 ms |
746320 KB |
Output is correct |
61 |
Correct |
423 ms |
751880 KB |
Output is correct |
62 |
Correct |
406 ms |
757472 KB |
Output is correct |
63 |
Correct |
409 ms |
757552 KB |
Output is correct |
64 |
Correct |
417 ms |
757764 KB |
Output is correct |
65 |
Correct |
418 ms |
751904 KB |
Output is correct |
66 |
Correct |
423 ms |
740692 KB |
Output is correct |
67 |
Correct |
434 ms |
757556 KB |
Output is correct |
68 |
Correct |
425 ms |
751696 KB |
Output is correct |
69 |
Correct |
429 ms |
757844 KB |
Output is correct |
70 |
Correct |
456 ms |
757332 KB |
Output is correct |
71 |
Correct |
425 ms |
752132 KB |
Output is correct |
72 |
Correct |
434 ms |
751700 KB |
Output is correct |
73 |
Correct |
425 ms |
757556 KB |
Output is correct |