洛谷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 ...
随机推荐
- ES6 Promise用法详解
What is Promise? Promise是一个构造函数,接受一个参数(Function),并且该参数接受两个参数resolve和reject(分别表示异步操作执行成功后的回调函数.执行失败后的 ...
- javaweb基础(26)_jsp标签库开发二
一.JspFragment类介绍 javax.servlet.jsp.tagext.JspFragment类是在JSP2.0中定义的,它的实例对象代表JSP页面中的一段符合JSP语法规范的JSP片段, ...
- C语言字符,字符串,字节操作常用函数
strlen 这个函数是在 string.h 的头文件中定义的 它的函数原型是 size_t strlen( const char ); size_t 是一个无符号整型,是这样定义的 typedef ...
- webpack4简单入门
安装webpack需要安装node环境,因此需要在电脑中安装node.node官网https://nodejs.org/,安装LTS版本即可. webpck基本概念 entry:分析依赖模块的入口 o ...
- http 调用错误处理
1. http code 在使用Nginx时,经常会碰到502 Bad Gateway和504 Gateway Time-out错误,下面以Nginx+PHP-FPM来分析下这两种常见错误的原因和解决 ...
- 从指定的view中截图 返回UIImage
-(UIImage *)getImageFromView:(UIView *)view{ UIGraphicsBeginImageContext(view.bounds.size); [view.la ...
- SAP系统管理中常见问题解答(转载)
1.如何查看SAP系统的位数? system——status看 Platform ID Platform 32-bit 64-bit --------------------------------- ...
- MySQL解决中文编码问题
转载组员博客 地址:MySQL解决中文编码问题
- 三十二、MySQL 导出数据
MySQL 导出数据 MySQL中你可以使用SELECT...INTO OUTFILE语句来简单的导出数据到文本文件上. 使用 SELECT ... INTO OUTFILE 语句导出数据 以下实例中 ...
- 企业shell面试题及解答
1.面试题:使用for循环在/tmp目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串template,示例如下 aaesdffbnv_template.html 方 ...