zoukankan      html  css  js  c++  java
  • 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
    ",cnt);
    	return 0;
    }
    
  • 相关阅读:
    Appium环境搭建(Mac版)
    html进阶css(1)
    HTML 表单与输出
    HTML 表格入门
    html图像入门
    LAMP_源码安装全教程
    构建高性能的MYSQL数据库系统-主从复制
    apache 服务器配制
    KickStart 无人值守安装系统
    zabbix 3.0 完全安装全解!
  • 原文地址:https://www.cnblogs.com/zsyzlzy/p/12373887.html
Copyright © 2011-2022 走看看