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 ...
随机推荐
- 跨平台编译ceres for Android
折腾了几乎一天,记录一下. 最大的坑是官网给出的 1.进入ceres源代码目录下的jni目录 2.EIGEN_PATH="指向eigen库目录,即包含EIGEN文件夹的那个文件夹” ndk- ...
- Shell-使用mkfifo实现多任务并发及并发数控制
以下为代码实现的一个模拟场景:3个生产者,在不断提供服务,处理需求,假设1s处理一个. 20个消费者,在不断消耗供给产品,提交需求,假设3s消耗一个. 情景分析:由于消费者的提交需求能力 和 生产者处 ...
- Python 之 random模块
Python中的random模块用于生成随机数.1.random.random() #用于生成一个0到1的随机浮点数:0<= n < 1.0>>> random.ran ...
- Tomcat设置默认启动项目
Tomcat设置默认启动项目 Tomcat设置默认启动项目,顾名思义,就是让可以在浏览器的地址栏中输入ip:8080,就能访问到我们的项目.具体操作如下: 1.打开tomcat的安装根目录,找 ...
- 关于STM32运行时程序卡在B.处的解决方法
文章转载自:https://blog.csdn.net/u014470361/article/details/78780444 背景: 程序运行时,发现程序卡死在B.处. 解决方法: 程序卡死在B.处 ...
- 四、指定Nginx启动用户
一.nginx指定启动用户 1.参考宝塔的配置 解释:(linux权限管理) 指定用www用户启动nginx,如果你用root启动nginx,万一nginx有漏洞,被提权了,你服务器就GG了 所以指定 ...
- 【计算机网络】网络地址转换NAT
网络地址转换NAT 要知道到每个IP使能的设备都需要一个IP地址.以一个家庭为例,假设当地的ISP已为该家庭分配过一块地址,但是后期家庭中的智能设备增加(智能手机.电脑等),这些都需要IP地址才可上网 ...
- Jmeter (一) 安装
https://blog.csdn.net/ls1792304830/article/details/52718177 一.准备工具 1.Windows 操作系统 2.jmeter 3.JDk 依赖环 ...
- 【javascript】[].slice.call(arguments)的作用
var thisExtends = function () { var args = [].slice.call(arguments).filter(function (item) { return ...
- EntityFrameworkCore
left join var query = from log in logRepository.Table.Include(a => a.User) join user in logReposi ...