Codeforces Round 491 div2 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.

扫描二维码关注公众号,回复: 1737745 查看本文章

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

#include<algorithm>
#include<iostream>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define maxn 103
#define LL long long
#define INF 10000000
using namespace std;
long long n,k;
bool judge(long long x)
{
    long long sd,tp=0,tot=n;
    while(tot>0)
    {
        sd=min(tot,x);
        tot-=sd;
        tp+=sd;
        if(tp*2>=n) return true;
        tot=tot-tot/10;
    }
    if(tp*2>=n) return true;
    return false;
}
/*
题意:一开始有n个糖果,可以任意指定一个数k,
每回合甲拿k个,乙拿剩余的十分之一(取下界),
求最小的k使得甲拿到的总数大于等于二分之一。


一开始看到十的十八次方时足足被吓了一跳,,
满脑子想着数学公式和性质来着,
却忽略了同样很完美的log二分算法。。。。

唉,,,又降分了,,,伤人不轻。
简单分析下如何用二分,,,
首先二分必须在一个答案集合中,这道题很明显二分区间是数值序列,靠下标即可。
那么二分的话符合题目条件的答案必须是连续的不能离散分布,,
还有就是指标函数必须两极分化,,即存在一个临界使得指标函数结果变化,且只存在两种结果。
很明显这道题可以这样处理。

*/
int main()
{
    cin>>n;
    long long l=1,r=n;
    while(l<r)
    {
        long long mid=(l+r)>>1;
        if(judge(mid)) r=mid;
        else l=mid+1;
    }
    cout<<r<<endl;

    return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/80789145