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 ...
随机推荐
- Linux下的打包操作
范例一:将整个 test 目录下的文件全部打包成为 test.tar[python@master ~]$ tar -cvf test.tar test/ ==仅打包,不压缩!test/ ...
- Java学习笔记【三、运算符、表达式、语句】
运算符 算数运算符 /* / % ++ -- 关系运算符 == != > /< >= /<= 位运算符 &(按位与,有0是0,否则1) |(按位或,有1是1,否则0) ...
- 第十章、datetime模块
目录 第十章.datetime模块 一.datetime 模块 第十章.datetime模块 一.datetime 模块 import datetime#返回当前时间 print(datetime.d ...
- 一、传统MVC token验证方式
一.传统MVC验证Token 推荐(https://www.cnblogs.com/xiaobai123/p/9242828.html) 自定义处理Jwt方式 1.新建JWT类 代码如下: using ...
- redis-数据淘汰策略
博客标题:Redis的数据淘汰策略及相关注意事项 配置redis.conf中的maxmemory这个值来开启内存淘汰功能 volatile-lru:从已设置过期时间的数据集(server.db[i]. ...
- regex正则
1 正则表达式基本语法 两个特殊的符号^和$.他们的作用是分别指出一个字符串的开始和结束.例子如下: ^The:表示所有以”The”开始的字符串(”There”,”The cat”等): of des ...
- 在jquery中,使用ajax上传文件和文本
function onSubmit (data) { //获取文本 var callingContent = $('#callingContent').val() // 获取文件 var files ...
- 什么是URL百分号编码?
㈠什么是URL 统一资源定位系统(uniform resource locator;URL)是因特网的万维网服务程序上用于指定信息位置的表示方法. ㈡URL编码 url编码是一种浏览器用来打包表单输入 ...
- left join和right join和inner join
此图仅限于理解他们之间的关系,下面还有举例,例子更好明白. left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录 ...
- Windows:打印为PDF(PDF转换器)
造冰箱的大熊猫@cnblogs 2019/4/17 文中图片可通过点击鼠标右键查看大图 Windows下实现PDF打印的几个办法: 1.Foxit Reader(福昕PDF浏览器) 安装Foxit R ...