ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#215277 | #2639. 省钱 | N | 40 | 5868ms | 6492kb | C++11 | 4.1kb | 2024-11-27 22:56:55 | 2024-11-27 23:40:23 |
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()];
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){
wd.erase(*ww.rbegin());
ww.erase(*ww.rbegin());
wd.insert(i);
ww.insert(i);
cost += c2;
}
}
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: 467ms
memory: 4188kb
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: 793ms
memory: 5944kb
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: 446ms
memory: 4188kb
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: 788ms
memory: 5360kb
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: 750ms
memory: 5944kb
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: 387ms
memory: 4188kb
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: 770ms
memory: 5356kb
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: 175ms
memory: 4188kb
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: 642ms
memory: 4184kb
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: 650ms
memory: 6492kb
input:
50000 44485 12129791734731 590854222 262410600 992399148 641692708 219274382 56485932 730651726 4088...
output:
49537
result:
ok single line: '49537'