#include<bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
const int N = 201;
int f[N][N][N][3];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
string s; cin >> s;
vector<int> pos[3];
vector<int> ord[3];
for(int i = 0 ; i < n ; ++i) {
if (s[i] == 'R') pos[0].push_back(i + 1);
if (s[i] == 'G') pos[1].push_back(i + 1);
if (s[i] == 'Y') pos[2].push_back(i + 1);
}
if (sz(pos[0]) > (n + 1) / 2) return 0 * puts("-1");
if (sz(pos[1]) > (n + 1) / 2) return 0 * puts("-1");
if (sz(pos[2]) > (n + 1) / 2) return 0 * puts("-1");
memset(f,0x3f,sizeof f);
if (sz(pos[0])) f[1][0][0][0] = pos[0][0] - 1;
if (sz(pos[1])) f[0][1][0][1] = pos[1][0] - 1;
if (sz(pos[2])) f[0][0][1][2] = pos[2][0] - 1;
for(int i = 0 ; i < 3 ; ++i) {
for(int j = 0 ; j <= n ; ++j)
ord[i].push_back(upper_bound(pos[i].begin(),pos[i].end(),j) - pos[i].begin());
pos[i].insert(pos[i].begin(),0);
}
for(int a = 0 ; a < sz(pos[0]) ; ++a)
for(int b = 0 ; b < sz(pos[1]) ; ++b)
for(int c = 0 ; c < sz(pos[2]) ; ++c) {
int P = a + b + c;
if (P < 2) continue;
for(int x = 0 ; x < 3 ; ++x) {
int na = a - (x == 0);
int nb = b - (x == 1);
int nc = c - (x == 2);
if (na < 0) continue;
if (nb < 0) continue;
if (nc < 0) continue;
int real_pos;
if (x == 0) real_pos = pos[0][a] + max(0,b - ord[1][pos[0][a]]) + max(0,c - ord[2][pos[0][a]]);
if (x == 1) real_pos = pos[1][b] + max(0,a - ord[0][pos[1][b]]) + max(0,c - ord[2][pos[1][b]]);
if (x == 2) real_pos = pos[2][c] + max(0,a - ord[0][pos[2][c]]) + max(0,b - ord[1][pos[2][c]]);
f[a][b][c][x] = min(f[na][nb][nc][0 + (x == 0)],f[na][nb][nc][2 - (x == 2)]) + abs(real_pos - P);
}
}
int ans = 0x3f;
ans = min(ans,f[pos[0].size() - 1][pos[1].size() - 1][pos[2].size() - 1][0]);
ans = min(ans,f[pos[0].size() - 1][pos[1].size() - 1][pos[2].size() - 1][1]);
ans = min(ans,f[pos[0].size() - 1][pos[1].size() - 1][pos[2].size() - 1][2]);
cout << ans << endl;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:65:105: warning: 'real_pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
f[a][b][c][x] = min(f[na][nb][nc][0 + (x == 0)],f[na][nb][nc][2 - (x == 2)]) + abs(real_pos - P);
~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
95736 KB |
Output is correct |
2 |
Correct |
85 ms |
95736 KB |
Output is correct |
3 |
Correct |
84 ms |
95736 KB |
Output is correct |
4 |
Correct |
88 ms |
95736 KB |
Output is correct |
5 |
Correct |
84 ms |
95608 KB |
Output is correct |
6 |
Correct |
85 ms |
95688 KB |
Output is correct |
7 |
Correct |
85 ms |
95836 KB |
Output is correct |
8 |
Correct |
84 ms |
95640 KB |
Output is correct |
9 |
Correct |
88 ms |
95624 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
89 ms |
95776 KB |
Output is correct |
12 |
Correct |
87 ms |
95608 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
87 ms |
95672 KB |
Output is correct |
15 |
Correct |
86 ms |
95736 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
95736 KB |
Output is correct |
2 |
Correct |
85 ms |
95736 KB |
Output is correct |
3 |
Correct |
84 ms |
95736 KB |
Output is correct |
4 |
Correct |
88 ms |
95736 KB |
Output is correct |
5 |
Correct |
84 ms |
95608 KB |
Output is correct |
6 |
Correct |
85 ms |
95688 KB |
Output is correct |
7 |
Correct |
85 ms |
95836 KB |
Output is correct |
8 |
Correct |
84 ms |
95640 KB |
Output is correct |
9 |
Correct |
88 ms |
95624 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
89 ms |
95776 KB |
Output is correct |
12 |
Correct |
87 ms |
95608 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
87 ms |
95672 KB |
Output is correct |
15 |
Correct |
86 ms |
95736 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
86 ms |
95856 KB |
Output is correct |
19 |
Correct |
86 ms |
95608 KB |
Output is correct |
20 |
Correct |
86 ms |
95652 KB |
Output is correct |
21 |
Correct |
97 ms |
95864 KB |
Output is correct |
22 |
Correct |
84 ms |
95736 KB |
Output is correct |
23 |
Correct |
89 ms |
95668 KB |
Output is correct |
24 |
Correct |
81 ms |
95736 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Incorrect |
87 ms |
95608 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
95684 KB |
Output is correct |
2 |
Incorrect |
91 ms |
95724 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
95736 KB |
Output is correct |
2 |
Correct |
85 ms |
95736 KB |
Output is correct |
3 |
Correct |
84 ms |
95736 KB |
Output is correct |
4 |
Correct |
88 ms |
95736 KB |
Output is correct |
5 |
Correct |
84 ms |
95608 KB |
Output is correct |
6 |
Correct |
85 ms |
95688 KB |
Output is correct |
7 |
Correct |
85 ms |
95836 KB |
Output is correct |
8 |
Correct |
84 ms |
95640 KB |
Output is correct |
9 |
Correct |
88 ms |
95624 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
89 ms |
95776 KB |
Output is correct |
12 |
Correct |
87 ms |
95608 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
87 ms |
95672 KB |
Output is correct |
15 |
Correct |
86 ms |
95736 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
86 ms |
95856 KB |
Output is correct |
19 |
Correct |
86 ms |
95608 KB |
Output is correct |
20 |
Correct |
86 ms |
95652 KB |
Output is correct |
21 |
Correct |
97 ms |
95864 KB |
Output is correct |
22 |
Correct |
84 ms |
95736 KB |
Output is correct |
23 |
Correct |
89 ms |
95668 KB |
Output is correct |
24 |
Correct |
81 ms |
95736 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Incorrect |
87 ms |
95608 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |