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

题目描述

Bessie and the herd love chocolate so Farmer John is buying them some.

The Bovine Chocolate Store features N (1 <= N <= 100,000) kinds of chocolate in essentially unlimited quantities. Each type i of chocolate has price P_i (1 <= P_i <= 10^18) per piece and there are C_i (1 <= C_i <= 10^18) cows that want that type of chocolate.

Farmer John has a budget of B (1 <= B <= 10^18) that he can spend on chocolates for the cows. What is the maximum number of cows that he can satisfy? All cows only want one type of chocolate, and will be satisfied only by that type.

Consider an example where FJ has 50 to spend on 5 different types of chocolate. A total of eleven cows have various chocolate preferences:

Chocolate_Type Per_Chocolate_Cost Cows_preferring_this_type 1 5 3

2 1 1

3 10 4

4 7 2

5 60 1

Obviously, FJ can‘t purchase chocolate type 5, since he doesn‘t have enough money. Even if it cost only 50, it‘s a counterproductive purchase since only one cow would be satisfied.

Looking at the chocolates start at the less expensive ones, he can purchase 1 chocolate of type #2 for 1 x 1 leaving 50- 1=49, then purchase 3 chocolate of type #1 for 3 x 5 leaving 49-15=34, then purchase 2 chocolate of type #4 for 2 x 7 leaving 34-14=20, then purchase 2 chocolate of type #3 for 2 x 10 leaving 20-20= 0.

He would thus satisfy 1 + 3 + 2 + 2 = 8 cows.

贝西和其他奶牛们都喜欢巧克力,所以约翰准备买一些送给她们。奶牛巧克力专卖店里

有N种巧克力,每种巧克力的数量都是无限多的。每头奶牛只喜欢一种巧克力,调查显示,

有Ci头奶牛喜欢第i种巧克力,这种巧克力的售价是P。

约翰手上有B元预算,怎样用这些钱让尽量多的奶牛高兴呢?

输入输出格式

输入格式:

  • Line 1: Two space separated integers: N and B
  • Lines 2..N+1: Line i contains two space separated integers defining chocolate type i: P_i and C_i

输出格式:

  • Line 1: A single integer that is the maximum number of cows that Farmer John can satisfy

输入输出样例

输入样例#1:

5 50
5 3
1 1
10 4
7 2
60 1

输出样例#1:

8

题目大意:n头奶牛,每个奶牛有喜欢的巧克力,巧克力有价钱。问B元钱最多满足多少牛。

  题解:简单贪心,每次购买价格最低的巧克力。

  代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define LL long long
using namespace std;

LL n,b,ans;
struct W{
    LL p,c;
    bool operator < (const W &a)const{return p<a.p;}
}q[100005];

inline LL read(){
    char ch=getchar();LL x=0,f=1;
    for(;!isdigit(ch);ch=getchar())if(ch==‘-‘)f=-1;
    for(;isdigit(ch);ch=getchar())x=x*10+ch-‘0‘;
    return x*f;
}

int main(){
    n=read();b=read();
    for(int i=1;i<=n;i++)q[i].p=read(),q[i].c=read();
    sort(q+1,q+n+1);
    for(int i=1;i<=n;i++){
        LL k=b/q[i].p;
        if(k==0)break;
        LL g=min(k,q[i].c);
        b-=g*q[i].p;
        ans+=g;
    }
    cout<<ans<<endl;
    return 0;
} 
时间: 2024-08-25 09:13:30

洛谷P2983 [USACO10FEB]购买巧克力Chocolate Buying的相关文章

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

https://www.luogu.org/problem/show?pid=2983 题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Store features N (1 <= N <= 100,000) kinds of chocolate in essentially unlimited quantities. Each type i of

洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving

题目描述 Farmer John is distributing chocolates at the barn for Valentine's day, and B (1 <= B <= 25,000) of his bulls have a special cow in mind to receive a chocolate gift. Each of the bulls and cows is grazing alone in one of the farm's N (2*B <=

洛谷——P2984 [USACO10FEB]给巧克力Chocolate Giving

https://www.luogu.org/problem/show?pid=2984 题目描述 Farmer John is distributing chocolates at the barn for Valentine's day, and B (1 <= B <= 25,000) of his bulls have a special cow in mind to receive a chocolate gift. Each of the bulls and cows is graz

洛谷1455 搭配购买(并查集)

洛谷1455 搭配购买 本题地址:http://www.luogu.org/problem/show?pid=1455 题目描述 明天就是母亲节了,电脑组的小朋友们在忙碌的课业之余挖空心思想着该送什么礼物来表达自己的心意呢?听说在某个网站上有卖云朵的,小朋友们决定一同前往去看看这种神奇的商品,这个店里有n朵云,云朵已经被老板编号为1,2,3,……,n,并且每朵云都有一个价值,但是商店的老板是个很奇怪的人,他会告诉你一些云朵要搭配起来买才卖,也就是说买一朵云则与这朵云有搭配的云都要买,电脑组的你觉

洛谷P1108 低价购买[DP | LIS方案数]

题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它.买的次数越多越好!你的目标是在遵循以上建议的前提下,求你最多能购买股票的次数.你将被给出一段时间内一支股票每天的出售价(2^16范围内的正整数),你可以选择在哪些天购买这支股票.每次购买都必须遵循“低价购买:再低价购买”的原则.写一个程序计算最大购买次数. 这里是某支股票的价格清单: 日期 1 2

[USACO10FEB]吃巧克力Chocolate Eating

题目:洛谷P2985. 题目大意:有n块巧克力要吃d天,并且只能按顺序吃.一块巧克力有一个开心值,吃了就能增加开心值.一个人初始开心值为0,且每天早上开心值变为原来的一半.问如何吃巧克力才能使开心值最小的一天开心值最大(每天都按吃完巧克力后计算),且需要输出方案. 解题思路:最大化最小值问题,用二分答案. 贪心地扫描,对于一个答案,如果开心值不到这个答案,就一直吃巧克力即可. 最后输出吃的方案时也用此种贪心法. 注意如果最后巧克力没有吃完,则在最后一天全部吃掉. C++ Code: #inclu

洛谷P2982 [USACO10FEB]慢下来Slowing down(线段树 DFS序 区间增减 单点查询)

To 洛谷.2982 慢下来Slowing down 题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows conveniently numbered 1..N move from the barn to her private pasture. The pastures are organized as a tree, with the barn being on pasture 1. Exactly N-1 cow

luogu P2985 [USACO10FEB]吃巧克力Chocolate Eating

题目描述 Bessie拿到了N (1 <= N <= 50,000)块巧克力.她决定想个办法吃掉这些巧克力,使得它在吃巧克力的这段时间里,最不开心的一天尽可能的开心.并且一共吃D (1 <= D <= 50,000)天. 每块巧克力有一个开心值H_i (1 <= H_i <= 1,000,000),当某天你吃下那块巧克力时,你将获得那块巧克力的开心值.每一天的开心值是所有当天吃掉的巧克力的总开心值之和.每天晚上Bessie睡觉之后,它的开心值会减半.也就是说,比如昨天B

洛谷1108 低价购买

题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它.买的次数越多越好!你的目标是在遵循以上建议的前提下,求你最多能购买股票的次数.你将被给出一段时间内一支股票每天的出售价(2^16范围内的正整数),你可以选择在哪些天购买这支股票.每次购买都必须遵循“低价购买:再低价购买”的原则.写一个程序计算最大购买次数.这里是某支股票的价格清单:日期  1  2