//#pragma GCC optimize("O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 61;
int dp[N][N][4][2];
int cnt[3];
int n;
void init(int q) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int t = 0; t < 4; t++) {
dp[i][j][t][q] = INT32_MAX;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
map<char, int> mp;
mp['R'] = 0;
mp['G'] = 1;
mp['Y'] = 2;
string s;
cin >> s;
vector<int> pos[3];
for (int i = 0; i < n; i++) {
cnt[mp[s[i]]]++;
pos[mp[s[i]]].push_back(i);
}
for (int i = 0; i < 3; i++) {
pos[i].push_back(-1);
reverse(pos[i].begin(), pos[i].end());
}
init(0);
dp[cnt[0]][cnt[1]][3][0] = 0;
for (int i = 0; i < n; i++) {
int q = i & 1;
int w = q ^ 1;
init(w);
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n - i - j; k++) {
for (int t = 0; t < 4; t++) {
if (dp[j][k][t][q] == INT32_MAX) {
continue;
}
if (j > 0 && t != 0) {
dp[j - 1][k][0][w] = min(dp[j - 1][k][0][w], dp[j][k][t][q] + abs(pos[0][j] - i));
}
if (k > 0 && t != 1) {
dp[j][k - 1][1][w] = min(dp[j][k - 1][1][w], dp[j][k][t][q] + abs(pos[1][k] - i));
}
if (n - i - j - k > 0 && t != 2) {
dp[j][k][2][w] = min(dp[j][k][2][w], dp[j][k][t][q] + abs(pos[2][n - i - j - k] - i));
}
}
}
}
}
int ans = INT32_MAX;
for (int i = 0; i < 4; i++) {
ans = min(ans, dp[0][0][i][n & 1]);
}
if (ans == INT32_MAX) {
cout << -1;
} else {
cout << ans / 2;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Runtime error |
3 ms |
760 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |