제출 #625735

#제출 시각아이디문제언어결과실행 시간메모리
625735phathnv메기 농장 (IOI22_fish)C++17
100 / 100
277 ms24624 KiB
#include "fish.h"

#include <bits/stdc++.h>
using namespace std;

long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
    vector<vector<int>> a(n + 1);
    vector<vector<array<int, 2>>> b(n + 1);
    vector<array<long long, 2>> dp[2], nDp[2];

    for (int i = 0; i < m; ++i) {
        b[x[i]].push_back({y[i], w[i]});
        if (0 <= x[i] - 1) {
            a[x[i] - 1].push_back(y[i]);
        }
        if (x[i] + 1 < n) {
            a[x[i] + 1].push_back(y[i]);
        }
    }
    for (int i = 0; i <= n; ++i) {
        a[i].push_back(-1);
        sort(a[i].begin(), a[i].end());
        sort(b[i].begin(), b[i].end());
        a[i].resize(unique(a[i].begin(), a[i].end()) - a[i].begin());
    }

    dp[0].push_back({-1, 0});
    dp[1].push_back({-1, 0});
    vector<array<int, 2>> weights;
    for (int i = 0; i <= n; ++i) {
        nDp[0].clear();
        nDp[1].clear();
        for (auto pos : a[i]) {
            nDp[0].push_back({pos, 0});
            nDp[1].push_back({pos, 0});
        }

        long long sumW1, sumW2;
        long long best;
        int ptrW1, ptrW2, ptrDp;

        // 0
        sumW1 = 0;
        sumW2 = 0;
        best = -1e18;
        ptrW1 = 0;
        ptrW2 = 0;
        ptrDp = 0;
        for (auto &[h, val] : nDp[0]) {
            while (ptrDp < (int)dp[0].size() && h >= dp[0][ptrDp][0]) {
                while (ptrW1 < (int)weights.size() && dp[0][ptrDp][0] >= weights[ptrW1][0]) {
                    sumW1 += weights[ptrW1][1];
                    ++ptrW1;
                }
                best = max(best, dp[0][ptrDp][1] - sumW1);
                ++ptrDp;
            }
            while (ptrW2 < (int)weights.size() && h >= weights[ptrW2][0]) {
                sumW2 += weights[ptrW2][1];
                ++ptrW2;
            }
            val = best + sumW2;
        }


        // 1
        weights = b[i];
        sumW1 = 0;
        sumW2 = 0;
        best = -1e18;
        ptrW1 = weights.size() - 1;
        ptrW2 = weights.size() - 1;
        ptrDp = dp[1].size() - 1;
        for (auto [h, w] : weights) {
            sumW1 += w;
            sumW2 += w;
        }

        for (int j = nDp[1].size() - 1; j >= 0; --j) {
            auto &[h, val] = nDp[1][j];
            while (ptrDp >= 0 && h <= dp[1][ptrDp][0]) {
                while (ptrW1 >= 0 && dp[1][ptrDp][0] < weights[ptrW1][0]) {
                    sumW1 -= weights[ptrW1][1];
                    --ptrW1;
                }
                best = max(best, dp[1][ptrDp][1] + sumW1);
                --ptrDp;
            }
            while (ptrW2 >= 0 && h < weights[ptrW2][0]) {
                sumW2 -= weights[ptrW2][1];
                --ptrW2;
            }
            val = best - sumW2;
        }

        // 1->0
        for (int j = 0; j < (int) nDp[0].size(); ++j) {
            nDp[0][j][1] = max(nDp[0][j][1], dp[1][0][1]);
        }

        // 0->1
        for (int j = 0; j < (int) nDp[0].size(); ++j) {
            nDp[1][j][1] = max(nDp[1][j][1], nDp[0][j][1]);
        }

        swap(dp[0], nDp[0]);
        swap(dp[1], nDp[1]);
    }

    return dp[1][0][1];
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...