# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
444181 | nonsensenonsense1 | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++17 | 357 ms | 757460 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], 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 if (2 * std::max(amt[0], std::max(amt[1], amt[2])) <= amt[0] + amt[1] + amt[2] + 1) for (int i = 0; i < 3; ++i) if (i != last && amt[i]) {
--amt[i];
cur = std::min(cur, f(i) + abs(amt[0] + amt[1] + amt[2] - pos[i][amt[i]]));
++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;
}
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 >> 1);
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... |