奶牛晒衣服

标准的二分答案

传送门(洛谷)

题目描述
在这里插入图片描述

此题一眼看来是可以用贪心思想的,但我主要讲述的是二分答案

二分标准模版:
while(l<=r)
	{
		ll mid=(l+r)/2;
		if(check(mid)) r=mid-1//如果在mid时间内可以,则向右缩短时间(不用我阐述为啥吧)
		else l=mid+1;
	}

每次我门二分在mid时间内能否将所有衣服烘干,具体讲述见注释

/**********************************
*	Problem LOG P1843 奶牛晒衣服
*	Time 2019.3.23
*	User mzg1824_TY
*	Algorithm 二分答案
**********************************/ 
#include<bits/stdc++.h>
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define ll long long
using namespace std;
const int maxn=1e6+10;
ll n,a,b;
ll c[maxn],l=0,r;

template <class t> void read(t &x)
{
	x=0;int f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
	while(isdigit(ch)){x=10*x+ch-'0';ch=getchar();}
	x*=f;
}

void readdata()
{
	read(n),read(a),read(b);
	rep(i,1,n) read(c[i]),r+=c[i];//r的最值就是所有衣服全水分值部自然干
}

bool check(ll x)
{
	ll h=0;
	for(int i=1;i<=n;i++)
	{
		if(c[i]-a*x>0)
		{
			if((c[i]-a*x)%b==0) h+=(c[i]-a*x)%b;//这里我们判断一下,在自然风干后用烘干机是否恰好烘干
			else h+=(c[i]-a*x)/b+1;//否则多用一次
		}
	}
	if(h<=x) return true;
	else return false;
}

void work()
{
	while(l<=r)
	{
		ll mid=(l+r)/2;
		if(check(mid)) r=mid-1;
		else l=mid+1;
	}
	printf("%lld",l);
}

int main()
{
	freopen("input.txt","r",stdin);
	readdata();
	work();
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值