#include <bits/stdc++.h>
#define N 405
#define INF 2000000000
using namespace std;
int n,dp[N][N][N][4], rs[N], gs[N], ys[N];
string s;
int cost(int r, int g, int y, int i) {
return abs(rs[i]-r)+abs(gs[i]-g)+abs(ys[i]-y);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> s;
for(int i = 1; i <= n; i ++) {
rs[i] = rs[i-1]+(s[i-1]=='R');
gs[i] = gs[i-1]+(s[i-1]=='G');
ys[i] = ys[i-1]+(s[i-1]=='Y');
}
for(auto&i: dp) for(auto&j: i) for(auto&k: j) for(auto&l: k) l = INF;
dp[0][0][0][0] = 0;
for(int i = 0; i < n; i ++) {
for(int r = 0; r <= i && r <= rs[n]; r ++) {
for(int g = 0; r+g <= i && g <= gs[n]; g ++) {
int y = i-r-g;
if(y > ys[n]) continue;
for(int t = (i!=0); t <= 3; t ++) {
if(t != 1)
dp[i+1][r+1][g][1] = min(dp[i+1][r+1][g][1],dp[i][r][g][t]+cost(r+1,g,y,i+1));
if(t != 2)
dp[i+1][r][g+1][2] = min(dp[i+1][r][g+1][2],dp[i][r][g][t]+cost(r,g+1,y,i+1));
if(t != 3)
dp[i+1][r][g][3] = min(dp[i+1][r][g][3],dp[i][r][g][t]+cost(r,g,y+1,i+1));
}
}
}
}
// cout << dp[2][1][0][1] << "\n";
// cout << dp[3][1][0][3] << "\n";
// cout << dp[4][2][0][1] << "\n";
// cout << dp[5][2][0][3] << "\n";
// cout << dp[6][2][1][2] << "\n";
int res = min({dp[n][rs[n]][gs[n]][1],dp[n][rs[n]][gs[n]][2],dp[n][rs[n]][gs[n]][3]})/2;
assert(res%2==0);
if(res == INF/2) cout << -1;
else cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
367 ms |
1040416 KB |
Output is correct |
2 |
Correct |
389 ms |
1040248 KB |
Output is correct |
3 |
Correct |
356 ms |
1040280 KB |
Output is correct |
4 |
Correct |
454 ms |
1040212 KB |
Output is correct |
5 |
Correct |
360 ms |
1040276 KB |
Output is correct |
6 |
Runtime error |
393 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
367 ms |
1040416 KB |
Output is correct |
2 |
Correct |
389 ms |
1040248 KB |
Output is correct |
3 |
Correct |
356 ms |
1040280 KB |
Output is correct |
4 |
Correct |
454 ms |
1040212 KB |
Output is correct |
5 |
Correct |
360 ms |
1040276 KB |
Output is correct |
6 |
Runtime error |
393 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
433 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
367 ms |
1040416 KB |
Output is correct |
2 |
Correct |
389 ms |
1040248 KB |
Output is correct |
3 |
Correct |
356 ms |
1040280 KB |
Output is correct |
4 |
Correct |
454 ms |
1040212 KB |
Output is correct |
5 |
Correct |
360 ms |
1040276 KB |
Output is correct |
6 |
Runtime error |
393 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |