#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 |
1 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 |
1 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 |
1 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 |
1 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 |
2904 KB |
Output is correct |
19 |
Correct |
2 ms |
3160 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 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 |
2908 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 |
2904 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 |
2896 KB |
Output is correct |
35 |
Correct |
2 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
2 ms |
2648 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 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 |
456 ms |
757580 KB |
Output is correct |
3 |
Correct |
402 ms |
751888 KB |
Output is correct |
4 |
Correct |
398 ms |
757312 KB |
Output is correct |
5 |
Correct |
398 ms |
757756 KB |
Output is correct |
6 |
Correct |
422 ms |
757504 KB |
Output is correct |
7 |
Correct |
394 ms |
751896 KB |
Output is correct |
8 |
Correct |
403 ms |
751944 KB |
Output is correct |
9 |
Correct |
396 ms |
746324 KB |
Output is correct |
10 |
Correct |
404 ms |
757760 KB |
Output is correct |
11 |
Correct |
401 ms |
757332 KB |
Output is correct |
12 |
Correct |
62 ms |
104416 KB |
Output is correct |
13 |
Correct |
139 ms |
244604 KB |
Output is correct |
14 |
Correct |
227 ms |
426064 KB |
Output is correct |
15 |
Correct |
397 ms |
757588 KB |
Output is correct |
16 |
Correct |
394 ms |
757332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 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 |
2904 KB |
Output is correct |
19 |
Correct |
2 ms |
3160 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 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 |
2908 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 |
2904 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 |
2896 KB |
Output is correct |
35 |
Correct |
2 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
2 ms |
2648 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 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 |
456 ms |
757580 KB |
Output is correct |
44 |
Correct |
402 ms |
751888 KB |
Output is correct |
45 |
Correct |
398 ms |
757312 KB |
Output is correct |
46 |
Correct |
398 ms |
757756 KB |
Output is correct |
47 |
Correct |
422 ms |
757504 KB |
Output is correct |
48 |
Correct |
394 ms |
751896 KB |
Output is correct |
49 |
Correct |
403 ms |
751944 KB |
Output is correct |
50 |
Correct |
396 ms |
746324 KB |
Output is correct |
51 |
Correct |
404 ms |
757760 KB |
Output is correct |
52 |
Correct |
401 ms |
757332 KB |
Output is correct |
53 |
Correct |
62 ms |
104416 KB |
Output is correct |
54 |
Correct |
139 ms |
244604 KB |
Output is correct |
55 |
Correct |
227 ms |
426064 KB |
Output is correct |
56 |
Correct |
397 ms |
757588 KB |
Output is correct |
57 |
Correct |
394 ms |
757332 KB |
Output is correct |
58 |
Correct |
452 ms |
757548 KB |
Output is correct |
59 |
Correct |
423 ms |
757320 KB |
Output is correct |
60 |
Correct |
423 ms |
746288 KB |
Output is correct |
61 |
Correct |
426 ms |
752112 KB |
Output is correct |
62 |
Correct |
411 ms |
757548 KB |
Output is correct |
63 |
Correct |
411 ms |
757332 KB |
Output is correct |
64 |
Correct |
433 ms |
757564 KB |
Output is correct |
65 |
Correct |
429 ms |
751956 KB |
Output is correct |
66 |
Correct |
417 ms |
740684 KB |
Output is correct |
67 |
Correct |
433 ms |
757508 KB |
Output is correct |
68 |
Correct |
426 ms |
751760 KB |
Output is correct |
69 |
Correct |
424 ms |
757584 KB |
Output is correct |
70 |
Correct |
425 ms |
757584 KB |
Output is correct |
71 |
Correct |
425 ms |
751856 KB |
Output is correct |
72 |
Correct |
415 ms |
752372 KB |
Output is correct |
73 |
Correct |
399 ms |
757332 KB |
Output is correct |