#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, Y);
if ((int)R.size() < (int)Y.size())
swap(R, Y);
if ((int)G.size() * 2 - 1 > n)
return cout << -1 << endl, 0;
memset(dp, 63, sizeof dp);
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 |
70 ms |
109048 KB |
Output is correct |
2 |
Correct |
64 ms |
109048 KB |
Output is correct |
3 |
Correct |
64 ms |
109176 KB |
Output is correct |
4 |
Correct |
63 ms |
109048 KB |
Output is correct |
5 |
Correct |
64 ms |
109048 KB |
Output is correct |
6 |
Correct |
65 ms |
109176 KB |
Output is correct |
7 |
Correct |
63 ms |
109048 KB |
Output is correct |
8 |
Correct |
64 ms |
109048 KB |
Output is correct |
9 |
Correct |
68 ms |
109048 KB |
Output is correct |
10 |
Correct |
4 ms |
376 KB |
Output is correct |
11 |
Incorrect |
63 ms |
109048 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
109048 KB |
Output is correct |
2 |
Correct |
64 ms |
109048 KB |
Output is correct |
3 |
Correct |
64 ms |
109176 KB |
Output is correct |
4 |
Correct |
63 ms |
109048 KB |
Output is correct |
5 |
Correct |
64 ms |
109048 KB |
Output is correct |
6 |
Correct |
65 ms |
109176 KB |
Output is correct |
7 |
Correct |
63 ms |
109048 KB |
Output is correct |
8 |
Correct |
64 ms |
109048 KB |
Output is correct |
9 |
Correct |
68 ms |
109048 KB |
Output is correct |
10 |
Correct |
4 ms |
376 KB |
Output is correct |
11 |
Incorrect |
63 ms |
109048 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
109048 KB |
Output is correct |
2 |
Correct |
65 ms |
108920 KB |
Output is correct |
3 |
Correct |
65 ms |
109048 KB |
Output is correct |
4 |
Correct |
66 ms |
109048 KB |
Output is correct |
5 |
Correct |
65 ms |
109048 KB |
Output is correct |
6 |
Correct |
64 ms |
109048 KB |
Output is correct |
7 |
Correct |
63 ms |
109048 KB |
Output is correct |
8 |
Correct |
64 ms |
109044 KB |
Output is correct |
9 |
Correct |
67 ms |
109048 KB |
Output is correct |
10 |
Correct |
65 ms |
109048 KB |
Output is correct |
11 |
Correct |
64 ms |
109048 KB |
Output is correct |
12 |
Correct |
65 ms |
109048 KB |
Output is correct |
13 |
Correct |
63 ms |
109048 KB |
Output is correct |
14 |
Correct |
64 ms |
109048 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
109048 KB |
Output is correct |
2 |
Correct |
64 ms |
109048 KB |
Output is correct |
3 |
Correct |
64 ms |
109176 KB |
Output is correct |
4 |
Correct |
63 ms |
109048 KB |
Output is correct |
5 |
Correct |
64 ms |
109048 KB |
Output is correct |
6 |
Correct |
65 ms |
109176 KB |
Output is correct |
7 |
Correct |
63 ms |
109048 KB |
Output is correct |
8 |
Correct |
64 ms |
109048 KB |
Output is correct |
9 |
Correct |
68 ms |
109048 KB |
Output is correct |
10 |
Correct |
4 ms |
376 KB |
Output is correct |
11 |
Incorrect |
63 ms |
109048 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |