先把所有券都用完,那么就有两种决策:
- 选择没选过的牛中原价最小的.
- 把优惠券转移到优惠价最低的未选择的牛 身上.(其实就是没选过的牛的最低优惠价+最低反悔价)
我们用a,b,c三个堆来实现.
a堆用于存未选过的牛的原价+编号.
b堆用于存未选过的牛的优惠价+编号.
c堆用于存反悔价.(即选择优惠的牛的原价-优惠价)
一开始往c堆塞k个0.(因为还不用反悔)
#include<queue>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define g getchar()
using namespace std;
typedef long long ll;
typedef pair<int,int> node;
const int N=5e4+10;
priority_queue<node,vector<node>,greater<node> >a,b;
priority_queue<int,vector<int>,greater<int> >c;
int n,k,x[N],y[N],cnt;ll s,m;
bool v[N];
template<class o>void qr(o&x) {
char c=g;x=0;
while(!isdigit(c))c=g;
while(isdigit(c))x=x*10+c-'0',c=g;
}
int main() {
qr(n);qr(k);qr(m);
for(int i=1;i<=k;i++)c.push(0);
for(int i=1;i<=n;i++) {
qr(x[i]);qr(y[i]);
a.push(make_pair(x[i],i));
b.push(make_pair(y[i],i));
}
while(1) {
while(a.size()&&v[a.top().second])
a.pop();
while(b.size()&&v[b.top().second])
b.pop();
if(!a.size())break;
node t1=a.top(),t2=b.top();int d=c.top();
if(t1.first<t2.first+d) {
if(s+t1.first>m)break;
s+=t1.first;cnt++;
v[t1.second]=1;
a.pop();
}
else {
if(s+t2.first+d>m)break;
s+=t2.first+d;cnt++;
v[t2.second]=1;
c.pop();c.push(x[t2.second]-y[t2.second]);
b.pop();
}
}
printf("%d\n",cnt);
return 0;
}