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]购买巧克力的更多相关文章

  1. 洛谷——P2983 [USACO10FEB]购买巧克力Chocolate Buying

    P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...

  2. 洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying 题解

    P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...

  3. 洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying

    购买巧克力Chocolate Buying 乍一看以为是背包,然后交了一个感觉没错的背包上去. #include <iostream> #include <cstdio> #i ...

  4. 洛谷P2983 [USACO10FEB]购买巧克力Chocolate Buying

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  5. P2983 [USACO10FEB]购买巧克力Chocolate Buying

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  6. 【洛谷】P2983 [USACO10FEB]购买巧克力Chocolate Buying(贪心)

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  7. 洛谷—— P2983 [USACO10FEB]购买巧克力Chocolate Buying

    https://www.luogu.org/problem/show?pid=2983 题目描述 Bessie and the herd love chocolate so Farmer John i ...

  8. [USACO10FEB]购买巧克力Chocolate Buying 【假背包真贪心】 By cellur925

    题目传送门 继续dp刷题计划,看到这道题,第一眼感觉不就是显然的完全背包嘛.把背包打完要开始填充数组大小的时候成为了mengbier,发现数据极大,达到了1e18.显然这不是一道平凡的背包题目. 于是 ...

  9. [USACO10FEB]购买巧克力Chocolate Buying

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

随机推荐

  1. 【漏洞分析】Discuz! X系列全版本后台SQL注入漏洞

    0x01漏洞描述 Discuz!X全版本存在SQL注入漏洞.漏洞产生的原因是source\admincp\admincp_setting.php在处理$settingnew['uc']['appid' ...

  2. Java并发与多线程

    1. 并发与并行 并发是指某个时间段内,多任务交替处理的能力:并行是指同时处理多任务的能力,多核CPU可以实现并行任务. 并发执行的特点: (1)并发程序间相互制约:程序执行结果的相互依赖以及共享资源 ...

  3. Maxwell平滑升级流程

    1. 同步配置文件 copy之前的配置文件     2  关掉监控程序  如果有的话     3  kill掉之前的maxwell程序     4 查询已经读取到的position位置,然后重启服务 ...

  4. Oracle笔记(八) 复杂查询及总结

    一.复杂查询 1. 列出至少有一个员工的所有部门编号.名称,并统计出这些部门的平均工资.最低工资.最高工资. 1.确定所需要的数据表: emp表:可以查询出员工的数量: dept表:部门名称: emp ...

  5. 移动端css适配

    /* iphoneX.iphoneXs */ @media only screen and (device-width: 375px) and (device-height: 812px) and ( ...

  6. Twitter的支撑架构:扩展网络与存储并提供服务——架构原则:一次性将事情做对,NFL原则 LSM+B+存储替代cassandra

    Twitter工程团队近期提供了Twitter核心技术的演进和扩展的详细资料,这些核心技术支撑了Twitter自营数据中心的系统架构,用于提供社会媒体服务.他们分享的关键经验包括:超越原始规格和需求进 ...

  7. codeforces 576C Points on Plane 相邻两点的欧拉距离

    题意:给出n个点,要求排序后,相邻两点的欧拉距离之和小于等于2.5e9做法:由于0≤ xi, yi ≤ 1e6,所以可以将x<=1000的点分成一份,1000<x<=2000的点分成 ...

  8. MySQL 关于视图的操作

    -- 视图就是一条select 语句 执行后返回结果集,是一种虚拟表,是一个逻辑表 -- 方便操作,减少复杂的SQL语句,增加可读性,更加安全一些 create view demo_view as s ...

  9. “我”这个字的unicode码到底是25105

    “我”这个字的unicode码到底是25105 “我”这个字的unicode码到底是25105 “我”这个字的unicode码到底是25105

  10. [Spring Boot] Aspect Intro

    Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another ...