ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#215261 | #2639. 省钱 | N | 40 | 4856ms | 6252kb | C++11 | 3.2kb | 2024-11-27 20:53:26 | 2024-11-27 23:38:54 |
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;
cost = qs = 0;
ll c1, c2;
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);
cost += w[i];
}else{
cost += c1;
ww.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()];
if(c1 < 0 && c1 < c2){
qd.erase(*qq.rbegin());
qq.erase(*qq.rbegin());
qd.insert(i);
qq.insert(i);
cost += c1;
}else if(c2 < 0){
ww.erase(*ww.rbegin());
ww.insert(i);
cost += c2;
}
}
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;
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 486ms
memory: 4184kb
input:
50000 30828 852557364841 682084050 257603011 870868024 517458094 732267860 201407488 777566656 55879...
output:
17903
result:
ok single line: '17903'
Test #2:
score: 0
Wrong Answer
time: 569ms
memory: 4388kb
input:
50000 10508 8982273367520 34111224 12372852 549875017 525549262 357107918 219952140 644308048 222008...
output:
37597
result:
wrong answer 1st lines differ - expected: '37621', found: '37597'
Test #3:
score: 10
Accepted
time: 454ms
memory: 4096kb
input:
50000 23114 535861686266 359271294 298114231 605400720 491693949 755566780 539381575 155586610 92962...
output:
14723
result:
ok single line: '14723'
Test #4:
score: 0
Wrong Answer
time: 620ms
memory: 4232kb
input:
50000 13490 4616703243118 286358449 133228996 162995754 17235506 661390160 561824344 282751480 15433...
output:
30881
result:
wrong answer 1st lines differ - expected: '30961', found: '30881'
Test #5:
score: 0
Wrong Answer
time: 662ms
memory: 5128kb
input:
50000 26352 8630976119100 70133466 32927792 90392510 89764542 307782646 75889114 123168574 66039130 ...
output:
42934
result:
wrong answer 1st lines differ - expected: '42944', found: '42934'
Test #6:
score: 10
Accepted
time: 369ms
memory: 3572kb
input:
50000 11800 213255455323 405512104 311547645 122797690 35257030 782246460 533338866 416860264 504733...
output:
9869
result:
ok single line: '9869'
Test #7:
score: 0
Wrong Answer
time: 628ms
memory: 4528kb
input:
50000 19734 4267681411347 732638120 327229436 361949068 274173372 539440696 285784669 94445920 84513...
output:
32439
result:
wrong answer 1st lines differ - expected: '32498', found: '32439'
Test #8:
score: 0
Wrong Answer
time: 84ms
memory: 3024kb
input:
50000 254 18445304121 375481124 36148026 388507104 183081259 261838134 179691990 485282800 209534680...
output:
1562
result:
wrong answer 1st lines differ - expected: '1564', found: '1562'
Test #9:
score: 0
Wrong Answer
time: 360ms
memory: 3172kb
input:
50000 3260 4076050769242 210627773 8756794 68253913 5333287 812306900 176444281 561388618 94960450 6...
output:
23087
result:
wrong answer 1st lines differ - expected: '23173', found: '23087'
Test #10:
score: 10
Accepted
time: 624ms
memory: 6252kb
input:
50000 44485 12129791734731 590854222 262410600 992399148 641692708 219274382 56485932 730651726 4088...
output:
49537
result:
ok single line: '49537'