P2983 [USACO10FEB] Chocolate Buying S

文章讲述了USACO竞赛中的一道题目,涉及如何在有限预算内使用贪心算法和排序策略,使最多数量的奶牛获得喜欢的巧克力。问题转化为求解在给定价格和数量的巧克力种类中,如何分配预算以满足最多的奶牛需求。
摘要由CSDN通过智能技术生成

[USACO10FEB] Chocolate Buying S

题面翻译

贝茜和其他奶牛们都喜欢巧克力,所以约翰准备买一些送给她们。奶牛巧克力专卖店里有 N N N 种巧克力,每种巧克力的数量都是无限多的。

每头奶牛只喜欢一种巧克力,调查显示,有 C i C_i Ci 头奶牛喜欢第 i i i 种巧克力,这种巧克力的售价是 P i P_i Pi

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

题目描述

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

The Bovine Chocolate Store features N ( 1 ≤ N ≤ 100 , 000 ) N (1 \le N \le 100,000) N(1N100,000) kinds of chocolate in essentially unlimited quantities. Each type i of chocolate has price P i ( 1 ≤ P i ≤ 1 0 18 ) P_i (1 \le P_i \le 10^{18}) Pi(1Pi1018) per piece and there are C i ( 1 ≤ C i ≤ 1 0 18 ) C_i (1 \le C_i \le 10^{18}) Ci(1Ci1018) cows that want that type of chocolate.

Farmer John has a budget of B ( 1 ≤ B ≤ 1 0 18 ) B (1 \le B \le 10^{18}) B(1B1018) 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 50 50 to spend on 5 5 5 different types of chocolate. A total of eleven cows have various chocolate preferences:

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

Obviously, FJ can’t purchase chocolate type 5 5 5, since he doesn’t have enough money. Even if it cost only 50 50 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 1 1 chocolate of type 2 2 2 for 1 × 1 1 \times 1 1×1 leaving 50 − 1 = 49 50-1=49 501=49, then purchase 3 3 3 chocolate of type 1 1 1 for 3 × 5 3 \times 5 3×5 leaving 49 − 15 = 34 49-15=34 4915=34, then purchase 2 2 2 chocolate of type 4 4 4 for 2 × 7 2 \times 7 2×7 leaving 34 − 14 = 20 34-14=20 3414=20, then purchase 2 2 2 chocolate of type 3 3 3 for 2 × 10 2 \times 10 2×10 leaving 20 − 20 = 0 20-20=0 2020=0.

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

输入格式

Line 1 1 1: Two space separated integers: N N N and B B B.

Lines 2 … N + 1 2\ldots N+1 2N+1: Line i i i contains two space separated integers defining chocolate type i i i: P i P_i Pi and C i C_i Ci.

输出格式

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

样例 1

样例输入 1

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

样例输出 1

8

题解部分

输出输入的意思

由于这个翻译不行,所以需要看英文 . . .
1 1 1行:输入两个数为 N N N B . B. B.
2... N + 1 2...N+1 2...N+1行,第 i i i行有两个数,第一个为巧克力的售价 P i P_i Pi,第二个数为喜欢第 i i i种巧克力的奶牛数 C i . C_i. Ci.

思路

既然已经明白了意思,就开始构思了
第一眼,这不是 d p dp dp ? ? ?然后又想了想拉开了旁边的算法 . . .
哦,贪心 ! ! !哦,排序后贪心 ! ! !
既然有排序的话结构体是肯定要用的 . . .
思路
首先,对于一种巧克力,我们肯定尽可能的满足奶牛的需求 . . .
既然这样那肯定是从喜欢便宜巧克力的奶牛开始呐,于是贪心就来了 . . .
我们首先对数据进行处理,从小到大进行排序
然后对于奶牛尽可能的满足他们,当遍历到无法满足奶牛时,就是答案了,因为如果连便宜巧克力都买不了,那贵的一定也买不了 . . .

Accepted Code

于是代码就有了

#include<bits/stdc++.h>
using namespace std;
const int N=100001;
struct no{
	unsigned long long c,p;
}q[N];
unsigned long long n,b,ans;
bool px(no a,no b){
	return a.p<b.p;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n>>b;
	for(unsigned long long i=1;i<=n;i++){
		cin>>q[i].p>>q[i].c;
	}
	sort(q+1,q+1+n,px);
	for(unsigned long long i=1;i<=n;i++){
		unsigned long long xx=q[i].p*q[i].c;
		if(xx<=b){
			ans+=q[i].c;//满足他们
			b-=xx;//不要忘记付钱
		}else{
			ans+=b/q[i].p;//尽可能满足
			break;
		}
	}
	cout<<ans;
	return 0;
}

注意:一定要开 unsigned long long

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值