Submission #1042330

#TimeUsernameProblemLanguageResultExecution timeMemory
1042330VMaksimoski008Catfish Farm (IOI22_fish)C++17
100 / 100
693 ms227120 KiB
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
    for(int i=0; i<M; i++) X[i]++, Y[i]++;
    map<int, int> mp[N+5];
    for(int i=0; i<M; i++) mp[X[i]][Y[i]] = W[i];
    ll ans = 0;

    vector<pair<int, int> > vec[N+5];
    vector<int> pos[N+5], SZ(N+5);
    for(int i=1; i<=N; i++) vec[i].push_back({ 0, 0 });
    for(int i=0; i<M; i++) vec[X[i]].push_back({ Y[i], W[i] });

    for(int i=1; i<=N; i++) {
        set<int> st;
        for(int j=-1; j<=1; j++) for(auto &x : vec[i-j]) st.insert(x.first);
        for(auto &x : st) pos[i].push_back(x);
        sort(vec[i].begin(), vec[i].end());
    }

    vector<vector<ll> > pref(N+5), pmx1(N+5), smx1(N+5), pmx2(N+5);
    vector<vector<vector<ll> > > dp(N+5);
    for(int i=N; i>=1; i--) {
        SZ[i] = pos[i].size() - 1;
        pref[i].resize(SZ[i] + 10);
        pmx1[i].resize(SZ[i] + 10);
        smx1[i].resize(SZ[i] + 10);
        pmx2[i].resize(SZ[i] + 10);
        dp[i].resize(SZ[i] + 5, vector<ll>(2));
        
        if(i == 1) {
            pmx1[i][0] = dp[i][0][1] - pref[i][0];
            for(int j=1; j<=SZ[i]; j++) pmx1[i][j] = max(pmx1[i][j-1], dp[i][j][1] - pref[i][j]);

            for(int j=SZ[i]; j>=0; j--) {
                int p = upper_bound(pos[i+1].begin(), pos[i+1].end(), pos[i][j]) - pos[i+1].begin() - 1;
                smx1[i][j] = pref[i+1][p];
            }

            for(int j=SZ[i]-1; j>=0; j--) smx1[i][j] = max(smx1[i][j], smx1[i][j+1]);
        }

        for(int j=1; j<=SZ[i]; j++) pref[i][j] = pref[i][j-1] + mp[i][pos[i][j]];
    }

    for(int i=2; i<=N; i++) {
        int ptr = 0, ptr2 = 0;
        for(int j=0; j<=SZ[i]; j++) {
            int p = upper_bound(pos[i-1].begin(), pos[i-1].end(), pos[i][j]) - pos[i-1].begin() - 1;
            dp[i][j][1] = max(dp[i][j][1], pmx1[i-1][p] + pref[i-1][p]);
            while(ptr + 1 <= SZ[i-1] && pos[i-1][ptr] < pos[i][j]) ptr++;
            if(pos[i-1][ptr] >= pos[i][j]) dp[i][j][0] = max(dp[i][j][0], smx1[i-1][ptr] - pref[i][j]);

            if(i < 3) continue;
            int pa = upper_bound(pos[i-2].begin(), pos[i-2].end(), pos[i][j]) - pos[i-2].begin() - 1;
            int pb = upper_bound(pos[i-1].begin(), pos[i-1].end(), pos[i][j]) - pos[i-1].begin() - 1;
            dp[i][j][0] = max(dp[i][j][0], pmx2[i-2][pa] + pref[i-1][pb]);
            
            while(ptr2 + 1 <= SZ[i-2] && pos[i-2][ptr2] < pos[i][j]) ptr2++;
            if(pos[i-2][ptr2] >= pos[i][j]) dp[i][j][1] = max(dp[i][j][1], smx1[i-2][ptr2]);
        }

        pmx1[i][0] = dp[i][0][1] - pref[i][0];
        pmx2[i][0] = max(dp[i][0][0], dp[i][0][1]);
        for(int j=1; j<=SZ[i]; j++) {
            pmx1[i][j] = max(pmx1[i][j-1], dp[i][j][1] - pref[i][j]);
            pmx2[i][j] = max(pmx2[i][j-1], max(dp[i][j][0], dp[i][j][1]));
        }

        if(i == N) continue;
        for(int j=SZ[i]; j>=0; j--) {
            int p = upper_bound(pos[i+1].begin(), pos[i+1].end(), pos[i][j]) - pos[i+1].begin() - 1;
            smx1[i][j] = max(dp[i][j][0], dp[i][j][1]) + pref[i+1][p];
        }

        for(int j=SZ[i]-1; j>=0; j--) smx1[i][j] = max(smx1[i][j], smx1[i][j+1]);
    }

    for(int i=1; i<=N; i++) {
        for(int j=0; j<=SZ[i]; j++) {
            if(pref[i+1].empty() || j == 0) ans = max({ ans, dp[i][j][0], dp[i][j][1] });
            else {
                int p = upper_bound(pos[i+1].begin(), pos[i+1].end(), pos[i][j]) - pos[i+1].begin() - 1;
                ans = max(ans, max(dp[i][j][0], dp[i][j][1]) + pref[i+1][p]);
            }
        }
    }
    return ans;
}
#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...