#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int maxn = 200 + 10;
const int inf = 1e9;
int dp[maxn][maxn][maxn][3];
int main(){
ios_base::sync_with_stdio(false);
int n;
string s;
cin >> n >> s;
vector<int> G, R, Y;
for (int i = 0; i < n; i++){
if (s[i] == 'G')
G.push_back(i);
else if (s[i] == 'R')
R.push_back(i);
else
Y.push_back(i);
}
if ((int)G.size() < (int)R.size())
swap(G, R);
if ((int)G.size() < (int)Y.size())
swap(G, R);
if ((int)R.size() < (int)Y.size())
swap(R, Y);
if ((int)G.size() * 2 - 1 > n)
return cout << -1 << endl, 0;
for (int g = 0; g <= (int)G.size(); g++){
for (int r = 0; r <= (int)R.size(); r++){
for (int y = 0; y <= (int)Y.size(); y++){
if (g + r + y == 0){
dp[g][r][y][0] = dp[g][r][y][1] = dp[g][r][y][2] = 0;
continue;
}
if (2 * max({g, r, y}) - 1 > g + r + y)
continue;
int now = g + r + y - 1;
if (g > 0){
int me = G[g - 1];
dp[g][r][y][0] = min(inf, min(dp[g-1][r][y][1], dp[g-1][r][y][2]) + abs(now-me));
}
if (r > 0){
int me = R[r - 1];
dp[g][r][y][1] = min(inf, min(dp[g][r-1][y][0], dp[g][r-1][y][2]) + abs(now-me));
}
if (y > 0){
int me = Y[y - 1];
dp[g][r][y][2] = min(inf, min(dp[g][r][y-1][0], dp[g][r][y-1][1]) + abs(now-me));
}
}
}
}
int g = (int)G.size(), r = (int)R.size(), y = (int)Y.size();
int answer = min({dp[g][r][y][0], dp[g][r][y][1], dp[g][r][y][2]});
answer /= 2;
cout << answer << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Incorrect |
5 ms |
408 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Incorrect |
5 ms |
408 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Incorrect |
5 ms |
408 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |