• CodeForces760B


    B. Frodo and pillows

    time limit per test:1 second
    memory limit per test:256 megabytes
    input:standard input
    output:standard output

    n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.

    Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?

    Input

    The only line contain three integers nm and k (1 ≤ n ≤ m ≤ 109, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.

    Output

    Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.

    Examples

    input

    4 6 2

    output

    2

    input

    3 10 3

    output

    4

    input

    3 6 1

    output

    3

    Note

    In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.

    In the second example Frodo can take at most four pillows, giving three pillows to each of the others.

    In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.

     以k位置为中心,枕头数量向两侧递减。二分搜索答案。

     1 //2017.01.31
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     int n, m, k;
    11     while(cin>>n>>m>>k)
    12     {
    13         m -= n;
    14         long long lp = k-1, rp = n-k;
    15         long long l = 0, r = m, mid, ans;
    16         while(l<=r)
    17         {
    18             mid = (l+r)>>1;
    19             long long tmp = mid;
    20             if(mid>=lp)tmp += (mid*2-lp-1)*lp/2;
    21             else tmp += (mid-1)*mid/2;
    22             if(tmp > m){r = mid-1;continue;}
    23             if(mid>=rp)tmp += (mid*2-rp-1)*rp/2;
    24             else tmp += (mid-1)*mid/2;
    25             if(tmp < m){
    26                 ans = mid+1;
    27                 l = mid+1;
    28             }
    29             else if(tmp > m)r = mid-1;
    30             else{
    31                 ans = mid+1;
    32                 break;
    33             }
    34         }
    35         cout<<ans<<endl;
    36     }
    37 
    38     return 0;
    39 }
  • 相关阅读:
    [LeetCode] 1072. Flip Columns For Maximum Number of Equal Rows
    [LeetCode] 1730. Shortest Path to Get Food
    [LeetCode] 1005. Maximize Sum Of Array After K Negations
    [LeetCode] 1286. Iterator for Combination
    [LeetCode] 390. Elimination Game
    [LeetCode] 1940. Longest Common Subsequence Between Sorted Arrays
    [LeetCode] 794. Valid TicTacToe State
    [LeetCode] 1162. As Far from Land as Possible
    [LeetCode] 2022. Convert 1D Array Into 2D Array
    [LeetCode] LeetCode 2021勋章
  • 原文地址:https://www.cnblogs.com/Penn000/p/6359202.html
Copyright © 2020-2023  润新知