CF991C Candies
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的更多相关文章
- CF991C Candies 二分 第十五
Candies time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- 【题解】CF991C Candies
题面传送门 解决思路 看到 \(10^{18}\) 的范围,我们可以想到二分答案.只要对于每一个二分出的答案进行 \(check\) ,如果可行就往比它小的半边找,不可行就往比它大的半边找. 以下是 ...
- 题解合集 (update on 11.5)
收录已发布的题解 按发布时间排序. 部分可能与我的其他文章有重复捏 qwq . AtCoder for Chinese: Link ZHOJ: Link 洛谷 \(1\sim 5\) : [题解]CF ...
- 【POJ2886】Who Gets the Most Candies?-线段树+反素数
Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- Who Gets the Most Candies?(线段树 + 反素数 )
Who Gets the Most Candies? Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d &am ...
- poj---(2886)Who Gets the Most Candies?(线段树+数论)
Who Gets the Most Candies? Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 10373 Acc ...
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- HDU 5127 Dogs' Candies
Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...
随机推荐
- sed命令总结
目录 1.概述 2.查 1.打印整行(一或多) 2.正则打印包含关键字的行 2.增 3.删 4.改 5.后向引用 6.结合 7.练习 我叫张贺,贪财好色.一名合格的LINUX运维工程师,专注于LINU ...
- day90_11_12
一.路由与正则. 1.当函数中需要参数,而不在路由中匹配的时候需要使用default方法: @app.route('/index/<testreg("\d+"):nid> ...
- AcWing 30. 正则表达式匹配 (剑指OFFER leetcode 10)
题目描述请实现一个函数用来匹配包括’.’和’*’的正则表达式. 模式中的字符’.’表示任意一个字符,而’*’表示它前面的字符可以出现任意次(含0次). 在本题中,匹配是指字符串的所有字符匹配整个模式. ...
- Log4j2之ThreadContext
简介 系统中使用log4j2作为日志系统,然而在高并发的情况下,多次请求的日志参杂在一起,要跟踪某个用户一次的请求操作所有日志是很麻烦的.幸运的是log4j中有相应的解决方案. NDC和MDC NDC ...
- java之运算符的优先级
优先级 运算符 结合性 1 () [] 从左往右 2 ! +(正) -(负) ++ -- 从右往左 3 * / % 从左往右 4 << >> >>> 从左往 ...
- Python连载43-current中的map函数、xml文件
一.current中的map函数 1.map(fn,*iterable,timeout=None) (1)跟map函数相类似(2)函数需要异步执行(3)timeout代表超时时间 (4)map和sub ...
- IT兄弟连 Java语法教程 标识符和关键字
Java语言也和其它编程语言一样,使用标识符作为变量.对象的名字.也提供了一系列的关键字用以实现特别的功能.本小节将详细介绍Java语言的标识符和关键字等内容. 1.分隔符 Java语言里的分号“;” ...
- 洛谷 P4999(数位DP)
###洛谷 P4999 题目链接 ### 题目大意:给你一个区间,求这段区间中所有数的,数位上的,数字之和. 分析: 这题与 洛谷 P2602 相似,稍微改一下就可以了. 求出 0 ~ 9 的个数,然 ...
- copy_from/to_user详解
参考:http://www.wowotech.net/memory_management/454.html 宋大侠的文章精彩,郭大侠的评论也精彩. 结论简单摘录如下: 无论是内核态还是用户态访问合法的 ...
- JQuery操作attr、prop、val()/text()/html()、class属性
1.1 arr操作 设置单个属性 // 第一个参数:需要设置的属性名 // 第二个参数:对应的属性值 // $obj.attr(name, value); // 用法举例. $('img').at ...