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?
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.
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
4 6 2
2
3 10 3
4
3 6 1
3
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);
}
}