//#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 = 401;
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] + max(0, 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] + max(0, 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] + max(0, 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;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5368 KB |
Output is correct |
2 |
Correct |
7 ms |
5368 KB |
Output is correct |
3 |
Correct |
7 ms |
5368 KB |
Output is correct |
4 |
Correct |
10 ms |
5368 KB |
Output is correct |
5 |
Correct |
12 ms |
5368 KB |
Output is correct |
6 |
Correct |
12 ms |
5368 KB |
Output is correct |
7 |
Correct |
11 ms |
5372 KB |
Output is correct |
8 |
Correct |
11 ms |
5368 KB |
Output is correct |
9 |
Correct |
11 ms |
5368 KB |
Output is correct |
10 |
Correct |
11 ms |
5368 KB |
Output is correct |
11 |
Incorrect |
11 ms |
5368 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5368 KB |
Output is correct |
2 |
Correct |
7 ms |
5368 KB |
Output is correct |
3 |
Correct |
7 ms |
5368 KB |
Output is correct |
4 |
Correct |
10 ms |
5368 KB |
Output is correct |
5 |
Correct |
12 ms |
5368 KB |
Output is correct |
6 |
Correct |
12 ms |
5368 KB |
Output is correct |
7 |
Correct |
11 ms |
5372 KB |
Output is correct |
8 |
Correct |
11 ms |
5368 KB |
Output is correct |
9 |
Correct |
11 ms |
5368 KB |
Output is correct |
10 |
Correct |
11 ms |
5368 KB |
Output is correct |
11 |
Incorrect |
11 ms |
5368 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5368 KB |
Output is correct |
2 |
Correct |
265 ms |
5420 KB |
Output is correct |
3 |
Correct |
240 ms |
5496 KB |
Output is correct |
4 |
Correct |
243 ms |
5500 KB |
Output is correct |
5 |
Correct |
251 ms |
5424 KB |
Output is correct |
6 |
Correct |
242 ms |
5424 KB |
Output is correct |
7 |
Correct |
246 ms |
5368 KB |
Output is correct |
8 |
Correct |
250 ms |
5496 KB |
Output is correct |
9 |
Correct |
244 ms |
5496 KB |
Output is correct |
10 |
Correct |
242 ms |
5496 KB |
Output is correct |
11 |
Correct |
243 ms |
5368 KB |
Output is correct |
12 |
Correct |
94 ms |
5368 KB |
Output is correct |
13 |
Correct |
135 ms |
5416 KB |
Output is correct |
14 |
Correct |
177 ms |
5496 KB |
Output is correct |
15 |
Correct |
241 ms |
5368 KB |
Output is correct |
16 |
Correct |
241 ms |
5240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5368 KB |
Output is correct |
2 |
Correct |
7 ms |
5368 KB |
Output is correct |
3 |
Correct |
7 ms |
5368 KB |
Output is correct |
4 |
Correct |
10 ms |
5368 KB |
Output is correct |
5 |
Correct |
12 ms |
5368 KB |
Output is correct |
6 |
Correct |
12 ms |
5368 KB |
Output is correct |
7 |
Correct |
11 ms |
5372 KB |
Output is correct |
8 |
Correct |
11 ms |
5368 KB |
Output is correct |
9 |
Correct |
11 ms |
5368 KB |
Output is correct |
10 |
Correct |
11 ms |
5368 KB |
Output is correct |
11 |
Incorrect |
11 ms |
5368 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |