#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
using namespace std;
const int N = 403;
const int inf = int(1e9) + 7;
int c[N][3], pos[N][3], f[N][N][N][3];
int n, step, p;
string s;
void Irene(int r, int g, int y, int cur, int col) {
if(col == 0) {
p = pos[r + 1][col];
step = max(c[p][1] - g, 0) + max(c[p][2] - y, 0);
f[r + 1][g][y][col] = min(f[r + 1][g][y][col], f[r][g][y][cur] + step);
} else if(col == 1) {
p = pos[g + 1][col];
step = max(c[p][0] - r, 0) + max(c[p][2] - y, 0);
f[r][g + 1][y][col] = min(f[r][g + 1][y][col], f[r][g][y][cur] + step);
} else {
p = pos[y + 1][col];
step = max(c[p][0] - r, 0) + max(c[p][1] - g, 0);
f[r][g][y + 1][col] = min(f[r][g][y + 1][col], f[r][g][y][cur] + step);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if(fopen("test.inp", "r")) {
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
cin >> n >> s;
s = ' ' + s;
for(int i = 1; i <= n; ++i) {
c[i][0] = c[i - 1][0], c[i][1] = c[i - 1][1], c[i][2] = c[i - 1][2];
if(s[i] == 'R') pos[++c[i][0]][0] = i;
else if(s[i] == 'G') pos[++c[i][1]][1] = i;
else pos[++c[i][2]][2] = i;
}
fill_n(&f[0][0][0][0], N * N * N * 3, inf);
f[0][0][0][0] = f[0][0][0][1] = f[0][0][0][2] = 0;
int res = inf, R = c[n][0], G = c[n][1], Y = c[n][2];
for(int i = 0; i <= R; ++i) {
for(int j = 0; j <= G; ++j) {
for(int k = 0; k <= Y; ++k) {
if(i < R) Irene(i, j, k, 1, 0), Irene(i, j, k, 2, 0);
if(j < G) Irene(i, j, k, 0, 1), Irene(i, j, k, 2, 1);
if(k < Y) Irene(i, j, k, 0, 2), Irene(i, j, k, 1, 2);
}
}
}
for(int i = 0; i < 3; ++i) res = min(res, f[R][G][Y][i]);
if(res == inf) return cout << -1, 0;
cout << res;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("test.inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("test.out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
593 ms |
768824 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
593 ms |
768824 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
595 ms |
768944 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
593 ms |
768824 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |