Submission #1301460

#TimeUsernameProblemLanguageResultExecution timeMemory
1301460ProtonDecay314Aliens (IOI16_aliens)C++20
0 / 100
1 ms428 KiB
#include "aliens.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<bool> vb;

#define INF(dt) numeric_limits<dt>::max()
#define fi first
#define se second

// Create a struct for linear functions
// First argument: slope, second argument: y-intercept
struct Line {
    ll m, b;

    int b2;

    pair<ll, int> evaluate(ll x) const {
        return {m * x + b, b2};
    }
};

/*
Maintain the minimum line in the li chao tree
*/
class Tree {
    public:
        Tree *lt, *rt;
        int l, r;
        Line v;

        Tree(int a_l, int a_r): lt(nullptr), rt(nullptr), l(a_l), r(a_r), v({0ll, INF(ll), INF(int)}) {}

        void build() {
            if(l == r) {
                return;
            }

            int m = (l + r) >> 1;

            lt = new Tree(l, m);
            rt = new Tree(m + 1, r);

            lt->build();
            rt->build();
        }

        void rst() {
            v = {0ll, INF(ll), INF(int)};
            if(l == r) {
                return;
            }

            lt->rst();
            rt->rst();
        }

        void insert_line(Line nl) {
            int m = (l + r) >> 1;
            bool less_l = nl.evaluate(l) < v.evaluate(l);
            bool less_m = nl.evaluate(m + 1) < v.evaluate(m + 1);

            /*
            ! TODO why does swap work
            ! and v = nl does not???

            well, if we do v = nl,
            we simply erase v from existence
            we don't want that---we want to consider both

            so nl is considered now
            therefore we instead propagate v downward
            */
            if(less_m) swap(v, nl);

            if(l == r) {
                return;
            } else if(less_l != less_m) {
                lt->insert_line(nl);
            } else {
                rt->insert_line(nl);
            }
        }

        pair<ll, int> evaluate(int x) {
            if(l == r) {
                return v.evaluate(x);
            }
            int m = (l + r) >> 1;

            if(x <= m) {
                return min(lt->evaluate(x), v.evaluate(x));
            } else {
                return min(rt->evaluate(x), v.evaluate(x));
            }
        }

        ~Tree() {
            if(lt != nullptr) {
                delete lt;
                delete rt;
            }
        }
};

Tree * tr;
/*
Suppose the penalty for using another picture is +y
*/
pll solve_lambda_brute(const vpll& pts, ll m, ll y) {
    ll np = pts.size();

    // resetting the li chao tree
    tr->rst();
    
    // note: stores NEGATIVE area, num pictures
    // then, the DP just gets the maximum state
    vector<pair<ll, int>> dp(np, {0ll, 0});

    
    for(ll i = 0ll; i < np; i++) {
        ll cl = pts[i].fi, cr = pts[i].se;

        if(i == 0ll) {
            tr->insert_line({- (pts[i].fi << 1ll), y + pts[i].fi * pts[i].fi, -1});
        } else {
            ll overlap = max(pts[i - 1ll].se - pts[i].fi + 1ll, 0ll);
            tr->insert_line({- (pts[i].fi << 1ll), dp[i - 1ll].fi - overlap * overlap + y + pts[i].fi * pts[i].fi, dp[i - 1ll].se - 1});
        }
        
        pair<ll, int> opt = tr->evaluate(cr + 1ll);
        
        dp[i] = {opt.fi + (cr + 1ll) * (cr + 1ll), opt.se};
        
        // cerr << y << " " << i << " " << dp[i].fi << " " << -dp[i].se << endl;
    }
    
    return dp[np - 1ll];
}

ll take_photos(int i_n, int i_m, int i_k, vi r, vi c) {
    ll n = i_n, m = i_m, k = i_k;
    
    tr = new Tree(0, i_m - 1);
    tr->build();
    // first, we preprocess. remove all redundant points
    
    vpll pts;
    
    for(ll i = 0ll; i < n; i++) {
        if(r[i] > c[i]) swap(r[i], c[i]);

        pts.push_back({r[i], c[i]});
    }

    sort(pts.begin(), pts.end(), [](pll p1, pll p2){return p1.fi < p2.fi || (p1.fi == p2.fi && p1.se > p2.se);});

    ll max_r = -1ll;

    vpll filt_pts;

    for(ll i = 0ll; i < n; i++) {
        ll cl = pts[i].fi, cr = pts[i].se;

        if(cr > max_r) {
            filt_pts.push_back(pts[i]);
            // cerr << cl << " " << cr << endl;
            max_r = cr;
        }
    }

    // Performing lagrangian relaxation
    ll lo = 0ll, hi = 1e12;

    while(hi - lo > 1ll) {
        ll mid = (lo + hi) >> 1ll;

        pll cpair = solve_lambda_brute(filt_pts, m, mid);

        ll c_k = -cpair.se;

        if(c_k >= k) lo = mid;
        else hi = mid;
    }

    // deleting for future invocations
    delete tr;

    return solve_lambda_brute(filt_pts, m, lo).fi - k * lo;
}

Compilation message (stderr)

aliens.h:1:9: warning: #pragma once in main file
    1 | #pragma once
      |         ^~~~
aliens_c.h:1:9: warning: #pragma once in main file
    1 | #pragma once
      |         ^~~~
#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...