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;
}

CF991C Candies的更多相关文章

  1. CF991C Candies 二分 第十五

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

  2. 【题解】CF991C Candies

    题面传送门 解决思路 看到 \(10^{18}\) 的范围,我们可以想到二分答案.只要对于每一个二分出的答案进行 \(check\) ,如果可行就往比它小的半边找,不可行就往比它大的半边找. 以下是 ...

  3. 题解合集 (update on 11.5)

    收录已发布的题解 按发布时间排序. 部分可能与我的其他文章有重复捏 qwq . AtCoder for Chinese: Link ZHOJ: Link 洛谷 \(1\sim 5\) : [题解]CF ...

  4. 【POJ2886】Who Gets the Most Candies?-线段树+反素数

    Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...

  5. poj 3159 Candies 差分约束

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 Descrip ...

  6. Who Gets the Most Candies?(线段树 + 反素数 )

    Who Gets the Most Candies? Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d &am ...

  7. poj---(2886)Who Gets the Most Candies?(线段树+数论)

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10373   Acc ...

  8. poj3159 Candies(差分约束,dij+heap)

    poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...

  9. HDU 5127 Dogs' Candies

    Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...

随机推荐

  1. 201871010116-祁英红《面向对象程序设计(java)》第十周学习总结

    博文正文开头格式:(2分) 项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://ww ...

  2. 201871010109-胡欢欢《面向对象程序设计(java)》第一周学习总结

    <面向对象程序设计(java)>第一周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 ...

  3. Java四个关键字 this super final static

    一.this ​ 关键字主要有三个应用: this调用本类中的属性,也就是类中的成员变量: this调用本类中的其他方法: this调用本类中的其他构造方法初始化对象,调用时要放在构造方法的首行. 引 ...

  4. 创建组件的方法,组件的props属性、state属性的用法和特点,父子组件传值,兄弟组件传值

    1.创建组件的方法   函数组件   class组件 1.1 函数组 无状态函数式组件形式上表现为一个只带有一个 `render()` 方法的组件类,通过函数形式或者 `ES6` 箭头 `functi ...

  5. jquery延迟加载

    jquery实现图片延时加载,实现原理:不设置img的src地址,把地址存在img的alt中,当img标签出现在可视区域,alt值传给src.为避免看到替换文本alt,把字体的颜色设置为背景的颜色,如 ...

  6. vscode源码分析【一】从源码运行vscode

    安装git,nodejs和yarn 安装Python27,3.x版本的不行,确保它在你的环境变量里: 安装gulp npm install --global gulp-cli 安装windows bu ...

  7. laravel依赖注入浅析

    laravel容器包含控制反转和依赖注入,使用起来就是,先把对象bind好,需要时可以直接使用make来取就好. 通常我们的调用如下. $config = $container->make('c ...

  8. js中的NaN,isNaN与Number.isNaN的区别,如何判断一个值严格等于NaN

    在JavaScript的数字类型Number中,我们最常使用的大概是整数类型与浮点数类型,但除这两者外,还有个特殊的存在NaN,为什么NaN!==NaN?我们如何判断一个值是否等于NaN呢?这篇文章好 ...

  9. 参数检查(@property)

    绑定属性时,如果直接把属性暴露出去,虽然写起来很简单,但无法对参数进行检查,导致属性被随便修改 因此,可以通过在类内定义get()获取属性值,定义set()对属性值进行设定并对设定值进行检查 但通过定 ...

  10. CPU参数指标说明

    %user %user表示CPU一共花了多少比例的时间运行在用户态空间或者说是用户进程(running user space processes) 典型的用户态空间程序有:Shells.数据库.web ...