#include<bits/stdc++.h>
using namespace std;
const int N = 401;
const int INF = 1e9 + 7;
vector <char> al = {'R', 'G', 'Y'};
int dp[N][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 k = 0; k < N; ++k) {
for (int t = 0; t < 3; ++t) {
dp[i][j][k][t] = INF;
}
}
}
}
for (int i = 0; i < 3; ++i) {
dp[0][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) {
int p = i + j + k + 1;
if (i < cnt[0] && pr != 0) {
mm(dp[i + 1][j][k][0], dp[i][j][k][pr] + abs(p - pos[0][i]));
}
if (j < cnt[1] && pr != 1) {
mm(dp[i][j + 1][k][1], dp[i][j][k][pr] + abs(p - pos[1][j]));
}
if (k < cnt[2] && pr != 2) {
mm(dp[i][j][k + 1][2], dp[i][j][k][pr] + abs(p - pos[2][k]));
}
}
}
}
}
int ans = INF;
for (int i = 0; i < 3; ++i) {
ans = min(ans, dp[cnt[0]][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 |
Execution timed out |
774 ms |
757612 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
774 ms |
757612 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
635 ms |
757624 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
774 ms |
757612 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |