#include<bits/stdc++.h>
using namespace std;
const int N = 401;
const int INF = 1e9 + 7;
vector <char> al = {'R', 'G', 'Y'};
int dp1[N][N][3], dp2[N][N][3];
int cnt[3];
vector <int> pos[3];
char s[N];
void mm(int &a, int b) {
a = min(a, b);
}
int get(char c) {
for (int i = 0; i < 3; ++i) {
if (al[i] == c) {
return i;
}
}
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
++cnt[get(s[i])];
pos[get(s[i])].push_back(i);
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
for (int t = 0; t < 3; ++t) {
dp1[i][j][t] = INF;
}
}
}
for (int i = 0; i < 3; ++i) {
dp1[0][0][i] = 0;
}
for (int i = 0; i <= cnt[0]; ++i) {
for (int j = 0; j <= cnt[1]; ++j) {
for (int k = 0; k <= cnt[2]; ++k) {
for (int pr = 0; pr < 3; ++pr) {
dp2[j][k][pr] = INF;
}
}
}
for (int j = 0; j <= cnt[1]; ++j) {
for (int k = 0; k <= cnt[2]; ++k) {
for (int pr = 0; pr < 3; ++pr) {
//cout << i << ' ' << j << ' ' << k << ' ' << pr << ' ' << dp1[j][k][pr] << '\n';
int p = i + j + k + 1;
if (i < cnt[0] && pr != 0) {
mm(dp2[j][k][0], dp1[j][k][pr] + abs(p - pos[0][i]));
}
if (j < cnt[1] && pr != 1) {
mm(dp1[j + 1][k][1], dp1[j][k][pr] + abs(p - pos[1][j]));
}
if (k < cnt[2] && pr != 2) {
mm(dp1[j][k + 1][2], dp1[j][k][pr] + abs(p - pos[2][k]));
}
}
}
}
if (i == cnt[0]) break;
for (int j = 0; j <= cnt[1]; ++j) {
for (int k = 0; k <= cnt[2]; ++k) {
for (int pr = 0; pr < 3; ++pr) {
dp1[j][k][pr] = dp2[j][k][pr];
}
}
}
}
int ans = INF;
for (int i = 0; i < 3; ++i) {
ans = min(ans, dp1[cnt[1]][cnt[2]][i]);
}
if (ans == INF) {
cout << "-1\n";
}
else {
cout << ans / 2 << '\n';
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'int get(char)':
joi2019_ho_t3.cpp:19:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2296 KB |
Output is correct |
2 |
Correct |
3 ms |
2296 KB |
Output is correct |
3 |
Correct |
4 ms |
2296 KB |
Output is correct |
4 |
Correct |
4 ms |
2296 KB |
Output is correct |
5 |
Correct |
4 ms |
2296 KB |
Output is correct |
6 |
Correct |
3 ms |
2168 KB |
Output is correct |
7 |
Correct |
4 ms |
2296 KB |
Output is correct |
8 |
Correct |
3 ms |
2296 KB |
Output is correct |
9 |
Correct |
4 ms |
2296 KB |
Output is correct |
10 |
Correct |
3 ms |
2296 KB |
Output is correct |
11 |
Incorrect |
4 ms |
2296 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2296 KB |
Output is correct |
2 |
Correct |
3 ms |
2296 KB |
Output is correct |
3 |
Correct |
4 ms |
2296 KB |
Output is correct |
4 |
Correct |
4 ms |
2296 KB |
Output is correct |
5 |
Correct |
4 ms |
2296 KB |
Output is correct |
6 |
Correct |
3 ms |
2168 KB |
Output is correct |
7 |
Correct |
4 ms |
2296 KB |
Output is correct |
8 |
Correct |
3 ms |
2296 KB |
Output is correct |
9 |
Correct |
4 ms |
2296 KB |
Output is correct |
10 |
Correct |
3 ms |
2296 KB |
Output is correct |
11 |
Incorrect |
4 ms |
2296 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2296 KB |
Output is correct |
2 |
Correct |
5 ms |
3064 KB |
Output is correct |
3 |
Correct |
5 ms |
3064 KB |
Output is correct |
4 |
Correct |
5 ms |
3064 KB |
Output is correct |
5 |
Correct |
5 ms |
3092 KB |
Output is correct |
6 |
Correct |
5 ms |
3064 KB |
Output is correct |
7 |
Correct |
5 ms |
3064 KB |
Output is correct |
8 |
Correct |
5 ms |
3064 KB |
Output is correct |
9 |
Correct |
5 ms |
3064 KB |
Output is correct |
10 |
Correct |
5 ms |
3064 KB |
Output is correct |
11 |
Correct |
5 ms |
3064 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2936 KB |
Output is correct |
15 |
Correct |
5 ms |
3064 KB |
Output is correct |
16 |
Correct |
5 ms |
3064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2296 KB |
Output is correct |
2 |
Correct |
3 ms |
2296 KB |
Output is correct |
3 |
Correct |
4 ms |
2296 KB |
Output is correct |
4 |
Correct |
4 ms |
2296 KB |
Output is correct |
5 |
Correct |
4 ms |
2296 KB |
Output is correct |
6 |
Correct |
3 ms |
2168 KB |
Output is correct |
7 |
Correct |
4 ms |
2296 KB |
Output is correct |
8 |
Correct |
3 ms |
2296 KB |
Output is correct |
9 |
Correct |
4 ms |
2296 KB |
Output is correct |
10 |
Correct |
3 ms |
2296 KB |
Output is correct |
11 |
Incorrect |
4 ms |
2296 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |