Codeforces 760B Frodo and pillows【贪心+二分】

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 n, m 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.


题目大意:

给你N个床,M个枕头,要求你至少每个床分配一个枕头,并且保证每相邻的两个床的枕头数相差绝对值不超过1.

问最多能够在第k个床放多少个枕头。


1、首先我们考虑如果在第K个床放置最多数目的枕头,那么很明显,如果在第k个床上放置了tmp个枕头,那么对应要在k-1和k+1床上至少放置tmp-1个枕头,在k-2,k+2床上至少放置tmp-2个枕头。依次类推,并且保证每个床至少一个枕头,如果枕头有剩余,那么此时随便放置都能够满足第K个床上放置tmp个枕头的条件。

那么我们考虑枚举一个数tmp,使得找到最大满足条件的tmp;


2、但是直接枚举&&贪心显然是会超时的,那么考虑到其单调性:tmp越大,那么剩余的枕头数就越少,那么tmp合法的几率就越小,那么根据这个单调性,我们对tmp进行二分枚举,并且过程记录可行解,输出最后一次记录下来的tmp可行解即可。


Ac代码:


#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
ll n,m,k;
ll Slove(ll mid)
{
    ll sum=0;
    if(k-(mid-1)>1)
    {
        sum+=k-(mid-1)-1;
        sum+=(1+mid)*mid/2;
    }
    if(k-(mid-1)==1)
    {
        sum+=(1+k)*k/2;
    }
    if(k-(mid-1)<1)
    {
        sum+=(mid-(k-1)+mid)*k/2;
    }
    if(k+(mid-1)<n)
    {
        sum+=n-(k+(mid-1));
        sum+=(1+mid)*mid/2;
    }
    if(k+(mid-1)==n)
    {
        sum+=(1+mid)*mid/2;
    }
    if(k+(mid-1)>n)
    {
        sum+=(mid-(n-k)+mid)*(n-k+1)/2;
    }
    if(sum-mid<=m)return 1;
    else return 0;
}
int main()
{
    while(~scanf("%I64d%I64d%I64d",&n,&m,&k))
    {
        ll ans=0;
        ll l=1;
        ll r=m;
        while(r-l>=0)
        {
            ll mid=(l+r)/2;
            if(Slove(mid)==1)
            {
                ans=mid;
                l=mid+1;
            }
            else r=mid-1;
        }
        printf("%I64d\n",ans);
    }
}









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值