Luogu P3045 [USACO12FEB]牛券Cow Coupons

思路来源

先把所有券都用完,那么就有两种决策:

  1. 选择没选过的牛中原价最小的.
  2. 把优惠券转移到优惠价最低的未选择的牛 身上.(其实就是没选过的牛的最低优惠价+最低反悔价)

我们用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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值