P2983 [USACO10FEB]购买巧克力
P2983 [USACO10FEB]购买巧克力
题解
注意题目开 long long
贪心策略:价格从低到高,买够为止
反证:若剩下的有一个K”,比K小,那么交换,稳赚不赔
所以,在买K之前,所有比他便宜的都买完了
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue> using namespace std; #define ll long long inline ll read()
{
ll ans=;
char last=' ',ch=getchar();
while(ch<''||ch>'') last=ch,ch=getchar();
while(ch>=''&&ch<='') ans=ans*+ch-'',ch=getchar();
if(last=='-') ans=-ans;
return ans;
} ll n,b,ans=;
struct node
{
ll cost,cow;
}candy[]; bool cmp(node x,node y)
{
return x.cost <y.cost ;
} int main()
{ n=read();b=read();
for(ll i=;i<=n;i++)
{
candy[i].cost =read();
candy[i].cow =read();
} sort(candy+,candy+n+,cmp); for(ll i=;i<=n;i++)
{
if(candy[i].cow==) continue;
if(b==)
{ printf("%lld",ans); return ; }
if(candy[i].cow <=b/candy[i].cost) //直接乘起来会爆炸
{
b-=candy[i].cost *candy[i].cow;
ans+=candy[i].cow;
continue;
}
else
{
ans+=b/candy[i].cost; //不要一个个枚举节省时间
printf("%lld",ans); return ;
}
}
printf("%lld",ans); return ;
}
P2983 [USACO10FEB]购买巧克力的更多相关文章
- 洛谷——P2983 [USACO10FEB]购买巧克力Chocolate Buying
P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...
- 洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying 题解
P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...
- 洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying
购买巧克力Chocolate Buying 乍一看以为是背包,然后交了一个感觉没错的背包上去. #include <iostream> #include <cstdio> #i ...
- 洛谷P2983 [USACO10FEB]购买巧克力Chocolate Buying
题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...
- P2983 [USACO10FEB]购买巧克力Chocolate Buying
题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...
- 【洛谷】P2983 [USACO10FEB]购买巧克力Chocolate Buying(贪心)
题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...
- 洛谷—— P2983 [USACO10FEB]购买巧克力Chocolate Buying
https://www.luogu.org/problem/show?pid=2983 题目描述 Bessie and the herd love chocolate so Farmer John i ...
- [USACO10FEB]购买巧克力Chocolate Buying 【假背包真贪心】 By cellur925
题目传送门 继续dp刷题计划,看到这道题,第一眼感觉不就是显然的完全背包嘛.把背包打完要开始填充数组大小的时候成为了mengbier,发现数据极大,达到了1e18.显然这不是一道平凡的背包题目. 于是 ...
- [USACO10FEB]购买巧克力Chocolate Buying
题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...
随机推荐
- python 实现服务树结构化
1. 所有服务树数据 tree_list = [{'id': 1, 'pid': 0, 'name': '1211', 'path': '1211', 'leaf': 0, 'type': 0}, ...
- JMeter 问题整理
1. 远程连接出现错误: java.net.connectexception: connection refused: connect -解决办法: 编辑jmeter.bat(windows OS), ...
- KMP算法查找字符串
假设长字符串为t,短字符串为p.为了进行KMP匹配,首先需要计算字符串p的next数组,后面实现了计算该数组的函数void KmpGenNext(char* p, int* next).对于”abca ...
- vs调试时,不显示局部变量
为了测试一个函数的返回值,就在某个函数里加了一个局部变量,调试却不显示所添加变量的信息. 你一定设置成了release 模式.改为debug就可以了. 比较弱智的问题,mark一下.
- centos7编译安装php7.3
处理问题 解决php configure: error: Cannot find ldap libraries in /usr/lib.错误 cp -frp /usr/lib64/libldap* ...
- Ubuntu 16.04安装docker详细步骤
1. 卸载之前的旧版本 sudo apt-get remove docker docker-engine docker-ce docker.io 2. 更新apt包 sudo apt-get upda ...
- haproxy和keepalived的理解(转载)
关于此两者,随便google一下都会有大量的文章,因此这里也不用介绍如何来进行安装和配置了.主要从一个整体方案的角度来描述什么要这样做,以及这样做所解决的问题. 所有的系统,都是先经历一个单台机器搞所 ...
- 题解 最长上升序列2 — LIS2
最长上升序列2 - LIS2 Description 已知一个 1 ∼ N 的排列的最长上升子序列长度为 K ,求合法的排列个数. Input 输入一行二个整数 N , K ( K ≤ N ≤ 15) ...
- 对Webpack 应用的研究-----------------引用
对大多数 Web 应用来说,页面性能直接影响着流量.这是一个经常为我们所忽视的事实.用户长时间的等待流失的不仅仅是跳出率.转化率,还有对产品的耐心和信赖.很多时候我们没有意识到性能问题,那是因为平常开 ...
- 51 Nod1042 数字0到9的数量
1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19 ...