# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
444205 | nonsensenonsense1 | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++17 | 497 ms | 757620 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 <cstdio>
#include <algorithm>
#include <cstring>
const int INF = 0x3f3f3f3f;
const int N = 401;
int n, amt[3], pos[3][N], pr[3][N], d[N][N][N][3];
char s[N];
int f(int last)
{
int &cur = d[amt[0]][amt[1]][amt[2]][last];
if (cur == -1) {
cur = INF;
if (!(amt[0] + amt[1] + amt[2])) cur = 0;
else for (int i = 0; i < 3; ++i) if (i != last && amt[i]) {
--amt[i];
int inc = 0;
for (int j = 0; j < 3; ++j) if (j != i) inc += std::max(0, pr[j][pos[i][amt[i]]] - amt[j]);
cur = std::min(cur, f(i) + inc);
++amt[i];
}
}
return cur;
}
int main()
{
memset(***d, -1, N * N * N * 3 << 2);
scanf("%d%s", &n, s);
for (int i = 0; i < n; ++i) {
int cur;
if (s[i] == 'R') cur = 0;
else if (s[i] == 'G') cur = 1;
else cur = 2;
pos[cur][amt[cur]++] = i;
for (int j = 0; j < 3; ++j) {
pr[j][i] += cur == j;
if (i) pr[j][i] += pr[j][i - 1];
}
}
int ans = INF;
for (int i = 0; i < 3; ++i) ans = std::min(ans, f(i));
if (ans == INF) printf("-1\n");
else printf("%d\n", ans);
return 0;
}
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... |