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 ...
随机推荐
- php 删除指定扩展名文件
<?php /** *@param $path文件夹绝对路径 $file_type待删除文件的后缀名 *return void */ function clearn_file($path, $f ...
- html ajax请求 php 下拉 加载更多数据 (也可点击按钮加载更多)
<input type="hidden" class="total_num" id="total" value="{$tot ...
- 《算法》第四版 IDEA 运行环境的搭建
<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...
- [Bzoj2282]消防(二分答案+树的直径)
Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家 ...
- LoadRunner使用代理远程执行提示找不到“pre_cci.c”文件
好久没有使用LoadRunner了,工作需要使用一下,执行总是提示找不到“pre_cci.c”文件,找问题花了很长时间终于找到问题了.万事还是需要找到原因: cci 会将 pre_cci.c 文件作为 ...
- 15.2,redis发布订阅
发布publish 订阅subscribe Redis 通过 PUBLISH . SUBSCRIBE 等命令实现了订阅与发布模式. 举例1: qq群的公告,单个发布者,多个收听者 发布/订阅 实验 发 ...
- P2680 运输计划(二分+树上差分)
P2680 运输计划 链接 分析: 二分+树上差分. 首先可以二分一个答案,那么所有比这个答案大的路径,都需要减去些东西才可以满足这个答案. 那么减去的这条边一定在所有的路径的交集上. 那么如果求快速 ...
- nohup 重定向的问题-- 费元星 站长
费元星 大牛 以前只知道使用nohup可以让一个程序后台执行,但是生成的日志文件都放到nohup.out中了,不能自己指定,尤其是在同一个目录下我需要让两个甚至多个程序都要后台执行时,这样看日志就比较 ...
- Postman-CI集成Jenkins(3)
Postman-CI集成Jenkins(3) Postman-简单使用 Postman-进阶使用 Postman-CI集成Jenkins Newman 官方说明:Postman's command-l ...
- vue计算属性和观察者
1. 计算属性 模板内的表达式非常便利,但在模板中放入太多的逻辑会让模板过重且难以维护,所有就有了计算属性 例子: //html代码 <div id="example"> ...