UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#215276#2639. 省钱N906068ms6492kbC++115.1kb2024-11-27 22:54:332024-11-27 23:40:14

answer

//
//  na 2639.cpp
//  Competitive Programming
//
//  Created by m2 on 2024/11/27.
//

#include <stdio.h>
#include <iostream>
#include <random>
#include <set>
typedef long long int ll;
const ll inf = 0x1fffffffffffffff;
using namespace std;
const int maxn = 5e4 + 5;
int n, k, w[maxn], q[maxn], d[maxn], qs;
ll S, cost;
template<typename T> inline void rd(T &v){
    v = 0; char r = getchar();
    while(r - 48 >= 10u)
        r = getchar();
    while(r - 48 < 10u){
        v = (v << 3) + (v << 1) + (r ^ 48);
        r = getchar();
    }
}
template<typename T> void pd(T v){
    if(v < 10)
        putchar(int(v) ^ 48);
    else{
        pd(v / 10);
        putchar(int(v % 10) ^ 48);
    }
}
struct CmpD{
    inline bool operator()(const int &a, const int &b) const{
        return d[a] == d[b]? a < b: d[a] < d[b];
    }
};
struct CmpW{
    inline bool operator()(const int &a, const int &b) const{
        return w[a] == w[b]? a < b: w[a] < w[b];
    }
};
struct CmpQ{
    inline bool operator()(const int &a, const int &b) const{
        return q[a] == q[b]? a < b: q[a] < q[b];
    }
};
inline bool check(int lim){
    set<int, CmpD> qd;
    set<int, CmpQ> qq;
    set<int, CmpW> ww;
    set<int, CmpD> wd;
    cost = qs = 0;
    ll c1, c2, c3, c4;
    int t1 = 0, t2 = 0;
    for(int i = 1; i <= n; ++i){
        if(qd.size() + ww.size() < lim){
            if(qs < k){
                qd.insert(i);
                qq.insert(i);
                cost += q[i];
                ++qs;
            }else{
                c1 = qd.empty()? inf: d[*qd.begin()] + q[i];
                if(w[i] <= c1){
                    ww.insert(i);
                    wd.insert(i);
                    cost += w[i];
                }else{
                    cost += c1;
                    ww.insert(*qd.begin());
                    wd.insert(*qd.begin());
                    qq.erase(*qd.begin());
                    qd.erase(qd.begin());
                    qd.insert(i);
                    qq.insert(i);
                }
            }
        }else{
            c1 = qq.empty()? inf: q[i] - q[*qq.rbegin()];
            c2 = ww.empty()? inf: w[i] - w[*ww.rbegin()];
            c3 = (wd.empty() || qq.empty())? inf: -d[*wd.rbegin()] + w[i] - q[*qq.rbegin()];
            c4 = (wd.empty() || qq.empty())? inf: d[*qd.begin()] - w[*wd.rbegin()] + q[i];
            if(c1 < 0 && c1 < c2 && c1 <= c3 && c1 <= c4){
                qd.erase(*qq.rbegin());
                qq.erase(*qq.rbegin());
                qd.insert(i);
                qq.insert(i);
                cost += c1;
            }else if(c2 < 0 && c2 <= c3 && c2 <= c4){
                wd.erase(*ww.rbegin());
                ww.erase(*ww.rbegin());
                wd.insert(i);
                ww.insert(i);
                cost += c2;
            }else if(c3 < 0 && c3 <= c4){
                qd.erase(*qq.rbegin());
                qq.erase(*qq.rbegin());
                qd.insert(*wd.rbegin());
                qq.insert(*wd.rbegin());
                ww.erase(*wd.rbegin());
                wd.erase(*wd.rbegin());
                ww.insert(i);
                wd.insert(i);
                cost += c3;
            }else if(c4 < 0){
                ww.erase(*wd.rbegin());
                wd.erase(*wd.rbegin());
                ww.insert(*qd.begin());
                wd.insert(*qd.begin());
                qq.erase(*qd.begin());
                qd.erase(qd.begin());
                qq.insert(i);
                qd.insert(i);
                cost += c4;
            }
        }
        while(!qq.empty() && !ww.empty() && d[t1 = *qd.rbegin()] < d[t2 = *wd.rbegin()]){
            qd.erase(t1);
            qq.erase(t1);
            qd.insert(t2);
            qq.insert(t2);
            wd.erase(t2);
            ww.erase(t2);
            wd.insert(t1);
            ww.insert(t1);
            cost += d[t1] - d[t2];
        }
        if(qd.size() + ww.size() == lim && cost <= S)
            return true;
    }
    return false;
}
int main(){
    cin.tie(0)->sync_with_stdio(false);
    rd(n); rd(k); rd(S);
    if(n != 0){
        for(int i = 1; i <= n; ++i){
            rd(w[i]); rd(q[i]);
            d[i] = w[i] - q[i];
        }
    }else{
        n = 5e4;
        random_device rd;
        mt19937 rnd(rd());
        uniform_int_distribution<> dis(1, 500);
        for(int i = 1; i <= n; ++i){
            q[i] = dis(rnd);
            d[i] = dis(rnd);
            w[i] = q[i] + d[i];
        }
    }
    int lt = 0, rt = n + 1, mid;
    while(lt < rt - 1){
        mid = (lt + rt) >> 1;
        if(check(mid))
            lt = mid;
        else
            rt = mid;
    }
    cout << lt << endl;
//    for(int i = n / 2; i; --i){
//        swap(q[i], q[n - i + 1]);
//        swap(d[i], d[n - i + 1]);
//        swap(w[i], w[n - i + 1]);
//    }
//    lt = 0; rt = n + 1;
//    while(lt < rt - 1){
//        mid = (lt + rt) >> 1;
//        if(check(mid))
//            lt = mid;
//        else
//            rt = mid;
//    }
//    cout << lt << endl;
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 484ms
memory: 4180kb

input:

50000 30828 852557364841
682084050 257603011
870868024 517458094
732267860 201407488
777566656 55879...

output:

17903

result:

ok single line: '17903'

Test #2:

score: 10
Accepted
time: 821ms
memory: 5940kb

input:

50000 10508 8982273367520
34111224 12372852
549875017 525549262
357107918 219952140
644308048 222008...

output:

37621

result:

ok single line: '37621'

Test #3:

score: 10
Accepted
time: 455ms
memory: 4184kb

input:

50000 23114 535861686266
359271294 298114231
605400720 491693949
755566780 539381575
155586610 92962...

output:

14723

result:

ok single line: '14723'

Test #4:

score: 10
Accepted
time: 837ms
memory: 5356kb

input:

50000 13490 4616703243118
286358449 133228996
162995754 17235506
661390160 561824344
282751480 15433...

output:

30961

result:

ok single line: '30961'

Test #5:

score: 0
Wrong Answer
time: 822ms
memory: 5944kb

input:

50000 26352 8630976119100
70133466 32927792
90392510 89764542
307782646 75889114
123168574 66039130
...

output:

42942

result:

wrong answer 1st lines differ - expected: '42944', found: '42942'

Test #6:

score: 10
Accepted
time: 387ms
memory: 4180kb

input:

50000 11800 213255455323
405512104 311547645
122797690 35257030
782246460 533338866
416860264 504733...

output:

9869

result:

ok single line: '9869'

Test #7:

score: 10
Accepted
time: 803ms
memory: 5356kb

input:

50000 19734 4267681411347
732638120 327229436
361949068 274173372
539440696 285784669
94445920 84513...

output:

32498

result:

ok single line: '32498'

Test #8:

score: 10
Accepted
time: 184ms
memory: 4184kb

input:

50000 254 18445304121
375481124 36148026
388507104 183081259
261838134 179691990
485282800 209534680...

output:

1564

result:

ok single line: '1564'

Test #9:

score: 10
Accepted
time: 621ms
memory: 4188kb

input:

50000 3260 4076050769242
210627773 8756794
68253913 5333287
812306900 176444281
561388618 94960450
6...

output:

23173

result:

ok single line: '23173'

Test #10:

score: 10
Accepted
time: 654ms
memory: 6492kb

input:

50000 44485 12129791734731
590854222 262410600
992399148 641692708
219274382 56485932
730651726 4088...

output:

49537

result:

ok single line: '49537'