C. Candies(二分)

C. Candies
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

After passing a test, Vasya got himself a box of nn candies. He decided to eat an equal amount of candies each morning until there are no more candies. However, Petya also noticed the box and decided to get some candies for himself.

This means the process of eating candies is the following: in the beginning Vasya chooses a single integer kk, same for all days. After that, in the morning he eats kk candies from the box (if there are less than kk candies in the box, he eats them all), then in the evening Petya eats 10%10% of the candies remaining in the box. If there are still candies left in the box, the process repeats — next day Vasya eats kk candies again, and Petya — 10%10% of the candies left in a box, and so on.

If the amount of candies in the box is not divisible by 1010, Petya rounds the amount he takes from the box down. For example, if there were 9797 candies in the box, Petya would eat only 99 of them. In particular, if there are less than 1010 candies in a box, Petya won't eat any at all.

Your task is to find out the minimal amount of kk that can be chosen by Vasya so that he would eat at least half of the nn candies he initially got. Note that the number kk must be integer.

Input

The first line contains a single integer nn (1n10181≤n≤1018) — the initial amount of candies in the box.

Output

Output a single integer — the minimal amount of kk that would allow Vasya to eat at least half of candies he got.

Example
input
Copy
68
output
Copy
3
Note

In the sample, the amount of candies, with k=3k=3, would change in the following way (Vasya eats first):

686559565148444137343128262321181714131096633068→65→59→56→51→48→44→41→37→34→31→28→26→23→21→18→17→14→13→10→9→6→6→3→3→0.

In total, Vasya would eat 3939 candies, while Petya — 2929.

题意:给出一个数n,表示有n块蛋糕,有两个人a,b。a每次可以取k块蛋糕(如果剩下的蛋糕不足k,则一次性取完),b每次取当前蛋糕的十分之一(如果不是整数的话,向下取整,例如,有9块蛋糕的话,那么b取0块)求一个最小的k,使得a所取的蛋糕至少大于n的一半。

思路:二分枚举k然后判断是否满足条件即可。(搞不懂的是,我用ceil和floor函数错了,自己手写就对了,emmm)

#include "iostream"
#include "algorithm"
using  namespace std;
typedef long long ll;
int main()
{
    ll n,l,r,mid,x,tmp,cnt,sum,ans=1e18;
    cin>>n;
    l=0,r=n;
    while(l<=r){
        sum=0;
        mid=(l+r)>>1;
        if(mid==0) {l+=1;continue;}//如果枚举出0,那么肯定是不可以的,所以l+1
        tmp=n;
        while(1){
            if(tmp>=mid) tmp-=mid,sum+=mid;//如果剩余的大于mid,那么减去mid,否则,直接取完
            else sum+=tmp,tmp=0;
            tmp-=tmp/10;
            x=n%2?(n/2)+1:n/2;//n/2不是整数,那么加一(相当与向上取整)
            if(sum>=x) break;//如果a取的蛋糕比n的一半还要多,那么这个mid是可以的
            if(tmp==0) {sum=-1;break;}
        }
        if(sum!=-1) r=mid-1,ans=min(ans,mid);//如果mid满足条件,更新ans
        else l=mid+1;
    }
    cout<<ans<<endl;
    return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80790373