Codeforces Round #491 (Div. 2) 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.


二分

#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstdio>
#include <iostream>
#define ll long long
using namespace std;

ll m;
bool work(ll n) {
	ll sum1=0;
	ll sum2=0;
	ll t = m;
	while (t > 0) {
		ll a = n<t?n:t;
		sum1 += a;
		t -= a;
		ll b = t/10;
		sum2 += b;
		t -= b;
	}
	return sum1 >= sum2;
}
int main() {
	ios::sync_with_stdio(false);
	cin >> m;
	ll a=1;
	ll b = m;
	ll res = m;/*
	for (ll i = 1; i <= m; i++) {
		if (work(i)) {
			cout << i << endl;
			break;
		}
	}*/
	while (a < b) {
		ll n = (a + b) / 2;
		if (work(n)) {
			res = n<res?n:res;
			b = n;
			if ((a + b) / 2 == a || (a + b) / 2 == b)
			{
				if (work(a) && a < res)
					res = a;
				if (work(b) && b < res)
					res = b;
				break;
			}
			continue;
		}
		else {
			a = n;
			if ((a + b) / 2 == a || (a + b) / 2 == b)
			{
				if (work(a) && a < res)
					res = a;
				if (work(b) && b < res)
					res = b;
				break;
			}
			continue;
		}
	}
	cout << res << endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值