Submission #680283

#TimeUsernameProblemLanguageResultExecution timeMemory
680283mjhmjh1104Tortoise (CEOI21_tortoise)C++17
48 / 100
3085 ms224540 KiB
#include <map>
#include <tuple>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int n, a[500006], dist[500006], res;
vector<int> playgrounds;
int pv[500006], nx[500006];

map<tuple<int, int, int, int>, int> mem;

int f(int x, int y, int z, int w) {
    if (y > 2 * n - 2) return 0;
    if (mem.find({ x, y, z, w }) != mem.end()) return mem[{ x, y, z, w }];
    if (w && a[x] == -1) return f(x, y, z, 0);
    int res = 0;
    if (w && a[x] != -1) res = max(res, f(x + 1, y - 1, min(n, max(a[x + 1], 0)), 1));
    if (x < n - 1) res = max(res, f(x + 1, y + 1, min(n, max(a[x + 1], 0)), 0));
    if (pv[x] != -1 && z > 0 && y <= 2 * x) {
        int dist = x - pv[x];
        res = max(res, f(x, y + 2 * dist, z - 1, 0) + 1);
    }
    if (nx[x] != -1 && z > 0 && y <= 2 * x) {
        int dist = nx[x] - x;
        res = max(res, f(x, y + 2 * dist, z - 1, 1) + 1);
    }
    return mem[{ x, y, z, w }] = res;
}

int main() {
    scanf("%d", &n);
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", a + i);
        sum += max(a[i], 0);
        dist[i] = (int)1e9;
        if (a[i] < 0) playgrounds.push_back(i);
    }
    for (int i = 0; i < n; i++) {
        int it = lower_bound(playgrounds.begin(), playgrounds.end(), i) - playgrounds.begin();
        if (it == (int)playgrounds.size()) nx[i] = -1;
        else nx[i] = playgrounds[it];
        it = upper_bound(playgrounds.begin(), playgrounds.end(), i) - playgrounds.begin() - 1;
        if (it == -1) pv[i] = -1;
        else pv[i] = playgrounds[it];
    }
    {
        int tmp = (int)1e9;
        for (int i = 0; i < n; i++) {
            if (a[i] == -1) tmp = 0;
            dist[i] = min(dist[i], tmp);
            tmp++;
        }
        tmp = (int)1e9;
        for (int i = n - 1; i >= 0; i--) {
            if (a[i] == -1) tmp = 0;
            dist[i] = min(dist[i], tmp);
            tmp++;
        }
    }
    printf("%lld", sum - f(0, 0, min(n, max(a[0], 0)), 0));
}

Compilation message (stderr)

tortoise.cpp: In function 'int main()':
tortoise.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
tortoise.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         scanf("%d", a + i);
      |         ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...