Submission #1347290

#TimeUsernameProblemLanguageResultExecution timeMemory
1347290penguin133Scarecrows 2 (JOI26_scarecrows)C++20
100 / 100
948 ms5644 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int n, k;
vector <pair <int, pair <int, int> > > r, c;

int32_t main () {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        int t, x, y, a;
        cin >> t >> x >> y >> a;
        if (t <= 2) {
            r.push_back({x, {3 - t, a}});
        }
        else {
            c.push_back({y, {3 - (t - 2), a}});
        }
    }
    sort(r.begin(), r.end());
    sort(c.begin(), c.end());
    ll lo = 0, hi = (ll)2e14, ans = hi + 1;
    while (lo <= hi) {
        ll mid = (lo + hi) >> 1;
        //penalty of mid for making new guy
        priority_queue <ll, vector <ll>, greater <ll> > op;
        priority_queue <ll> cl;
        ll cc = 0, us = 0;
        for (auto i : r) {
            if (i.second.first == 1) {
                op.push(i.second.second);
            }
            else {
                ll res = (op.empty() ? 1e18 : op.top());
                ll cs2 = i.second.second - (cl.empty() ? -1e18 : cl.top());
                if (res + i.second.second - mid < 0 && res + i.second.second - mid <= cs2) {
                    cc += res + i.second.second - mid;
                    us++;
                    op.pop();
                    cl.push(i.second.second);
                }
                else if (!cl.empty() && i.second.second < cl.top()) {
                    cc += i.second.second - cl.top();
                    cl.pop();
                    cl.push(i.second.second);
                }
            }
        }
        while (!op.empty()) op.pop();
        while (!cl.empty()) cl.pop();
        for (auto i : c) {
            if (i.second.first == 1) {
                op.push(i.second.second);
            }
            else {
                ll res = (op.empty() ? 1e18 : op.top());
                ll cs2 = i.second.second - (cl.empty() ? -1e18 : cl.top());
                if (res + i.second.second - mid < 0 && res + i.second.second - mid <= cs2) {
                    cc += res + i.second.second - mid;
                    us++;
                    op.pop();
                    cl.push(i.second.second);
                }
                else if (!cl.empty() && i.second.second < cl.top()) {
                    cc += i.second.second - cl.top();
                    cl.pop();
                    cl.push(i.second.second);
                }
            }
        }
        if (us >= k) ans = mid, hi = mid - 1;
        else lo = mid + 1;
    }
    if (ans > (ll)2e14) {
        cout << -1 << '\n';
        return 0;
    }
    priority_queue <ll, vector <ll>, greater <ll> > op;
    priority_queue <ll> cl;
    ll cc = 0, us = 0;
    for (auto i : r) {
        if (i.second.first == 1) {
            op.push(i.second.second);
        }
        else {
            ll res = (op.empty() ? 1e18 : op.top());
            ll cs2 = i.second.second - (cl.empty() ? -1e18 : cl.top());
            if (res + i.second.second - ans <= cs2 && res + i.second.second - ans < 0) {
                cc += res + i.second.second - ans;
                us++;
                op.pop();
                cl.push(i.second.second);
            }
            else if (!cl.empty() && i.second.second < cl.top()) {
                cc += i.second.second - cl.top();
                cl.pop();
                cl.push(i.second.second);
            }
        }
    }
    while (!op.empty()) op.pop();
    while (!cl.empty()) cl.pop();
    for (auto i : c) {
        if (i.second.first == 1) {
            op.push(i.second.second);
        }
        else {
            ll res = (op.empty() ? 1e18 : op.top());
            ll cs2 = i.second.second - (cl.empty() ? (ll)-1e18 : cl.top());
            if (res + i.second.second - ans < 0 && res + i.second.second - ans <= cs2) {
                cc += res + i.second.second - ans;
                us++;
                op.pop();
                cl.push(i.second.second);
            }
            else if (!cl.empty() && i.second.second < cl.top()) {
                cc += i.second.second - cl.top();
                cl.pop();
                cl.push(i.second.second);
            }
        }
    }
    //cout << ans << '\n';
    //assert(us == k);
    cout << cc + k * 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...