C - C

CodeForces - 991C

题目内容:

After passing a test, Vasya got himself a box of n 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 k, same for all days. After that, in the morning he eats k candies from the box (if there are less than k candies in the box, he eats them all), then in the evening Petya eats 10% of the candies remaining in the box. If there are still candies left in the box, the process repeats — next day Vasya eats k candies again, and Petya — 10% of the candies left in a box, and so on.

If the amount of candies in the box is not divisible by 10, Petya rounds the amount he takes from the box down. For example, if there were 97 candies in the box, Petya would eat only 9 of them. In particular, if there are less than 10 candies in a box, Petya won't eat any at all.

Your task is to find out the minimal amount of k that can be chosen by Vasya so that he would eat at least half of the n candies he initially got. Note that the number k must be integer.

Input
The first line contains a single integer n (1≤n≤1018) — the initial amount of candies in the box. Output
Output a single integer — the minimal amount of k that would allow Vasya to eat at least half of candies he got. Example
Input
68
Output
3
Note
In the sample, the amount of candies, with k=3, would change in the following way (Vasya eats first): 68→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 39 candies, while Petya — 29.

题意:有n颗糖果,V和P来吃这些糖果,一开始,V选择一个整数k,以后每天都相同数量k个。之后,早上他从盒子里吃k个糖果(如果盒子里少于k个糖果,他就全部吃掉),然后晚上P吃掉盒子里剩余的10%的糖果。如果盒子里还剩下糖果,则重复此过程-第二天,V再吃k糖果,P-盒子里剩下的糖果的10%,依此类推。如果包装盒中的糖果数量不能被10整除,则P将其从包装盒中取出的糖果数量向下取整。例如,如果盒子里有97个糖果,那么P只吃其中的9个。特别是,如果一个盒子里的糖果少于10,那么P根本不会吃任何糖果。任务是找出V可以选择的最小数量的k,以便他可以吃掉最初获得的n糖果的至少一半。注意数字k为整数。

思路:运用二分法

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n,l,r,mid,x,tmp,cnt,sum,ans=1e18;
cin>>n;
l=0,r=n;
while(l<=r){
sum=0;
mid=(l+r)/2;
if(mid==0) {l+=1;continue;}//如果枚举出0,那么肯定是不可以的,所以l+1
tmp=n;
while(1){
if(tmp>=mid) tmp-=mid,sum+=mid;//如果剩余的大于mid,那么减去mid,否则,直接取完
else sum+=tmp,tmp=0;
tmp-=tmp/10;
x=n%2?(n/2)+1:n/2;//n/2不是整数,那么加一(相当与向上取整)
if(sum>=x) break;//如果a取的蛋糕比n的一半还要多,那么这个mid是可以的
if(tmp==0) {sum=-1;break;}
}
if(sum!=-1) r=mid-1,ans=min(ans,mid);//如果mid满足条件,更新ans
else l=mid+1;
}
cout<<ans<<endl;
return 0;
}

最新文章

  1. Openfire阶段实践总结
  2. 数据库大作业--由python+flask
  3. LightOJ1079 Just another Robbery(DP)
  4. 夺命雷公狗---微信开发13----获取access_token
  5. Tomcat:IOException while loading persisted sessions: java.io.EOFException解决手记
  6. D.T SOFTWARE (1) 软件架构直播答疑课程
  7. 多条件判断语句case
  8. BaseAdapter中重写getview的心得以及发现convertView回收的机制
  9. QT皮肤系统的动态切换
  10. About Undefined Behavior[译文]
  11. Golang网络库中socket阻塞调度源码剖析
  12. JAVA知识的相关积累--用于自己以后查找
  13. 201621123057 《Java程序设计》第12周学习总结
  14. xftp上传文件失败,执行程序发现磁盘满了:No space left on device
  15. 下拉框 JComboBox,文本框JTextField
  16. CSS盒子模型(Box Model)
  17. 手把手在Ubuntu上面安装Docker
  18. openssl RSA密钥格式PKCS1和PKCS8相互转换
  19. textarea 分割
  20. (转)Word插入图片显示不全怎么办

热门文章

  1. 云原生数据库 TDSQL-C 产品概述、产品优势、应用场景
  2. Qt 自定义事件
  3. AtomicStampedReference AtomicReference解决CAS机制中ABA问题
  4. RabbitMQ之消息模式2
  5. 基本ServletWEB项目
  6. vue 动态ip配置,避免重复打包
  7. JAVA反序列化漏洞基础原理
  8. 放码来战!HMS Core线上Codelabs挑战赛正式开始
  9. 【MySQL】MySQL基础(SQL语句、约束、数据类型)
  10. CentOS8部署tftp