• 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 o 65 o 59 o 56 o 51 o 48 o 44 o 41 o 37 o 34 o 31 o 28 o 26 o 23 o 21 o 18 o 17 o 14 o 13 o 10 o 9 o 6 o 6 o 3 o 3 o 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;
    }
    
  • 相关阅读:
    偶然闪退的排查
    ANTI-INFLAMMATORY FOODS
    健康餐
    PSORIASIS-7 THINGS TO AVOID
    eczema: improve or reverse your eczema
    VL10 or VL10A前台操作
    装修设计
    春运高速记录 1/15/2020
    rman 备份并异机恢复
    RMAN冷备份、一致性备份脚本
  • 原文地址:https://www.cnblogs.com/fusiwei/p/11754185.html
Copyright © 2020-2023  润新知