zoukankan      html  css  js  c++  java
  • B. Sport Mafia 二分

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Each evening after the dinner the SIS's students gather together to play the game of Sport Mafia.

    For the tournament, Alya puts candies into the box, which will serve as a prize for a winner. To do that, she performs nn actions. The first action performed is to put a single candy into the box. For each of the remaining moves she can choose from two options:

    • the first option, in case the box contains at least one candy, is to take exactly one candy out and eat it. This way the number of candies in the box decreased by 11;
    • the second option is to put candies in the box. In this case, Alya will put 11 more candy, than she put in the previous time.

    Thus, if the box is empty, then it can only use the second option.

    For example, one possible sequence of Alya's actions look as follows:

    • put one candy into the box;
    • put two candies into the box;
    • eat one candy from the box;
    • eat one candy from the box;
    • put three candies into the box;
    • eat one candy from the box;
    • put four candies into the box;
    • eat one candy from the box;
    • put five candies into the box;

    This way she will perform 99 actions, the number of candies at the end will be 1111, while Alya will eat 44 candies in total.

    You know the total number of actions nn and the number of candies at the end kk. You need to find the total number of sweets Alya ate. That is the number of moves of the first option. It's guaranteed, that for the given nn and kk the answer always exists.

    Please note, that during an action of the first option, Alya takes out and eats exactly one candy.

    Input

    The first line contains two integers nn and kk (1n1091≤n≤109; 0k1090≤k≤109) — the total number of moves and the number of candies in the box at the end.

    It's guaranteed, that for the given nn and kk the answer exists.

    Output

    Print a single integer — the number of candies, which Alya ate. Please note, that in this problem there aren't multiple possible answers — the answer is unique for any input data.

    Examples
    input
    Copy
    1 1
    
    output
    Copy
    0
    input
    Copy
    9 11
    
    output
    Copy
    4
    input
    Copy
    5 0
    
    output
    Copy
    3
    input
    Copy
    3 2
    
    output
    Copy
    1
    Note

    In the first example, Alya has made one move only. According to the statement, the first move is always putting one candy in the box. Hence Alya ate 00 candies.

    In the second example the possible sequence of Alya's actions looks as follows:

    • put 11 candy,
    • put 22 candies,
    • eat a candy,
    • eat a candy,
    • put 33 candies,
    • eat a candy,
    • put 44 candies,
    • eat a candy,
    • put 55 candies.

    This way, she will make exactly n=9n=9 actions and in the end the box will contain 1+211+31+41+5=111+2−1−1+3−1+4−1+5=11 candies. The answer is 44, since she ate 44 candies in total.

     

    题意:n次操作,每次操作可以选择往盒子里放 i 个糖果(i <n),也可以在盒子里拿一个糖果吃掉,要求当n次操作之后盒子里恰好有k个糖果,问在n次操作中有几次是吃糖果

    题解:二分枚举吃糖果的次数x,(0<=x<=n-1,因为第一次一定是放糖果)

     

    #include<iostream>
    #include<algorithm>
    #include<string>
    using namespace std;
    typedef long long ll; 
    ll n,k;
    ll find(ll x)
    {
        ll sum=0;
        for(int i=1;i<=n-x;i++)
            sum=sum+i;
        if(sum-x>k)
            return 1;
        else
            return 0;
    }
    int main()
    {
        while(cin>>n>>k)
        {
            ll l=0,r=n-1,mid;
            while(l<=r)
            {
                mid=l+(r-l)/2;
                if(find(mid))
                    l=mid+1;
                else
                    r=mid-1;
            }
            cout<<l<<endl;
        }
        return 0;
    }

     

     

  • 相关阅读:
    DevExpress控件经验集合
    Oracle 11g基础
    NoSuchMethodException问题总结
    设计模式学习笔记十四:适配器模式
    设计模式学习笔记十三:模板方法模式
    设计模式学习笔记十二:访问者模式
    设计模式学习笔记十一:观察者模式
    【亲述】Uber容错设计与多机房容灾方案
    Linux 动态监听进程shell
    玩具:加减法验证码(1+?=4)
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11208085.html
Copyright © 2011-2022 走看看