CodeForces - 991C Candies(二分)

题干:

给定糖果数n。
Vasya每天早上起来吃i颗糖果,Petya每天晚上吃现有糖果数的十分之一(向下取整)(如果现有糖果数小于10则不吃)。
求Vasya 每天至少要吃多少糖果,才能在糖果被全部吃完时至少吃了一半的糖果。
1<=n<= 10 18 {10}^{18}

思路:

虽然给定的n有1e18,但因为不是多组输入,所以还是可以用二分(o(lg(n)))来暴力的。
因为打表没找出啥规律
注意下n=1的情况和二分的边界

#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
ll n;
bool chick(ll x)
{
	ll t=n,sum=0;
	while(t)
	{
		if(t>x)
		{
			t-=x;
			sum+=x;
		}
		else 
		{
			sum+=t;
			t=0;
		}
		if(t>=10)
			t-=(t/10);
	}
	//printf("%lld\n",sum);
	if(sum>=(n+1)/2)
		return true;
	else 	
		return false; 
}
int main()
{
	scanf("%lld",&n);
	if(n==1)
		printf("1\n");
	else
	{
		ll l=1,r=n,mid,ans=n;
		while(l<=r)
		{
			mid=(l+r)/2;
			//printf("%lld %lld\n%lld\n",l,r,mid);
			if(chick(mid))
			{
				r=mid-1;
				ans=min(ans,mid);
			}
			else	
				l=mid+1;
		}
		printf("%lld\n",ans);
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42279796/article/details/89526683