#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#if 0
const int MOD = ;
inline int lgput(int a, int b) {
int ans = 1;
while(b > 0) {
if(b & 1) ans = (1LL * ans * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return ans;
}
inline void mod(int &x) {
if(x >= MOD)
x -= MOD;
}
inline void add(int &x, int y) {
x += y;
mod(x);
}
inline void sub(int &x, int y) {
x += MOD - y;
mod(x);
}
inline void mul(int &x, int y) {
x = (1LL * x * y) % MOD;
}
inline int inv(int x) {
return lgput(x, MOD - 2);
}
#endif
#if 0
int fact[], invfact[];
inline void prec(int n) {
fact[0] = 1;
for(int i = 1; i <= n; i++) {
fact[i] = (1LL * fact[i - 1] * i) % MOD;
}
invfact[n] = lgput(fact[n], MOD - 2);
for(int i = n - 1; i >= 0; i--) {
invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
}
}
inline int comb(int n, int k) {
if(n < k) return 0;
return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif
using namespace std;
const int INF = 1e9;
const int MAXN = 400;
int dp[MAXN + 1][MAXN + 1][MAXN + 1][3];
int num[3][3][MAXN + 1][MAXN + 1];
int main() {
#if 0
ifstream cin("A.in");
ofstream cout("A.out");
#endif
int i, j, n;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
string str;
cin >> n >> str;
str = " " + str;
vector <int> ind('Z'), fr(3), pos[3];
ind['R'] = 0, ind['Y'] = 1, ind['G'] = 2;
for(i = 1; i <= n; i++) {
str[i] = ind[str[i]];
fr[str[i]]++;
}
for(i = 0; i < 3; i++) {
pos[i].push_back(0);
}
for(i = 1; i <= n; i++) {
pos[str[i]].push_back(i);
}
for(int a = 0; a < 3; a++) {
for(int b = 0; b < 3; b++) {
for(i = 0; i <= fr[a]; i++) {
int cnt = 0;
for(j = 0; j <= fr[b]; j++) {
if(pos[a][i] < pos[b][j]) {
cnt++;
}
num[a][b][i][j] = cnt;
}
}
}
}
for(int a = 0; a <= fr[0]; a++) {
for(int b = 0; b <= fr[1]; b++) {
for(int c = 0; c <= fr[2]; c++) {
for(i = 0; i < 3; i++) {
dp[a][b][c][i] = INF;
}
}
}
}
for(i = 0; i < 3; i++) {
dp[0][0][0][i] = 0;
}
for(int a = 0; a <= fr[0]; a++) {
for(int b = 0; b <= fr[1]; b++) {
for(int c = 0; c <= fr[2]; c++) {
int p = a + b + c;
if(p == 0) continue;
for(int d = 0; d < 3; d++) {
for(int e = 0; e < 3; e++) {
if(d == e) continue;
int cur;
if(d == 0) {
if(a == 0) continue;
cur = dp[a - 1][b][c][e] + (pos[0][a] + num[0][1][a][b] + num[0][2][a][c]) - p;
}
if(d == 1) {
if(b == 0) continue;
cur = dp[a][b - 1][c][e] + (pos[1][b] + num[1][0][b][a] + num[1][2][b][c]) - p;
}
if(d == 2) {
if(c == 0) continue;
cur = dp[a][b][c - 1][e] + (pos[2][c] + num[2][0][c][a] + num[2][1][c][b]) - p;
}
dp[a][b][c][d] = min(dp[a][b][c][d], cur);
}
}
}
}
}
int ans = INF;
for(i = 0; i < 3; i++) {
ans = min(ans, dp[fr[0]][fr[1]][fr[2]][i]);
}
if(ans == INF) {
ans = -1;
}
cout << ans;
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:97:19: warning: array subscript has type 'char' [-Wchar-subscripts]
pos[str[i]].push_back(i);
^
joi2019_ho_t3.cpp:139:29: warning: 'cur' may be used uninitialized in this function [-Wmaybe-uninitialized]
int cur;
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
632 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
632 KB |
Output is correct |
15 |
Correct |
2 ms |
632 KB |
Output is correct |
16 |
Correct |
2 ms |
632 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
632 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
632 KB |
Output is correct |
15 |
Correct |
2 ms |
632 KB |
Output is correct |
16 |
Correct |
2 ms |
632 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
4 ms |
2808 KB |
Output is correct |
19 |
Correct |
4 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2936 KB |
Output is correct |
21 |
Correct |
4 ms |
2424 KB |
Output is correct |
22 |
Correct |
5 ms |
3452 KB |
Output is correct |
23 |
Correct |
4 ms |
2424 KB |
Output is correct |
24 |
Correct |
3 ms |
1784 KB |
Output is correct |
25 |
Correct |
3 ms |
1144 KB |
Output is correct |
26 |
Correct |
2 ms |
1144 KB |
Output is correct |
27 |
Correct |
3 ms |
2168 KB |
Output is correct |
28 |
Correct |
4 ms |
2680 KB |
Output is correct |
29 |
Correct |
4 ms |
2808 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
1 ms |
2296 KB |
Output is correct |
32 |
Correct |
4 ms |
2296 KB |
Output is correct |
33 |
Correct |
3 ms |
1016 KB |
Output is correct |
34 |
Correct |
3 ms |
1272 KB |
Output is correct |
35 |
Correct |
4 ms |
2296 KB |
Output is correct |
36 |
Correct |
5 ms |
2908 KB |
Output is correct |
37 |
Correct |
4 ms |
2040 KB |
Output is correct |
38 |
Correct |
3 ms |
2040 KB |
Output is correct |
39 |
Correct |
4 ms |
2424 KB |
Output is correct |
40 |
Correct |
2 ms |
760 KB |
Output is correct |
41 |
Correct |
3 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
4316 KB |
Output is correct |
3 |
Correct |
6 ms |
4216 KB |
Output is correct |
4 |
Correct |
6 ms |
4344 KB |
Output is correct |
5 |
Correct |
6 ms |
4216 KB |
Output is correct |
6 |
Correct |
6 ms |
4216 KB |
Output is correct |
7 |
Correct |
6 ms |
4316 KB |
Output is correct |
8 |
Correct |
7 ms |
4216 KB |
Output is correct |
9 |
Correct |
2 ms |
4216 KB |
Output is correct |
10 |
Correct |
6 ms |
4216 KB |
Output is correct |
11 |
Correct |
6 ms |
4316 KB |
Output is correct |
12 |
Correct |
4 ms |
2296 KB |
Output is correct |
13 |
Correct |
5 ms |
2936 KB |
Output is correct |
14 |
Correct |
5 ms |
3576 KB |
Output is correct |
15 |
Correct |
6 ms |
4216 KB |
Output is correct |
16 |
Correct |
7 ms |
4316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
632 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
632 KB |
Output is correct |
15 |
Correct |
2 ms |
632 KB |
Output is correct |
16 |
Correct |
2 ms |
632 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
4 ms |
2808 KB |
Output is correct |
19 |
Correct |
4 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2936 KB |
Output is correct |
21 |
Correct |
4 ms |
2424 KB |
Output is correct |
22 |
Correct |
5 ms |
3452 KB |
Output is correct |
23 |
Correct |
4 ms |
2424 KB |
Output is correct |
24 |
Correct |
3 ms |
1784 KB |
Output is correct |
25 |
Correct |
3 ms |
1144 KB |
Output is correct |
26 |
Correct |
2 ms |
1144 KB |
Output is correct |
27 |
Correct |
3 ms |
2168 KB |
Output is correct |
28 |
Correct |
4 ms |
2680 KB |
Output is correct |
29 |
Correct |
4 ms |
2808 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
1 ms |
2296 KB |
Output is correct |
32 |
Correct |
4 ms |
2296 KB |
Output is correct |
33 |
Correct |
3 ms |
1016 KB |
Output is correct |
34 |
Correct |
3 ms |
1272 KB |
Output is correct |
35 |
Correct |
4 ms |
2296 KB |
Output is correct |
36 |
Correct |
5 ms |
2908 KB |
Output is correct |
37 |
Correct |
4 ms |
2040 KB |
Output is correct |
38 |
Correct |
3 ms |
2040 KB |
Output is correct |
39 |
Correct |
4 ms |
2424 KB |
Output is correct |
40 |
Correct |
2 ms |
760 KB |
Output is correct |
41 |
Correct |
3 ms |
1016 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
6 ms |
4316 KB |
Output is correct |
44 |
Correct |
6 ms |
4216 KB |
Output is correct |
45 |
Correct |
6 ms |
4344 KB |
Output is correct |
46 |
Correct |
6 ms |
4216 KB |
Output is correct |
47 |
Correct |
6 ms |
4216 KB |
Output is correct |
48 |
Correct |
6 ms |
4316 KB |
Output is correct |
49 |
Correct |
7 ms |
4216 KB |
Output is correct |
50 |
Correct |
2 ms |
4216 KB |
Output is correct |
51 |
Correct |
6 ms |
4216 KB |
Output is correct |
52 |
Correct |
6 ms |
4316 KB |
Output is correct |
53 |
Correct |
4 ms |
2296 KB |
Output is correct |
54 |
Correct |
5 ms |
2936 KB |
Output is correct |
55 |
Correct |
5 ms |
3576 KB |
Output is correct |
56 |
Correct |
6 ms |
4216 KB |
Output is correct |
57 |
Correct |
7 ms |
4316 KB |
Output is correct |
58 |
Correct |
142 ms |
90864 KB |
Output is correct |
59 |
Correct |
141 ms |
84116 KB |
Output is correct |
60 |
Correct |
152 ms |
90316 KB |
Output is correct |
61 |
Correct |
150 ms |
88568 KB |
Output is correct |
62 |
Correct |
14 ms |
8440 KB |
Output is correct |
63 |
Correct |
20 ms |
11640 KB |
Output is correct |
64 |
Correct |
66 ms |
34176 KB |
Output is correct |
65 |
Correct |
91 ms |
46740 KB |
Output is correct |
66 |
Correct |
143 ms |
88004 KB |
Output is correct |
67 |
Correct |
138 ms |
75640 KB |
Output is correct |
68 |
Correct |
142 ms |
86648 KB |
Output is correct |
69 |
Correct |
157 ms |
90708 KB |
Output is correct |
70 |
Correct |
140 ms |
82808 KB |
Output is correct |
71 |
Correct |
153 ms |
99420 KB |
Output is correct |
72 |
Correct |
97 ms |
97400 KB |
Output is correct |
73 |
Correct |
14 ms |
9208 KB |
Output is correct |