2590: [Usaco2012 Feb]Cow Coupons

Time Limit: 10 Sec Memory Limit: 128 MB

Submit: 349 Solved: 181

[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<=109)。FJ有K张优惠券,使用优惠券购买第i头奶牛时价格会降为Ci(1<=Ci<=Pi),每头奶牛只能使用一次优惠券。FJ想知道花不超过M(1<=M<=1014)的钱最多可以买多少奶牛?

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.

HINT

Source

Gold

Solution Notes (Nathan Pinsker):

There are several different ways to approach this problem.

One of them stems from the initial idea of picking the lowest-cost cow each time: use all coupons on the cheapest

cows, then buy as many cows as possible without coupons. However, this doesn't quite work: if several cows are very cheap with or without a coupon, and other cows are cheap with a coupon but very expensive without one, we can intuitively see that we would like to use our coupons on the more expensive cows. This leads to the idea of "revoking" a coupon: for cow i, we can pay (\(P_i-C_i\)) in order to regain one of our coupons (because we are now buying cow i at the "expensive" price).

After purchasing as many cows as possible with coupons, we store their (\(P_i-C_i\)) values in a heap.

To purchase a remaining cow j, we can either pay \(P_j\) or \(C_j\)+ (\(P_i-C_i\)), where cow i is the top cow in our heap.

This ensures we are always using exactly as many coupons as we can.

For each cow we add to our lineup, we are greedily paying the minimum possible amount for it, so this solution is clearly optimal.

这个题解是从USACO上找的

他什么意思呢

这个题目可以用贪心做

先买下所有用优惠券最便宜的奶牛

然后找出试图用原价比优惠价贵很多的奶牛去替代相对来说优惠价和原价相差较小的奶牛,这样就能省下更多钱

这个东西可以用堆维护

Bruce Merry's solution (implementing this idea) is below:

#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std; typedef long long ll; struct pqitem{
ll value;
int index;
bool operator<(const pqitem &b)const{
return value>b.value;
}
pqitem(ll value,int index):value(value),index(index){}
}; int main(){
int N,K;
ll M;
cin>>N>>K>>M;
vector<ll>P(N),C(N);
for(int i=0;i<N;i++)
cin>>P[i]>>C[i];
typedef priority_queue<pqitem> pqtype;
priority_queue<ll,vector<ll>,greater<ll> >recover;
pqtype cheap;
pqtype expensive;
for(int i=0;i<K;i++)
recover.push(0LL);
for(int i=0;i<N;i++){
cheap.push(pqitem(C[i],i));
expensive.push(pqitem(P[i],i));
}
vector<bool>used(N,false);
int nused=0;
while(M>0&&nused<N){
while(used[cheap.top().index])
cheap.pop();
while(used[expensive.top().index])
expensive.pop();
if(recover.top()+cheap.top().value<expensive.top().value){
const pqitem top=cheap.top();
ll cost=recover.top()+top.value;
if(M<cost)
break;
M-=cost;
recover.pop();
recover.push(P[top.index]-C[top.index]);
used[top.index]=true;
}
else{
const pqitem top=expensive.top();
ll cost=top.value;
if(M<cost)
break;
M-=cost;
used[top.index]=true;
}
nused++;
}
cout<<nused;
return 0;
}

USACO 2012 Feb Cow Coupons的更多相关文章

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

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

  2. 2590: [Usaco2012 Feb]Cow Coupons

    2590: [Usaco2012 Feb]Cow Coupons Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 306  Solved: 154[Su ...

  3. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  4. [Usaco2012 Feb] Cow Coupons

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

  5. [USACO 2012 Feb Gold] Cow Coupons【贪心 堆】

    传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=118 传送门2:http://www.lydsy.com/JudgeOn ...

  6. BZOJ2590 [Usaco2012 Feb]Cow Coupons

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

  7. 【bzoj2591】[Usaco 2012 Feb]Nearby Cows 树形dp

    题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...

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

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

  9. BZOJ 1631 Usaco 2007 Feb. Cow Party

    [题解] 最短路裸题.. 本题要求出每个点到终点走最短路来回的距离,因此我们先跑一遍最短路得出每个点到终点的最短距离,然后把边反向再跑一遍最短路,两次结果之和即是答案. #include<cst ...

随机推荐

  1. shell功能

    日志切割: function rotate() { logs_path=$ echo Rotating Log: $ cp ${logs_path} ${logs_path}.$(date -d &q ...

  2. Mapper的方式总结

    Mapper的方式总结: <mappers> <!-- 通过package元素将会把指定包下面的所有Mapper接口进行注册 --> <package name=&quo ...

  3. HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)

    Problem Description A password locker with N digits, each digit can be rotated to 0-9 circularly.You ...

  4. DPDK Qos之报文处理流水线

    原创翻译,转载请注明出处. 下面是一个支持Qos的复杂报文处理流水线的图: 流水线是通过DPDP可重用的软件库构建出来的.在流水线里实现QoS主要是如下模块:policer,dropper,shced ...

  5. Jlink 软件断点和硬件断点

    调试2440 RAM拷贝至SDRAM遇到的问题 汇编代码主要是初始化一些寄存器,关狗,初始化时钟,初始化存储管理器以便访问内存,然后将SoC上4k RAM数据拷贝至SDRAM,然后在SRAM里面运行, ...

  6. [Elasticsearch] 多字段搜索 (五) - 以字段为中心的查询

    以字段为中心的查询(Field-centric Queries) 上述提到的三个问题都来源于most_fields是以字段为中心(Field-centric),而不是以词条为中心(Term-centr ...

  7. 《深入浅出 Java Concurrency》—并发容器 ConcurrentMap

    (转自:http://blog.csdn.net/fg2006/article/details/6404226) 在JDK 1.4以下只有Vector和Hashtable是线程安全的集合(也称并发容器 ...

  8. Codeforces Round #390 (Div. 2) E(bitset优化)

    题意就是一个给出2个字符矩阵,然后进行匹配,输出每个位置的匹配的结果 (超出的部分循环处理) 一种做法是使用fft,比较难写,所以没有写 这里使用一个暴力的做法,考虑到一共只出现26个字符 所以使用一 ...

  9. Flash by sshockwave [树dp]

    题目 给定一棵树,每个点有一个活动时间,长度为正整数$t_i$ 你需要安排每个点的活动时间什么时候开始什么时候结束,并且满足:任何一个时刻没有两个相邻的点都在活动 开始时刻为0,在以上条件下最小化所有 ...

  10. 无人值守安装linux系统

    需要使用到的服务:PXE + DHCP+TFTP+ Kickstart+ FTP KickStart是一种无人职守安装方式 执行 PXE + KickStart安装需要准备内容:  • DHCP 服务 ...