QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#225716#7523. Partially Free Mealammardab3anTL 1ms3592kbC++142.8kb2023-10-25 01:42:522023-10-25 01:42:53

Judging History

你现在查看的是最新测评结果

  • [2023-10-25 01:42:53]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3592kb
  • [2023-10-25 01:42:52]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll ans[200010];

void solve(const vector<pair<int, int>> &v, int l, int r, int tl, int tr, multiset<pair<int, int>> &f, multiset<pair<int, int>> &s, set<int> &idx, ll sum){
    if(tl > tr) return;
    int mid = (tl+tr)/2;
    pair<ll, ll> best(1e18, -1);
    while(f.size() >= mid){
        auto [x, y] = *(--f.end());
        f.erase(--f.end());
        s.insert({x, y});
        sum -= x;
    }
    while(f.size() < mid-1 && !s.empty()){
        auto [x, y] = *(s.begin());
        s.erase(s.begin());
        f.insert({x, y});
        sum += x;
    }
    for(int i = l; i <= r; i++){
        if(f.size() == mid-1) best = min(best, {sum + v[i].second + v[i].first, i});
        if(i == r) break;
        if(idx.count(i) == 0){
            s.insert({v[i].first, i});
            idx.insert(i);
        }
        while(f.size() >= mid){
            auto [x, y] = *(--f.end());
            f.erase(--f.end());
            s.insert({x, y});
            sum -= x;
        }
        while(f.size() < mid-1 && !s.empty()){
            auto [x, y] = *(s.begin());
            s.erase(s.begin());
            f.insert({x, y});
            sum += x;
        }
        while(!f.empty() && !s.empty()){
            auto [x1, y1] = *(--f.end());
            auto [x2, y2] = *s.begin();
            if(x1 <= x2) break;
            f.erase(--f.end());
            s.erase(s.begin());
            s.insert({x1, y1});
            f.insert({x2, y2});
            sum -= x1;
            sum += x2;
        }
    }
    ans[mid] = best.first;
    while(!idx.empty() && *(--idx.end()) > best.second-1){
        int x = *(--idx.end());
        idx.erase(--idx.end());
        if(f.count({v[x].first, x})){
            f.erase({v[x].first, x});
            sum -= v[x].first;
        }
        if(s.count({v[x].first, x})){
            s.erase({v[x].first, x});
        }
    }
    solve(v, best.second, r, mid+1, tr, f, s, idx, sum);
    while(!idx.empty() && *(--idx.end()) >= l){
        int x = *(--idx.end());
        idx.erase(--idx.end());
        if(f.count({v[x].first, x})){
            f.erase({v[x].first, x});
            sum -= v[x].first;
        }
        if(s.count({v[x].first, x})){
            s.erase({v[x].first, x});
        }
    }
    sum = 0;
    solve(v, l, best.second, tl, mid-1, f, s, idx, sum);
}

int main()
{

    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n;
    cin >> n;

    vector<pair<int, int>> v(n);

    for(auto &[f, s] : v) cin >> s >> f;

    sort(v.begin(), v.end());

    for(auto &[f, s] : v) swap(f, s);

    multiset<pair<int, int>> f, s;
    set<int> idx;
    solve(v, 0, n-1, 1, n, f, s, idx, 0);

    for(int i = 1; i <= n; i++){
        cout << ans[i] << "\n";
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3592kb

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: