CodeForces - 991C Candies(二分)

題幹:

給定糖果數n。
Vasya每天早上起來吃i顆糖果,Petya每天晚上吃現有糖果數的十分之一(向下取整)(如果現有糖果數小於10則不吃)。
求Vasya 每天至少要吃多少糖果,才能在糖果被全部吃完時至少吃了一半的糖果。
1<=n<=1018{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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章