BZOJ2590 [Usaco2012 Feb]Cow Coupons
好吧。。。想了半天想错了。。。虽然知道是贪心。。。
我们每次找没有被买的两种价格最小的牛,比较a = 当前差价最大的 + 当前优惠券价格最小的牛与b = 当前非优惠券价格最小的牛
所以。。。我们要
先维护两个小根堆,分别表示用优惠券买的牛的价格和不用优惠券买的牛的价格
还有个叫Recover的大根堆,表示当前几个用优惠券的那几头牛的差价(差价定义为非优惠价格与优惠价格的差值)
a与b哪个小就买哪个。。。
/**************************************************************
Problem: 2590
User: rausen
Language: C++
Result: Accepted
Time:140 ms
Memory:3332 kb
****************************************************************/ #include <cstdio>
#include <algorithm>
#include <queue> using namespace std;
typedef long long ll;
const int N = ;
struct data{
int w, v;
data(void){}
data(int x, int y) : w(x), v(y) {}
};
inline bool operator < (const data a, const data b){
return a.v > b.v;
}
int cnt, n, k;
int p[N], c[N];
bool vis[N];
ll m;
priority_queue <data> h, H;
priority_queue <ll, vector<ll>, greater<ll> > Re; inline int read(){
int x = ;
char ch = getchar();
while (ch < '' || ch > '')
ch = getchar();
while (ch >= '' && ch <= ''){
x = x * + ch - '';
ch = getchar();
}
return x;
} int main(){
int i;
ll cost;
data T;
n = read(), k = read();
scanf("%lld", &m);
for (i = ; i <= n; ++i){
p[i] = read(), c[i] = read();
h.push(data(i, c[i]));
H.push(data(i, p[i]));
}
while (!Re.empty()) Re.pop();
for (i = ; i <= k; ++i)
Re.push();
while (m > && cnt < n){
while (vis[h.top().w])
h.pop();
while (vis[H.top().w])
H.pop();
if (Re.top() + h.top().v < H.top().v){
T = h.top(), cost = Re.top() + T.v;
if (m < cost) break;
m -= cost;
Re.pop();
Re.push(p[T.w] - c[T.w]);
vis[T.w] = ;
}else{
T = H.top(), cost = T.v;
if (m < cost) break;
m -= cost;
vis[T.w] = ;
}
++cnt;
}
printf("%d\n", cnt);
return ;
}
BZOJ2590 [Usaco2012 Feb]Cow Coupons的更多相关文章
- 【贪心】【堆】bzoj2590 [Usaco2012 Feb]Cow Coupons
每个物品有属性a,b 考虑在仅仅用光优惠券时的最优方案. 显然是按照b排序,取前K个. 但是我们还要尽可能去取剩余的. 假设朴素地取剩余的话,应该把剩余的对a排序,然后尽量去取. 但是有可能对其用优惠 ...
- 2590: [Usaco2012 Feb]Cow Coupons
2590: [Usaco2012 Feb]Cow Coupons Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 306 Solved: 154[Su ...
- [Usaco2012 Feb] Cow Coupons
[Usaco2012 Feb] Cow Coupons 一个比较正确的贪心写法(跑得贼慢...) 首先我们二分答案,设当前答案为mid 将序列按照用券之后能省掉的多少排序,那么我们对于两种情况 \(m ...
- 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 ...
随机推荐
- C++ tinyXml直接解析XML字符串
转载:http://www.cnblogs.com/1024Planet/p/4401929.html <?xml version=\"1.0\" encoding=\&qu ...
- Spark样本类与模式匹配
一.前言 样本类(case class)与模式匹配(pattern matching)是Scala中一个比较复杂的概念,往往让人感觉深陷泥沼.我在这里对Scala中的样本类与模式匹配进行了一些整理,希 ...
- Python3基础 try-多个指定except与不指定except 简单示例
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Go第三篇之大话容器
Go语言数组 数组(Array)是一段固定长度的连续内存区域 在 Go 语言中,数组从声明时就确定,使用时可以修改数组成员,但是数组大小不可变化 Go 语言数组的声明 数组的写法如下: var 数组变 ...
- 将Sublime Text 添加到鼠标右键菜单的教程方法
安装notepad++软件,在菜单右键自动会添加“edit with notepad++"的选项,那么怎么将Sublime Text 添加到鼠标右键菜单呢?下面是我的操作过程,希望有帮助! ...
- hdu 3698 UVA1490 Let the light guide us 线段树优化DP
题目链接 and 题目大意 hdu3698 但是 hdu的数据比较弱,所以在这luogu提交吧UVA1490 Let the light guide us 有一个\(n*m\)的平原,要求每行选一个点 ...
- Educational Codeforces Round 57 (Rated for Div. 2)
我好菜啊. A - Find Divisible 好像没什么可说的. #include<cstdio> #include<cstring> #include<algori ...
- [bzoj 1774][Usaco2009 Dec]Toll 过路费
题目描述 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费 ...
- UVa 10163 仓库守卫
https://vjudge.net/problem/UVA-10163 题意: 有n个仓库,m个管理员,每个管理员有一个能力值P(接下来的一行有m个数,表示每个管理员的能力值) 每个仓库只能由一个管 ...
- UVa 674 Coin Change(完全背包)
https://vjudge.net/problem/UVA-674 题意: 计算兑换零钱的方法共有几种. 思路: 完全背包基础题. #include<iostream> #include ...