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. 手机连上同一个局域网的PC,修改项目的vhost配置

    <VirtualHost 172.16.6.100:80> DocumentRoot "D:\phpStudy\WWW\mywork\many_school" Serv ...

  2. Pandas 数值计算和统计基础

    1.(1) # 基本参数:axis.skipna import numpy as np import pandas as pd df = pd.DataFrame({'key1':[4,5,3,np. ...

  3. MySQL CONCAT()与GROUP_CONCAT()的使用

      1 . MySQL CONCAT(str1,str2, ...)  --返回连接的字符串 mysql> SELECT CONCAT('My', 'S', 'QL'); -> 'MySQ ...

  4. JavaSE——javac、javap、jad

    一.javac 用法:javac <选项> <源文件> 其中,可能的选项包括: -help                            帮助信息   -g       ...

  5. TouTiao开源项目 分析笔记18 视频详情页面

    1.效果预览 1.1.需要做到的真实效果 1.2.触发的点击事件 在MediaArticleVideoViewBinder的每一个item点击事件中: VideoContentActivity.lau ...

  6. Struts2---数据封装机制

    Struts2属性驱动和模型驱动 自动完成了数据的获取和封装 LoginAction.java public class LoginAction implements ModelDriven<U ...

  7. 从键盘输入数,输出它们的平方值&判断是不是2的阶次方数

    1.从键盘输入两个整数,然后输出它们的平方值和立方值 在Java中,没有像C语言那样有一个专供接受键盘输入值的scanf函数,所以一般的做法是从键盘输入一行字符,保存到字符串s中,再将字符组成的字符串 ...

  8. luogu4196 [CQOI2006]凸多边形 半平面交

    据说pkusc出了好几年半平面交了,我也来水一发 ref #include <algorithm> #include <iostream> #include <cstdi ...

  9. 途牛banner自动轮播

    <!DOCTYPE html>               <!--申明文档类型:html--> <html lang="en">       ...

  10. Git从入门到熟练

    Git的特性 1. 分布式版本控制 集中式VS分布式 保存更新时的文件快照而非差异 (快照 :是文件系统中的概念或者技术:来自照相领域的概念,是指特定时间点的一个状态) 其他系统在每个版本中记录着各个 ...