洛谷P3045 [USACO12FEB]牛券Cow Coupons
P3045 [USACO12FEB]牛券Cow Coupons
- 71通过
- 248提交
- 题目提供者洛谷OnlineJudge
- 标签USACO2012云端
- 难度提高+/省选-
- 时空限制1s / 128MB
提交 讨论 题解
最新讨论更多讨论
- 86分求救
题目描述
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?
FJ准备买一些新奶牛,市场上有N头奶牛(1<=N<=50000),第i头奶牛价格为Pi(1<=Pi<=10^9)。FJ有K张优惠券,使用优惠券购买第i头奶牛时价格会降为Ci(1<=Ci<=Pi),每头奶牛只能使用一次优惠券。FJ想知道花不超过M(1<=M<=10^14)的钱最多可以买多少奶牛?
输入输出格式
输入格式:
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.
输出格式:
- Line 1: A single integer, the maximum number of cows FJ can afford.
输入输出样例
- 4 1 7
- 3 2
- 2 2
- 8 1
- 4 3
- 3
说明
FJ has 4 cows, 1 coupon, and a budget of 7.
FJ uses the coupon on cow 3 and buys cows 1, 2, and 3, for a total cost of 3 + 2 + 1 = 6.
分析:其实很容易发现这就是一道背包题,对于每头牛我们都有用与不用优惠券两种选择,然而会发现,这个m不是一般的大,所以不能用dp.dp和贪心是差不多的,考虑到dp不行,试试贪心。因为我们的目标是要使买的牛最多,也就是花的钱最少,于是我当时想了一种贪心:我们可以取前k个用优惠券的价格(从小到大排序),然后和不排序的放在一起排序一下,然后遍历求解.这样的话有一个问题:我们已经假定前k个用优惠券的牛用优惠券,然而有时候不用优惠券比用优惠券要好,那就是用不用价格都相等的情况,所以我们不再取前k个,我们把每头牛拆成2头牛,一头用优惠券,一头不用,然后排序求解即可.
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <vector>
- #include <queue>
- #include <functional>
- using namespace std;
- int n, k,p[],c[],vis[],ans;
- long long m;
- struct node
- {
- int id, use, money;
- }e[];
- bool cmp(node a, node b)
- {
- if (a.money == b.money)
- return a.use < b.use;
- return a.money < b.money;
- }
- int main()
- {
- scanf("%d%d%lld", &n, &k, &m);
- for (int i = ; i <= n; i++)
- {
- scanf("%d%d", &p[i], &c[i]);
- e[i * - ].id = i;
- e[i * - ].use = ;
- e[i * - ].money = c[i];
- e[i * ].id = i;
- e[i * ].use = ;
- e[i * ].money = p[i];
- }
- sort(e + , e + n * + , cmp);
- for (int i = ; i <= n * ; i++)
- {
- if (vis[e[i].id])
- continue;
- if (e[i].use && k <= )
- continue;
- if (m <= )
- break;
- if (m >= e[i].money)
- {
- vis[e[i].id] = ;
- ans++;
- m -= e[i].money;
- if (e[i].use)
- k--;
- }
- }
- printf("%d", ans);
- return ;
- }
洛谷P3045 [USACO12FEB]牛券Cow Coupons的更多相关文章
- P3045 [USACO12FEB]牛券Cow Coupons
P3045 [USACO12FEB]牛券Cow Coupons 贪心题.先选中 \(c_i\) 最小的 \(k\) 头牛,如果这样就超过 \(m\) ,直接退出,输出答案.否则考虑把后面的牛依次加入, ...
- [USACO12FEB]牛券Cow Coupons(堆,贪心)
[USACO12FEB]牛券Cow Coupons(堆,贪心) 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= ...
- [USACO12FEB]牛券Cow Coupons
嘟嘟嘟 这其实是一道贪心题,而不是dp. 首先我们贪心的取有优惠券中价值最小的,并把这些东西都放在优先队列里,然后看[k + 1, n]中,有些东西使用了优惠券减的价钱是否比[1, k]中用了优惠券的 ...
- LuoguP3045牛券Cow Coupons
LuoguP3045 [USACO12FEB]牛券Cow Coupons 果然我贪心能力还是太差了 ZR讲过的原题我回来对做法没有一丁点印象 有时候有这样一种题目 每个数有两种不同的价值 你可以选择价 ...
- 洛谷P3048 [USACO12FEB]牛的IDCow IDs
P3048 [USACO12FEB]牛的IDCow IDs 12通过 67提交 题目提供者lin_toto 标签USACO2012 难度普及/提高- 时空限制1s / 128MB 提交 讨论 题解 ...
- 洛谷——P2952 [USACO09OPEN]牛线Cow Line
P2952 [USACO09OPEN]牛线Cow Line 题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a l ...
- 洛谷 P3048 [USACO12FEB]牛的IDCow IDs
题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...
- 洛谷 P3014 [USACO11FEB]牛线Cow Line
P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) ...
- 洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver
P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N ...
随机推荐
- C语言字符,字符串,字节操作常用函数
strlen 这个函数是在 string.h 的头文件中定义的 它的函数原型是 size_t strlen( const char ); size_t 是一个无符号整型,是这样定义的 typedef ...
- macbook pro开机键盘键盘和触摸板没反应问题
今天遇到开机键盘和触摸板没反应的问题,打电话给售后,他叫我插一个usb外置键盘,开机时按shift+alt+control+电源键开机,突然发现可以了,这bug我也是醉了
- 爬虫学习(五)——使用handler管理器对象进行数据爬取的步骤
# 使用管理器对象进行爬取数据的步骤 import urllib.requesturl = "https://www.baidu.com/"# 创建handler的管理器对象han ...
- 第2 章Python 语言基础
必背必记 1.转义字符 Python 中的字符串还支持转义字符.所谓转义字符是指使用反斜杠“\”对一些特殊字符进行转义. \ 续行符 \n 换行符 \0 空 \t 水平制表符,用于横向跳到下一制表 ...
- 【CSS】非常简单的css实现div悬浮页面底部
<div id="demo_div"></div> <style> #demo_div{ left:; position: fixed; bot ...
- Essential C++ 3.1 节的代码练习——指针方式
// // PointerToValue.cpp // Working // // Created by Hawkins, Dakota Y on 6/3/16. // Copyright 2016 ...
- poj 2533Longest Ordered Subsequence
Longest Ordered Subsequence Description A numeric sequence of ai is ordered if a1 < a2 < - < ...
- HOJ_14001 Just Terraffic!
题意相对来说比较扭曲..所以来说下模型,具体做法有兴趣的孩纸去问度娘或者波塞冬吧~~ 给出一个序列长度,并且输入该序列,该序列的含义是横坐标: 任何两个相邻坐标绝对值小于等于1000的必然为一个整体, ...
- A brief look at the Objects in JavaScript
Objects An object is a self-contained collection of data. This data comes in to forms: properties ...
- js数据类型的检测总结,附面试题--封装一个函数,输入任意,输出他的类型
一.javascript 中有几种类型的值 1.基本数据类型 : 包括 Undefined.Null.Boolean.Number.String.Symbol (ES6 新增,表示独一无二的值) 特点 ...