이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 401;
int count_r[N], count_g[N], count_y[N];
int where_r[N], where_g[N], where_y[N];
int swaps[N][N][N][3];
void set_min(int &a, int b) {
a = min(a, b);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
string s;
cin >> n >> s;
for (int i = 0; i < n; ++i) {
count_r[i + 1] = count_r[i] + (s[i] == 'R');
count_g[i + 1] = count_g[i] + (s[i] == 'G');
count_y[i + 1] = count_y[i] + (s[i] == 'Y');
if (s[i] == 'R') {
where_r[count_r[i]] = i;
} else if (s[i] == 'G') {
where_g[count_g[i]] = i;
} else {
where_y[count_y[i]] = i;
}
}
for (int r = 0; r <= n; ++r) {
for (int g = 0; g <= n; ++g) {
for (int y = 0; y <= n; ++y) {
for (int i = 0; i < 3; ++i) {
if (r + g + y > 0) {
swaps[r][g][y][i] = INT_MAX;
}
}
}
}
}
for (int r = 0; r <= n; ++r) {
for (int g = 0; g <= n; ++g) {
for (int y = 0; y <= n; ++y) {
for (int i = 0; i < 3; ++i) {
if (swaps[r][g][y][i] < INT_MAX) {
if (i != 0 && r < count_r[n]) {
set_min(swaps[r + 1][g][y][0], swaps[r][g][y][i] +
max(0, count_g[where_r[r]] - g) +
max(0, count_y[where_r[r]] - y)
);
}
if (i != 1 && g < count_g[n]) {
set_min(swaps[r][g + 1][y][1], swaps[r][g][y][i] +
max(0, count_r[where_g[g]] - r) +
max(0, count_y[where_g[g]] - y)
);
}
if (i != 2 && y < count_y[n]) {
set_min(swaps[r][g][y + 1][2], swaps[r][g][y][i] +
max(0, count_r[where_y[y]] - r) +
max(0, count_g[where_y[y]] - g)
);
}
}
}
}
}
}
int ans = INT_MAX;
for (int i = 0; i < 3; ++i) {
set_min(ans, swaps[count_r[n]][count_g[n]][count_y[n]][i]);
}
cout << (ans < INT_MAX ? ans : -1) << "\n";
}
# | 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... |