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

2020.1.30--vj补题的更多相关文章

  1. LCCUP 2020 秋季编程大赛 补题

    果然是力扣杯,难度较于平时周赛提高了不少,个人感觉最后两题并不太容易QAQ LCP 18.早餐组合 #二分思想 题目链接 题意 你获得了每种主食的价格,及每种饮料的价格,你需要选择一份主食和一份饮料, ...

  2. QFNU-ACM 2020.04.05个人赛补题

    A.CodeForces-124A (简单数学题) #include<cstdio> #include<algorithm> #include<iostream> ...

  3. 2021-5-15 vj补题

    C - Win or Freeze CodeForces - 151C 题目内容: You can't possibly imagine how cold our friends are this w ...

  4. 2020.12.3--vj个人赛补题

    A Vasya studies music.He has learned lots of interesting stuff. For example, he knows that there are ...

  5. Technocup 2020 - Elimination Round 1补题

    慢慢来. 题目册 题目 A B C D tag math strings greedy dp 状态 √ √ √ √ //∅,√,× 想法 A. CME res tp A 题意:有\(n\)根火柴,额外 ...

  6. 2021.5.22 vj补题

    A - Marks CodeForces - 152A 题意:给出一个学生人数n,每个学生的m个学科成绩(成绩从1到9)没有空格排列给出.在每科中都有成绩最好的人或者并列,求出最好成绩的人数 思路:求 ...

  7. 2020.10.30--vj个人赛补题

    D - D CodeForces - 743A Vladik is a competitive programmer. This year he is going to win the Interna ...

  8. 2020.10.23-vj个人赛补题

    B - B Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consistin ...

  9. 2020.10.16--vj个人赛补题

    D - Drinks Choosing Old timers of Summer Informatics School can remember previous camps in which eac ...

  10. 2020.10.9--vj个人赛补题

    B - A Tide of Riverscape 题意:给出一组字符串,由'0','1',' . '组成,' . '可以换成 0或1,判断第 i  个和第 i+p 个字符是否可以不相等,如果可以则输出 ...

随机推荐

  1. 分享几个下载豆瓣资源的chrome插件

    最近chrome终于以4.69%的市场占有率击败firefox成为中国第二大浏览器.(第一当然是争霸宇宙的IE了) 虽然chrome官方应用程序商店有不少豆瓣的辅助插件,但大多没什么用.属于蛋疼插件. ...

  2. Markdown Sublime flowchart.js 流程图

    先亮出来一个 flowchart.js 的 Sample 给 Sublime 安装 MarkdownPreview,这个不必多说了 Sublime Text -> Preferences -&g ...

  3. 编程读写CAD文件验证

    背景 B/S应用系统,根据用户上传数据:业务数据和CAD坐标数据,经过一系列运筹算法运算后,输出一批坐标数据,作为给用户的规划结果.此时需要方便直观的给用户展示坐标数据.可选方式有两个: web页面画 ...

  4. 机械硬盘换到SSD后系统引导报错代码0xc000000e

    由于机械硬盘IO不够用,系统使用起来非常的缓慢,特意购买了新的SSD进行了替换.机械硬盘的IO在70左右,SSD的IO在1000-4000左右指普通消费SSD. 由于不想安装系统,就直接把机械硬盘的数 ...

  5. Pytest系列(3) - setup和teardown的详细使用

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 用过unittest的童鞋都 ...

  6. WEB漏洞——文件上传

    有关文件上传的知识 为什么文件上传存在漏洞 上传文件时,如果服务端代码未对客户端上传的文件进行严格的验证和过滤就容易造成可以上传任意文件的情況,包括上传脚本文件(asp.aspx.php.jsp等格式 ...

  7. awk的执行方式

    https://blog.csdn.net/fengyuanye/article/details/82858863 awk执行有三种形式: 1.直接以命令行来执行,        语法形式为:awk  ...

  8. crontab 语法和最快速的学习

    1.Cron 时间表语法 # ┌───────────── 分钟 (0 - 59) # │ ┌───────────── 小时 (0 - 23) # │ │ ┌───────────── 月的某天 ( ...

  9. PHP的Sodium加密扩展函数了解

    这是本次加密扩展系列的最后一篇文章,也是我们要学习了解的最后一个 PHP 加密扩展.Sodium 出现的目的也是为了代替 Mcrypt 这个原来的加密扩展.在 PHP7.2 之后,Mcrypt 已经被 ...

  10. css 限制字数

    text-overflow: ellipsis; overflow: hidden; 不过需设置宽高