QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#425185#7523. Partially Free MealpandapythonerTL 0ms3712kbC++202.6kb2024-05-29 23:45:222024-05-29 23:45:22

Judging History

This is the latest submission verdict.

  • [2024-05-29 23:45:22]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 3712kb
  • [2024-05-29 23:45:22]
  • Submitted

answer

// #pragma GCC optimize("Ofast,unroll-loops,fast-math")
// #pragma GCC target("avx,avx2")


#include <bits/stdc++.h>


using namespace std;


#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()


const ll inf = 1e18;
mt19937 rnd(234);
const ll mod = 1e9 + 7;
typedef complex<flt> base;
const flt pi = atan2(1, 0) * 2;


int n;
vector<pair<ll, ll>> a;

struct aboba {
    int i = -1;
    multiset<ll> left, right;
    ll lsm = 0;


    ll get(int ni, int k) {
        while (i < ni) {
            ++i;
            ll x = a[i].first;
            if (left.empty() or x > *prev(left.end())) {
                right.insert(x);
            } else {
                left.insert(x);
                lsm += x;
            }
        }
        while (i > ni) {
            ll x = a[i].first;
            if (left.empty() or x > *prev(left.end())) {
                right.erase(right.find(x));
            } else {
                left.erase(left.find(x));
                lsm -= x;
            }
            --i;
        }
        assert(i == ni);
        if ((int)left.size() + (int)right.size() < k) {
            return inf;
        }
        while ((int)left.size() < k) {
            auto it = right.begin();
            lsm += *it;
            left.insert(*it);
            right.erase(it);
        }
        while ((int)left.size() > k) {
            auto it = prev(left.end());
            lsm -= *it;
            right.insert(*it);
            left.erase(it);
        }
        assert((int)left.size() == k);
        return lsm + a[i].second;
    }
};

aboba biba;
vector<ll> rs;


void solve_rec(int lk, int rk, int li, int ri) {
    if (lk > rk) {
        return;
    }
    int mk = (lk + rk) / 2;
    ll opti = -1;
    ll val = inf;
    for (int i = li; i <= ri; i += 1) {
        ll x = biba.get(i, mk + 1);
        if (x < val) {
            val = x;
            opti = i;
        }
    }
    rs[mk] = val;
    solve_rec(lk, mk - 1, li, opti);
    solve_rec(mk + 1, rk, opti, ri);
}


void solve() {
    biba = aboba();
    sort(all(a), [&](auto x, auto y) {return x.second < y.second;});
    rs.resize(n);
    solve_rec(0, n - 1, 0, n - 1);
}

int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }
    cin >> n;
    a.resize(n);
    for (int i = 0; i < n; i += 1) {
        cin >> a[i].first >> a[i].second;
    }
    solve();
    for (int i = 0; i < n; i += 1) {
        cout << rs[i] << "\n";
    }
    return 0;
}


詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3712kb

input:

3
2 5
4 3
3 7

output:

7
11
16

result:

ok 3 lines

Test #2:

score: -100
Time Limit Exceeded

input:

200000
466436993 804989151
660995237 756645598
432103296 703610564
6889895 53276988
873617076 822481192
532911431 126844295
623111499 456772252
937464699 762157133
708503076 786039753
78556972 5436013
582960979 398984169
786333369 325119902
930705057 615928139
924915828 506145001
164984329 208212435...

output:


result: