제출 #1244318

#제출 시각아이디문제언어결과실행 시간메모리
1244318steveonalex나일강 (IOI24_nile)C++20
100 / 100
1071 ms12720 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned int ul;
typedef unsigned long long ull;

#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()

ll gcd(ll a, ll b){return __gcd(a, b);}
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}

ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}

// mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}

template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }

template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }

template <class T>
    void printArr(T the_array_itself, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: the_array_itself) out << item << separator;
        out << finish;
    }

template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

#include "nile.h"

const int BLOCK = 330;

vector<long long> calculate_costs(
    vector<int> W, vector<int> A,
    vector<int> B, vector<int> E){
    int n = W.size(), q = E.size();

    vector<pair<int, int>> val;
    ll sum = 0;
    for(int i = 0; i < n; ++i){
        val.push_back(make_pair(W[i], A[i] - B[i]));
        sum += A[i];
    }

    while(n % BLOCK){
        n++;
        val.push_back(make_pair(2e9 + 69, 0));
    }

    sort(ALL(val));

    vector<array<int, 3>> edges;
    for(int i = 0; i < n; ++i){
        for(int j = 1; j < min(3, i + 1); ++j){
            edges.push_back({{val[i].first - val[i - j].first, i, j}});
        }
    }

    sort(ALL(edges));
    reverse(ALL(edges));

    vector<pair<int, int>> queries;
    vector<ll> ans(q);
    for(int i = 0; i < q; ++i) queries.push_back(make_pair(E[i], i));
    sort(ALL(queries));
    
    int back_edge[n][3]; memset(back_edge, 0, sizeof back_edge);

    ll dp[n][3]; memset(dp, 0, sizeof dp);

    for(pair<int,int> cur: queries){
        int idx = cur.second, d = cur.first;
        while(edges.size() && edges.back()[0] <= d){
            array<int, 3> edge = edges.back(); edges.pop_back();

            int i = edge[1], j = edge[2];
            back_edge[i][j] = val[i].second + val[i - j].second;

            int b_i = i / BLOCK;
            int l = b_i * BLOCK, r = min(l + BLOCK - 1, n-1);
            for(int step = 0; step < 3; ++step){
                for(int i = l; i <= r; ++i){
                    dp[i][step] = 0;
                    for(int j = 0; j < 3; ++j){
                        int p = i - j - 1;
                        ll val = 0;
                        if (p >= l) val = dp[p][step];
                        if (p < l && p != l - step - 1) continue;
                        maximize(dp[i][step], val + back_edge[i][j]);
                    }
                }
            }
        }

        ll T[3]; memset(T, 0, sizeof T);
        for(int b_i = 1; b_i <= n / BLOCK; ++b_i){
            int r = b_i * BLOCK - 1;
            minimize(r, n-1);

            ll tmp[3]; memset(tmp, 0, sizeof tmp);
            for(int x = 0; x < 3; ++x) for(int y = 0; y < 3; ++y){
                maximize(tmp[2-y], T[2-x] + dp[r - y][x]);
            }
            for(int x = 0; x < 3; ++x) T[x] = tmp[x];
        }

        ans[idx] = sum - *max_element(T, T + 3);
    }

    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...