#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 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;
}
}
}
}
vector < vector < vector < vector <int> > > > dp(fr[0] + 1);
for(auto &a : dp) {
a.resize(fr[1] + 1);
for(auto &b : a) {
b.resize(fr[2] + 1);
for(auto &c : b) {
c.resize(3, INF);
}
}
}
for(i = 0; i < 3; i++) {
dp[0][0][0][i] = 0;
}
for(int p = 1; p <= n; p++) {
for(int a = 0; a <= p && a <= fr[0]; a++) {
for(int b = 0; a + b <= p && b <= fr[1]; b++) {
int c = p - a - b;
if(c > fr[2]) 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:96:19: warning: array subscript has type 'char' [-Wchar-subscripts]
pos[str[i]].push_back(i);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
420 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
9 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 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 |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
420 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
9 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
1156 KB |
Output is correct |
19 |
Correct |
4 ms |
1124 KB |
Output is correct |
20 |
Correct |
3 ms |
1144 KB |
Output is correct |
21 |
Correct |
4 ms |
1144 KB |
Output is correct |
22 |
Correct |
4 ms |
1144 KB |
Output is correct |
23 |
Correct |
4 ms |
1144 KB |
Output is correct |
24 |
Correct |
4 ms |
1144 KB |
Output is correct |
25 |
Correct |
3 ms |
760 KB |
Output is correct |
26 |
Correct |
3 ms |
760 KB |
Output is correct |
27 |
Correct |
4 ms |
1024 KB |
Output is correct |
28 |
Correct |
4 ms |
1144 KB |
Output is correct |
29 |
Correct |
4 ms |
1144 KB |
Output is correct |
30 |
Correct |
4 ms |
1144 KB |
Output is correct |
31 |
Correct |
4 ms |
1144 KB |
Output is correct |
32 |
Correct |
4 ms |
1144 KB |
Output is correct |
33 |
Correct |
3 ms |
760 KB |
Output is correct |
34 |
Correct |
3 ms |
888 KB |
Output is correct |
35 |
Correct |
4 ms |
1016 KB |
Output is correct |
36 |
Correct |
4 ms |
1144 KB |
Output is correct |
37 |
Correct |
3 ms |
1016 KB |
Output is correct |
38 |
Correct |
4 ms |
1144 KB |
Output is correct |
39 |
Correct |
5 ms |
1144 KB |
Output is correct |
40 |
Correct |
3 ms |
632 KB |
Output is correct |
41 |
Correct |
3 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
11 ms |
4600 KB |
Output is correct |
3 |
Correct |
11 ms |
4472 KB |
Output is correct |
4 |
Correct |
10 ms |
4472 KB |
Output is correct |
5 |
Correct |
11 ms |
4544 KB |
Output is correct |
6 |
Correct |
11 ms |
4472 KB |
Output is correct |
7 |
Correct |
11 ms |
4472 KB |
Output is correct |
8 |
Correct |
10 ms |
4472 KB |
Output is correct |
9 |
Correct |
14 ms |
4472 KB |
Output is correct |
10 |
Correct |
11 ms |
4472 KB |
Output is correct |
11 |
Correct |
11 ms |
4472 KB |
Output is correct |
12 |
Correct |
5 ms |
1912 KB |
Output is correct |
13 |
Correct |
6 ms |
2684 KB |
Output is correct |
14 |
Correct |
8 ms |
3448 KB |
Output is correct |
15 |
Correct |
11 ms |
4576 KB |
Output is correct |
16 |
Correct |
11 ms |
4532 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 |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
420 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
9 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
1156 KB |
Output is correct |
19 |
Correct |
4 ms |
1124 KB |
Output is correct |
20 |
Correct |
3 ms |
1144 KB |
Output is correct |
21 |
Correct |
4 ms |
1144 KB |
Output is correct |
22 |
Correct |
4 ms |
1144 KB |
Output is correct |
23 |
Correct |
4 ms |
1144 KB |
Output is correct |
24 |
Correct |
4 ms |
1144 KB |
Output is correct |
25 |
Correct |
3 ms |
760 KB |
Output is correct |
26 |
Correct |
3 ms |
760 KB |
Output is correct |
27 |
Correct |
4 ms |
1024 KB |
Output is correct |
28 |
Correct |
4 ms |
1144 KB |
Output is correct |
29 |
Correct |
4 ms |
1144 KB |
Output is correct |
30 |
Correct |
4 ms |
1144 KB |
Output is correct |
31 |
Correct |
4 ms |
1144 KB |
Output is correct |
32 |
Correct |
4 ms |
1144 KB |
Output is correct |
33 |
Correct |
3 ms |
760 KB |
Output is correct |
34 |
Correct |
3 ms |
888 KB |
Output is correct |
35 |
Correct |
4 ms |
1016 KB |
Output is correct |
36 |
Correct |
4 ms |
1144 KB |
Output is correct |
37 |
Correct |
3 ms |
1016 KB |
Output is correct |
38 |
Correct |
4 ms |
1144 KB |
Output is correct |
39 |
Correct |
5 ms |
1144 KB |
Output is correct |
40 |
Correct |
3 ms |
632 KB |
Output is correct |
41 |
Correct |
3 ms |
888 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
11 ms |
4600 KB |
Output is correct |
44 |
Correct |
11 ms |
4472 KB |
Output is correct |
45 |
Correct |
10 ms |
4472 KB |
Output is correct |
46 |
Correct |
11 ms |
4544 KB |
Output is correct |
47 |
Correct |
11 ms |
4472 KB |
Output is correct |
48 |
Correct |
11 ms |
4472 KB |
Output is correct |
49 |
Correct |
10 ms |
4472 KB |
Output is correct |
50 |
Correct |
14 ms |
4472 KB |
Output is correct |
51 |
Correct |
11 ms |
4472 KB |
Output is correct |
52 |
Correct |
11 ms |
4472 KB |
Output is correct |
53 |
Correct |
5 ms |
1912 KB |
Output is correct |
54 |
Correct |
6 ms |
2684 KB |
Output is correct |
55 |
Correct |
8 ms |
3448 KB |
Output is correct |
56 |
Correct |
11 ms |
4576 KB |
Output is correct |
57 |
Correct |
11 ms |
4532 KB |
Output is correct |
58 |
Execution timed out |
559 ms |
134520 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |