#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int maxn = 400;
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);
}
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(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(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(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]});
if (answer > n * n)
answer = -1;
else
answer /= 2;
cout << answer << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
711 ms |
751908 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
711 ms |
751908 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
395 ms |
751840 KB |
Output is correct |
2 |
Correct |
388 ms |
751732 KB |
Output is correct |
3 |
Correct |
416 ms |
751980 KB |
Output is correct |
4 |
Correct |
393 ms |
751864 KB |
Output is correct |
5 |
Correct |
399 ms |
751608 KB |
Output is correct |
6 |
Correct |
375 ms |
751736 KB |
Output is correct |
7 |
Correct |
381 ms |
751736 KB |
Output is correct |
8 |
Correct |
394 ms |
751880 KB |
Output is correct |
9 |
Correct |
382 ms |
751736 KB |
Output is correct |
10 |
Correct |
387 ms |
751864 KB |
Output is correct |
11 |
Correct |
380 ms |
751740 KB |
Output is correct |
12 |
Correct |
380 ms |
751844 KB |
Output is correct |
13 |
Correct |
374 ms |
751732 KB |
Output is correct |
14 |
Correct |
387 ms |
752048 KB |
Output is correct |
15 |
Correct |
380 ms |
751736 KB |
Output is correct |
16 |
Correct |
385 ms |
751864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
711 ms |
751908 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |