cf#342-A-Guest From the Past-贪心

70 篇文章 0 订阅

http://codeforces.com/contest/625/problem/A

题意给n块钱买牛奶


塑料盒牛奶价格 a

玻璃盒牛奶价格 b


回收玻璃盒的价格 c

问你最多能买多少盒牛奶


贪心,看 a 便宜还是 d=b-c便宜

如果a便宜,就先尽可能买a,剩下的钱买b

否则,先尽可能买b,最后买a


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const __int64 inf=2147483647;
const double pi=acos(-1.0);
double eps=0.000001;
__int64 min(__int64 a,__int64 b)
{return a<b?a:b;}
__int64 max(__int64 a,__int64 b)
{
	return a<b?b:a;
}
__int64 maxx=0;

	__int64 n,a,b,c; 
int main()
{    
	scanf("%I64d",&n);
	scanf("%I64d%I64d%I64d",&a,&b,&c);
	__int64 d=b-c;
__int64 cun=0;
	if(d<a)  //如果买b的代价(d)比较小,则贪心买多一点b
	{
		if (n>b)
		{
			cun+= (n-b)/d; //先留下一个b,以免出现无法购买的情况
		n=b+(n-b)%d;
		}
		if (n>=b)
		{
			cun++; //买下最后一个b
			n-=d;
		}
			cun+=n/a;	//剩下的都去买a
	}
	else		//买a的代价比较小,贪心买a
	{
		cun+=n/a;
		n=n%a;	
		if (n>b)	//剩余的钱如果可以买,则按上面的方法,尽可能买b
		{
			cun+= (n-b)/d; 
		n=b+(n-b)%d;
		} 
		if (n>=b)
		{
			cun++;
			n-=d;
		}
	}

	 
 	  printf("%I64d\n",cun);
	return 0;
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值