CF991C Candies

洛谷评测传送门

题目描述

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.

输入格式

The first line contains a single integer nn ( 1 \leq n \leq 10^{18}1≤n≤1018 ) — the initial amount of candies in the box.

输出格式

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

题意翻译

Vasya有nn个糖果,在开始的时候 Vasya 选择了一个整数kk,表示他每天会吃kk个糖果,Petya想偷吃一部分糖果,他每天会吃当前数量的10%10%(下取整)的糖果

输出最小的kk,使得Vasya至少吃掉一半的糖果

注意:

若Vasya吃糖果时数量不满kk,则Vasya会全吃掉。

若Petya吃糖果时数量不满1010个,则Petya不会吃糖果

感谢@attack 提供翻译

输入输出样例

输入 #1复制

输出 #1复制

说明/提示

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

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

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

题解:

首先来说一下这道题为什么要用二分来解决。

二分需要满足两种结构:第一个,我们知道解可能存在于一个固定的区间。第二个,这个区间必须具有单调性。

假如我们已知的区间是答案的值域,那么这个二分就叫做二分答案。假如我们已知的区间是我们自己定义的一个序列,那么这个二分就叫二分查找。前提就是必须有序。一开始本蒟蒻真的没有想到二分,还在那傻乎乎地找。通过这道题也是能够发现,让我们在可能范围内确定一个解,尤其是最小解/最大解。二分真的是一个不错的选择。

那么我们来设计这个二分算法:

首先确定二分区间,这道题的二分区间就是\(1-n\),这个应该不用证明...

然后我们确定二分转移的条件:只要按照这个值拿拿拿,拿到的东西满足\(tot>n/2\),那么就是合法的,区间左移,否则右移。

那么就是二分的重头戏:判断函数。

这个依照题意模拟就可以。

对于二分循环的写法,如果还有模糊的,请移步本蒟蒻的这篇博客:

浅谈二分写法

(为了保证二分模板的完整性,我还是喜欢把两个函数分开写...)

代码:

#include<cstdio>
#define int long long
using namespace std;
int n,tmp;
int judge(int mid)
{
int now=n,tot=0;
while(now)
{
if(now<=mid)
{
tot+=now;
break;
}
now-=mid;
tot+=mid;
now-=now/10;
}
return tot;
}
bool check(int mid)
{
if(judge(mid)>=tmp)
return 1;
else
return 0;
}
signed main()
{
scanf("%I64d",&n);
int l=1,r=n;
if(n&1)
tmp=n/2+1;
else
tmp=n/2;
while(l<r)
{
int mid=(l+r)>>1;
if(check(mid))
r=mid;
else
l=mid+1;
}
printf("%I64d",l);
return 0;
}

最新文章

  1. Java—Servlet技术
  2. [WPF实用技巧]如何使WPF的TreeView节点之间有连线
  3. KnockoutJS 3.X API 第一章 简介
  4. 错误:document.getElementById(&quot;userForm&quot;).submit();Object is not a function
  5. iOS开发--Swift 如何完成工程中Swift和OC的混编桥接(Cocoapods同样适用)
  6. POJ 1845 (约数和+二分等比数列求和)
  7. python_os
  8. 十六、Swing高级组件
  9. pt-query-digest用法
  10. hdu 1561 The more, The Better(树形dp,基础)
  11. sysctl命令详解
  12. opacity兼容写法
  13. 配置Tomcat以指定的身份(非root)运行
  14. Problem D: 栈小游戏
  15. Navi.Soft31.产品.微信聊天(永久免费)
  16. Android studio统计项目总行数
  17. 用requests爬取一个招聘网站
  18. 一些常用的mysql语句实例-以后照写2
  19. redis 安装报错
  20. 【Android UI】Android颜色系大全

热门文章

  1. fallowing-travelvue
  2. WPF 快捷键
  3. &lt;Topological Sort&gt; ( 高频, hard) 269
  4. 使用 Hbuilder 连接手机调试移动端项目
  5. window.onload在文档加载完成后执行
  6. Javascript模块化开发1——package.json详解
  7. 【数字图像分析】基于Python实现 Canny Edge Detection(Canny 边缘检测算法)
  8. C# WebClient,HttpClient,WebRequest
  9. python基础(6):基本数据类型一(int、bool、str)
  10. Java生鲜电商平台-SpringCloud微服务架构高并发参数优化实战