2590: [Usaco2012 Feb]Cow Coupons
2590: [Usaco2012 Feb]Cow Coupons
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 306 Solved: 154
[Submit][Status][Discuss]
Description
Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget of M units of money (1 <= M <= 10^14). Cow i costs P_i money (1 <= P_i <= 10^9), but FJ has K coupons (1 <= K <= N), and when he uses a coupon on cow i, the cow costs C_i instead (1 <= C_i <= P_i). FJ can only use one coupon per cow, of course. What is the maximum number of cows FJ can afford? PROBLEM NAME: coupons
FJ准备买一些新奶牛,市场上有N头奶牛(1<=N<=50000),第i头奶牛价格为Pi(1<=Pi<=10^9)。FJ有K张优惠券,使用优惠券购买第i头奶牛时价格会降为Ci(1<=Ci<=Pi),每头奶牛只能使用一次优惠券。FJ想知道花不超过M(1<=M<=10^14)的钱最多可以买多少奶牛?
Input
* Line 1: Three space-separated integers: N, K, and M.
* Lines 2..N+1: Line i+1 contains two integers: P_i and C_i.
Output
* Line 1: A single integer, the maximum number of cows FJ can afford.
Sample Input
3 2
2 2
8 1
4 3
Sample Output
OUTPUT DETAILS: FJ uses the coupon on cow 3 and buys cows 1, 2, and 3, for a total cost of 3 + 2 + 1 = 6.
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define LL long long
using namespace std;
const int M=;
const LL inf=1e15;
LL read(){
LL ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
LL m,n,k,v,cost;
LL w[M],p[M],ans,sum;
bool f[M];
struct node{
LL w,pos;
bool operator < (const node& x)const {return x.w<w;}
};
priority_queue<node>q1,q2,q3;
int main()
{
n=read(); k=read(); m=read();
k=min(k,n);
for(int i=;i<=n;i++){
w[i]=read(); q1.push((node){w[i],i});
p[i]=read(); q2.push((node){p[i],i});
}
for(ans=;ans<=k;ans++){
node x=q2.top();
if(cost+x.w>m){printf("%lld\n",ans-); return ;}
q2.pop(); f[x.pos]=;
cost+=x.w;
q3.push((node){w[x.pos]-p[x.pos],x.pos});
}ans--;
if(ans==n){printf("%lld\n",ans); return ;}
while(cost<=m&&ans<n){
node x=q3.top(),y=q2.top(),z=q1.top();
while(f[y.pos]&&!q2.empty()) q2.pop(),y=q2.top();
if(q2.empty()) y.w=inf;
while(f[z.pos]&&!q1.empty()) q1.pop(),z=q1.top();
if(q1.empty()) z.w=inf;
if(x.w+y.w<=z.w) f[y.pos]=,q3.pop(),q2.pop(),q3.push((node){w[y.pos]-p[y.pos],y.pos}),cost+=x.w+y.w ;
else q1.pop(),f[z.pos]=,cost+=z.w;
if(cost<=m) ans++;
}
printf("%lld\n",ans);
return ;
}
2590: [Usaco2012 Feb]Cow Coupons的更多相关文章
- [Usaco2012 Feb] Cow Coupons
[Usaco2012 Feb] Cow Coupons 一个比较正确的贪心写法(跑得贼慢...) 首先我们二分答案,设当前答案为mid 将序列按照用券之后能省掉的多少排序,那么我们对于两种情况 \(m ...
- BZOJ2590 [Usaco2012 Feb]Cow Coupons
好吧...想了半天想错了...虽然知道是贪心... 我们每次找没有被买的两种价格最小的牛,比较a = 当前差价最大的 + 当前优惠券价格最小的牛与b = 当前非优惠券价格最小的牛 所以...我们要 先 ...
- 【贪心】【堆】bzoj2590 [Usaco2012 Feb]Cow Coupons
每个物品有属性a,b 考虑在仅仅用光优惠券时的最优方案. 显然是按照b排序,取前K个. 但是我们还要尽可能去取剩余的. 假设朴素地取剩余的话,应该把剩余的对a排序,然后尽量去取. 但是有可能对其用优惠 ...
- USACO 2012 Feb Cow Coupons
2590: [Usaco2012 Feb]Cow Coupons Time Limit: 10 Sec Memory Limit: 128 MB Submit: 349 Solved: 181 [Su ...
- [Usaco 2012 Feb]Cow coupons牛券:反悔型贪心
Description Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), ...
- 洛谷P3045 [USACO12FEB]牛券Cow Coupons
P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...
- BZOJ1631: [Usaco2007 Feb]Cow Party
1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 459 Solved: 338[Submit ...
- BZOJ3301: [USACO2011 Feb] Cow Line
3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 67 Solved: 39[Submit ...
- BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序
1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 387 Solved: 215[S ...
随机推荐
- 3.4.2 Undefined类型【JavaScript高级程序设计第三版】
Undefined 类型只有一个值,即特殊的 undefined.在使用 var 声明变量但未对其加以初始化时,这个变量的值就是 undefined,例如: var message; alert(me ...
- 《Python语言及其应用》学习笔记
第二章 ========== 对象的类型决定了可以对它进行的操作.对象的类型还决定了它装着的数据是允许被修改的变量(可变的),还是不可被修改的常量(不可变的). Python是强类型的,你永远无法修改 ...
- urllib使用一
urllib.urlopen()方法: 参数: 1.url(要访问的网页链接http:或者是本地文件file:) 2.data(如果有,就会由GET方法变为POST方法,提交的数据格式必须是appli ...
- scrapy如何实现分布式爬虫
使用scrapy爬虫的时候,记录一下如何分布式爬虫问题: 关键在于多台主机协作的关键:共享爬虫队列 主机:维护爬取队列从机:负责数据抓取,数据处理,数据存储 队列如何维护:Redis队列Redis 非 ...
- 剑指offer题目练习一
看见了一道二维数组找数的题,已排好序的数组(从左至右从上到下,都是由小变大的)让找数,瞬间就出思路了,并没有必要去看他的解释,两次二分就搞定了. #include<cstdio> #inc ...
- 1,flask简介
一. Python 现阶段三大主流Web框架 Django Tornado Flask 对比 1.Django 主要特点是大而全,集成了很多组件,例如: Models Admin Form 等等, 不 ...
- HDU 5293 Tree chain problem 树形DP
题意: 给出一棵\(n\)个节点的树和\(m\)条链,每条链有一个权值. 从中选出若干条链,两两不相交,并且使得权值之和最大. 分析: 题解 #include <cstdio> #incl ...
- hihocoder #1394 : 网络流四·最小路径覆盖(最小路径覆盖)
#1394 : 网络流四·最小路径覆盖 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 国庆期间正是旅游和游玩的高峰期. 小Hi和小Ho的学习小组为了研究课题,决定趁此机 ...
- Spring MVC重定向和转发
技术交流群:233513714 转发和重定向 开始Java EE时,可能会对转发(forward)和重定向(redirect)这个两个概念不清楚.本文先通过代码实例和运行结果图片感性 认识二者的区别, ...
- 《Cracking the Coding Interview》——第4章:树和图——题目2
2014-03-19 03:32 题目:给定一个有向图,判断其中两点是否联通. 解法:DFS搜索解决,如果是无向图的话,就可以用并查集高效解决问题了. 代码: // 4.2 Write a progr ...