Codeforces Round #491 (Div. 2):C. Candies(二分)

本文介绍了一个有趣的算法问题:如何确定Vasya每天从糖果盒中取出的最小数量的糖果,以便他能够消耗至少一半的糖果总量,同时考虑到Petya会吃掉剩余糖果的10%。文章提供了一种通过二分查找来解决该问题的有效方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
68
output
3


题意:有一盒糖罐,里面有n个糖果,Vasya和Petya轮流吃,Vasya先吃且每次固定吃x个(不足x个全吃完),Petya每次吃掉当前的10%(向下取整,可能为0),求最小的x使得Vasya能吃到至少一半的糖果

思路:二分答案,直接模拟检测


#include<stdio.h>
#define LL long long
LL n;
LL Jud(LL x)
{
	LL now, sum;
	now = n, sum = 0;
	while(now)
	{
		if(now<=x)
		{
			sum += now;
			break;
		}
		now -= x;
		sum += x;
		now -= now/10;
	}
	return sum;
}
int main(void)
{
	LL l, r, m;
	scanf("%I64d", &n);
	l = 1, r = n;
	while(l<r)
	{
		m = (l+r)/2;
		if(Jud(m)>=(n+1)/2)
			r = m;
		else
			l = m+1;
	}
	printf("%I64d\n", l);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值