이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 407;
const int INF = 0x3f3f3f3f;
int dp[N][N][N][3];
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n;
string s;
cin >> n >> s;
vector<int> r, g, y;
for (int i = 0; i < n; ++i) {
if (s[i] == 'R')
r.push_back(i);
if (s[i] == 'G')
g.push_back(i);
if (s[i] == 'Y')
y.push_back(i);
}
int red = r.size(), green = g.size(), yellow = y.size();
memset(dp, 0x3f, sizeof(dp));
for (int i = 0; i < 3; ++i)
dp[0][0][0][i] = 0;
for (int i = 0; i <= red; ++i) {
for (int j = 0; j <= green; ++j) {
for (int k = 0; k <= yellow; ++k) {
for (int last = 0; last < 3; ++last) {
int pos = i + j + k;
int val = dp[i][j][k][last];
if (val == INF)
continue;
if (i < red && last != 0) dp[i + 1][j][k][0] = min(dp[i + 1][j][k][0], val + abs(pos - r[i]));
if (j < green && last != 1) dp[i][j + 1][k][1] = min(dp[i][j + 1][k][1], val + abs(pos - g[j]));
if (k < yellow && last != 2) dp[i][j][k + 1][2] = min(dp[i][j][k + 1][2], val + abs(pos - y[k]));
}
}
}
}
int ans = INF;
for (int i = 0; i < 3; ++i)
ans = min(ans, dp[red][green][yellow][i]);
if (ans == INF) {
cout << -1 << endl;
return 0;
}
cout << ans / 2 << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |