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

4 1 7
3 2
2 2
8 1
4 3

Sample Output

3
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.
 
跟着大爷跑来写了波题 找个时间也去刷刷USACO(觉得要跪)
这道题据说网上大部分题解都是错的???手动黑人问号脸 可能数据比较水吧
据tjm大爷讲:
 首先肯定选k个优惠券最小的,如果不够选直接输出。如果还有多余的钱的话,比较一下(已买的里面原价与优惠价的最小差价+没买的里面的最小优惠价)和(没买的里面的最小原价),选一个小的。第一个实际上相当于把优惠卷花钱赎回来。。。神犇传送门(tjm大爷的博客)
我的思路自然是照着大爷写的一波 写法可能有点小不同 但思路一样就好啦 2333
当然还得不要脸地贴一波自己的博客啦
#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的更多相关文章

  1. [Usaco2012 Feb] Cow Coupons

    [Usaco2012 Feb] Cow Coupons 一个比较正确的贪心写法(跑得贼慢...) 首先我们二分答案,设当前答案为mid 将序列按照用券之后能省掉的多少排序,那么我们对于两种情况 \(m ...

  2. BZOJ2590 [Usaco2012 Feb]Cow Coupons

    好吧...想了半天想错了...虽然知道是贪心... 我们每次找没有被买的两种价格最小的牛,比较a = 当前差价最大的 + 当前优惠券价格最小的牛与b = 当前非优惠券价格最小的牛 所以...我们要 先 ...

  3. 【贪心】【堆】bzoj2590 [Usaco2012 Feb]Cow Coupons

    每个物品有属性a,b 考虑在仅仅用光优惠券时的最优方案. 显然是按照b排序,取前K个. 但是我们还要尽可能去取剩余的. 假设朴素地取剩余的话,应该把剩余的对a排序,然后尽量去取. 但是有可能对其用优惠 ...

  4. USACO 2012 Feb Cow Coupons

    2590: [Usaco2012 Feb]Cow Coupons Time Limit: 10 Sec Memory Limit: 128 MB Submit: 349 Solved: 181 [Su ...

  5. [Usaco 2012 Feb]Cow coupons牛券:反悔型贪心

    Description Farmer  John  needs  new  cows! There  are  N  cows  for  sale (1 <= N <= 50,000), ...

  6. 洛谷P3045 [USACO12FEB]牛券Cow Coupons

    P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...

  7. BZOJ1631: [Usaco2007 Feb]Cow Party

    1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 459  Solved: 338[Submit ...

  8. BZOJ3301: [USACO2011 Feb] Cow Line

    3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 67  Solved: 39[Submit ...

  9. BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 215[S ...

随机推荐

  1. python爬虫:利用正则表达式爬取豆瓣读书首页的book

    1.问题描述: 爬取豆瓣读书首页的图书的名称.链接.作者.出版日期,并将爬取的数据存储到Excel表格Douban_I.xlsx中 2.思路分析: 发送请求--获取数据--解析数据--存储数据 1.目 ...

  2. 各种Nand的总结

    1. 微观 NAND闪存NAND是非易失性存储技术,NAND闪存由多个存放以位(bit)为单位的单元构成,这些位通过电荷被打开或关闭,如何组织这些开关单元来储存在SSD上的数据,也决定了NAND闪存的 ...

  3. 17-比赛1 F - 较小元素 Weak in the Middle (set)

    Seg-El has last chance to make the final changes in order to prevent the destruction of Krypton. He ...

  4. android:windowBackground 和 Android:background 的区别

    通过问别人,我知道了android:windowBackground 和 Android:background的区别 android:windowBackground 一般用于activity启动的时 ...

  5. Maven学习 (五) Elipse中发布一个Maven项目到Tomcat

    对于maven初学者的我,经常遇到一个问题就是,maven项目创建成功后,本来已经添加了jar的依赖,但是发布到Tomcat中就是没有jar包存在, 启动Tomcat总是报没有找到jar包,可项目结构 ...

  6. 打开Vim/Vi代码高亮

    由于新装Vim/Vi 默认是没有打开代码高亮配置的,就看到有朋友一次次到网上去找各种配置.其实Vim默认带来配置文件的样本的,只需拷贝过来就可使用. 在用户根目录(~)中新建vim的配置文件 .vim ...

  7. iOS-QQ好友列表实现

    0.QQ好友列表实现 0.首先说说实现思路 自定义UITableView,每一个分组都是一个UITableViewHeaderFooterView,然后自定义cell,这里分组的实现主要是自定义UIT ...

  8. pdb在python程序中应用

    1.什么是pdb? pdb是python提供的调试程序的一种工具. 2.为什么需要pdb模块? 当我们的程序越写越大的时候,我们用print xxx 这种方式打断点,调试,非常不方便,这个时候我们需要 ...

  9. WPF调用摄像头

    添加程序集:WPFMediaKit.dll 更关键代码如下: 界面设计代码如下: <Window x:Class="摄像头调用.MainWindow" xmlns=" ...

  10. glance上传镜像

    glance image-create --name "centos68-test" --file centos68.dsk \ --disk-format raw --conta ...