# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
97299 | RezwanArefin01 | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++17 | 1251 ms | 809548 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int N = 410;
int n, c[3], dp[N][3][N][N], cnt[3], f[3][N];
char s[N];
vector<int> pos[3];
int main(int argc, char const *argv[]) {
scanf("%d", &n);
scanf(" %s", s + 1);
for(int i = 1; i <= n; ++i) {
int c = s[i] == 'R' ? 0 : s[i] == 'G' ? 1 : 2;
pos[c].push_back(i);
++cnt[c];
}
sort(cnt, cnt + 3);
sort(pos, pos+3, [](vector<int> &a, vector<int> &b) { return a.size() < b.size(); });
for(int i = 0; i <= 2; ++i) {
for(int x : pos[i]) f[i][x] = 1;
for(int j = n - 1; j >= 0; --j)
f[i][j] += f[i][j + 1];
}
memset(dp, 63, sizeof dp);
memset(dp[1], 0, sizeof dp[1]);
for(int i = 1; i <= n; ++i) {
for(int p = 0; p <= 2; ++p) {
for(c[0] = 0; c[0] < i && c[0] <= cnt[0]; ++c[0]) {
for(c[1] = 0; c[1] < i - c[0] && c[1] <= cnt[1]; ++c[1]) {
c[2] = i - c[0] - c[1] - 1;
if(c[2] > cnt[2]) continue;
for(int q = 0; q <= 2; ++q) if(p != q && c[q] != cnt[q]) {
int idx = pos[q][c[q]];
idx += max(0, c[p] - cnt[p] + f[p][idx]) +
max(0, c[p^q^3] - cnt[p^q^3] + f[p^q^3][idx]);
int &x = dp[i + 1][q][q == 0 ? c[0] + 1 : c[0]][q == 1 ? c[1] + 1 : c[1]];
x = min(x, dp[i][p][c[0]][c[1]] + abs(idx - i));
}
}
}
}
}
int ans = 1e9;
for(int i = 0; i <= 2; i++) ans = min(ans, dp[n + 1][i][cnt[0]][cnt[1]]);
if(ans >= 1e9) ans = -1;
printf("%d\n", ans);
}
Compilation message (stderr)
# | 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... |