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.

输入输出样例

输入样例#1:

4 1 7
3 2
2 2
8 1
4 3
输出样例#1:

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的更多相关文章

  1. P3045 [USACO12FEB]牛券Cow Coupons

    P3045 [USACO12FEB]牛券Cow Coupons 贪心题.先选中 \(c_i\) 最小的 \(k\) 头牛,如果这样就超过 \(m\) ,直接退出,输出答案.否则考虑把后面的牛依次加入, ...

  2. [USACO12FEB]牛券Cow Coupons(堆,贪心)

    [USACO12FEB]牛券Cow Coupons(堆,贪心) 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= ...

  3. [USACO12FEB]牛券Cow Coupons

    嘟嘟嘟 这其实是一道贪心题,而不是dp. 首先我们贪心的取有优惠券中价值最小的,并把这些东西都放在优先队列里,然后看[k + 1, n]中,有些东西使用了优惠券减的价钱是否比[1, k]中用了优惠券的 ...

  4. LuoguP3045牛券Cow Coupons

    LuoguP3045 [USACO12FEB]牛券Cow Coupons 果然我贪心能力还是太差了 ZR讲过的原题我回来对做法没有一丁点印象 有时候有这样一种题目 每个数有两种不同的价值 你可以选择价 ...

  5. 洛谷P3048 [USACO12FEB]牛的IDCow IDs

    P3048 [USACO12FEB]牛的IDCow IDs 12通过 67提交 题目提供者lin_toto 标签USACO2012 难度普及/提高- 时空限制1s / 128MB 提交  讨论  题解 ...

  6. 洛谷——P2952 [USACO09OPEN]牛线Cow Line

    P2952 [USACO09OPEN]牛线Cow Line 题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a l ...

  7. 洛谷 P3048 [USACO12FEB]牛的IDCow IDs

    题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...

  8. 洛谷 P3014 [USACO11FEB]牛线Cow Line

    P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) ...

  9. 洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

    P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N ...

随机推荐

  1. SQL Server中Table字典数据的查询SQL示例代码

    SQL Server中Table字典数据的查询SQL示例代码 前言 在数据库系统原理与设计(第3版)教科书中这样写道: 数据库包含4类数据: 1.用户数据 2.元数据 3.索引 4.应用元数据 其中, ...

  2. hello spring boot neo4j

    新建springboot 项目: https://www.cnblogs.com/lcplcpjava/p/7406253.html bug fixs: 1. Maven Configuration ...

  3. k8s的service简述

    k8s向集群外部暴露端口的3种方式: 1.service->nodePort :仅暴露一个宿主机端口,用于集群外部访问,因为此操作被写入各个节点的iptables或ipvs规则当中,可以用任意一 ...

  4. Window_Bat_Scripts—检测特定网段未使用的IP地址

    1.1    脚本名称 Check_IP_Not_Use.bat 1.2    脚本代码 @Echo off set /p input_number=请输入网络位(192.168.1.): IF EX ...

  5. tp5查询

    TP5的EXP.批量查询.聚合查询等. <!--more--> //使用EXP条件表达式,表示后面是原生的SQL表达式 $result = Db::table('think_inno')- ...

  6. nginx的缓存服务

    都知道缓存的目的是为了减小服务端的压力,可以在客户端直接取到数据 客户端---------------nginx(代理缓存)------------------服务端 代理缓存的描述: 就是客户端发送 ...

  7. HDU1301 Jungle Roads

    Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...

  8. jvm探秘之三:GC初步

    GC即垃圾收集器,虚拟机的必要组成部分. 不过这里说当然是,hotspot虚拟机(jvm的主要版本)的GC机制,前面说过了jvm的组成部分,那么想当然GC只需要负责方法区和堆就好了,虚拟机栈.本地方法 ...

  9. sql中一个服务器建立另一个服务器的连接

    EXEC sp_addlinkedserver 'TonyLink','','SQLOLEDB','111.111.1.111(服务器名)' EXEC sp_addlinkedsrvlogin 'To ...

  10. HDU 3896 Greatest TC 双连通分量

    题意 给一个连通的无向图,有两种询问: \(a, b, c, d\),问如果删掉\(c,d\)之间的边,\(a,b\)之间是否还连通 \(a, b, c\),问如果删掉顶点\(c\),\(a,b\)之 ...