Submission #615263

#TimeUsernameProblemLanguageResultExecution timeMemory
615263cheissmart휴가 (IOI14_holiday)C++14
24 / 100
5096 ms22752 KiB
#include"holiday.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 1e5 + 7;


ll go(int n, int s, int d, int a[]) {
    priority_queue<int, vi, greater<int>> pq;
    ll ans = 0, sum = 0;
    int cost = 0;
    for(int i = s; i < n; i++) {
        pq.push(a[i]);
        sum += a[i];
        while(SZ(pq) + cost > d) {
            if(pq.empty()) return ans;
            sum -= pq.top();
            pq.pop();
        }
        ans = max(ans, sum);
        cost++;
    }
    return ans;
}

struct DS {
    vi a;
    V<pi> stk;
    int c, n;

    V<vi> cnt;
    V<V<ll>> sum;
    void add(int pos, int val) {
        for(; pos <= n; pos += pos & -pos)
            cnt[pos].PB(val);
    }
    int qry_cnt(int pos, int val) { // <= val
        int res = 0;
        for(; pos; pos -= pos & -pos) {
            res += upper_bound(ALL(cnt[pos]), val) - cnt[pos].begin();
        }
        return res;
    }
    ll qry_sum(int pos, int val) { // <= val
        ll res = 0;
        for(; pos; pos -= pos & -pos) {
            int tt = upper_bound(ALL(cnt[pos]), val) - cnt[pos].begin();
            if(tt)
                res += sum[pos][tt - 1];
        }
        return res;
    }
    ll qry(int pos, int need) {
        int lb = -INF, rb = 0;
        while(lb <= rb) {
            int mb = (lb + rb) / 2;
            if(qry_cnt(pos, mb) >= need)
                rb = mb - 1;
            else
                lb = mb + 1;
        }
        return qry_sum(pos, lb) - 1LL * (qry_cnt(pos, lb) - need) * lb;
    }

    ll test(int d, int i) {
        if(i * c > d) return -1;
        d -= i * c;
        d = min(d, i + 1);
        return -qry(i + 1, d);
    }
    DS(vi _a, int _c) {
        a = _a;
        c = _c;
        n = SZ(a);
        cnt.resize(n + 1);
        sum.resize(n + 1);
        for(int i = 1; i <= n; i++)
            add(i, -a[i - 1]);

        for(int i = 1; i <= n; i++) {
            sort(ALL(cnt[i]));
            sum[i] = V<ll>(ALL(cnt[i]));
            for(int j = 1; j < SZ(sum[i]); j++)
                sum[i][j] += sum[i][j - 1];
        }

        for(int i = 0; i < n; i++) {
            while(SZ(stk) && test(stk.back().F, stk.back().S) <= test(stk.back().F, i))
                stk.pop_back();
            if(stk.empty())
                stk.EB(i * c, i);
            else {
                int lb = stk.back().F + 1, rb = INF;
                // find first x s.t. test(x, stk.back().S) <= test(x, i)
                while(lb <= rb) {
                    int mb = (lb + rb) / 2;
                    if(test(mb, stk.back().S) <= test(mb, i))
                        rb = mb - 1;
                    else
                        lb = mb + 1;
                }
                stk.EB(lb, i);
            }
        }
    }
    ll qry(int d) {
        int it = upper_bound(ALL(stk), pi(d, INF)) - stk.begin() - 1;
        return test(d, stk[it].S);
    }
};

ll solve(int n, int s, int d, int a[]) { // go left in the first step
    if(s == 0) return 0;
    vi pl, pr;
    for(int i = s - 1; i >= 0; i--) pl.PB(a[i]);
    for(int i = s; i < n; i++) pr.PB(a[i]);
    DS dsl = DS(pl, 2);
    DS dsr = DS(pr, 1);
    ll ans = 0;
    for(int i = 0; i + 2 <= d; i++) {
        ans = max(ans, dsl.qry(i) + dsr.qry(d - i - 2));
    }
    return ans;
}

ll findMaxAttraction(int n, int s, int d, int a[]) {
    ll ans = go(n, s, d, a);

    ans = max(ans, solve(n, s, d, a));

    reverse(a, a + n);
    s = n - s - 1;

    ans = max(ans, go(n, s, d, a));
    ans = max(ans, solve(n, s, d, a));

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