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
Copy
4 6 2
output
Copy
2
input
Copy
3 10 3
output
Copy
4
input
Copy
3 6 1
output
Copy
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.
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,k;
int judge(ll mid)
{
ll sum=0;
if(k>=mid) //左边的枕头比床少,枕头数从左边递减至1
{
sum+=(1+mid)*mid/2;//求和范围:1-mid
sum+=(k-mid);//再加上空出来的床上的枕头(初值即为1)
}
if(k<mid) {//左边的枕头比床多,求和范围为1-k
sum+=((mid-k+1)+mid)*k/2;
}
if(k+(mid-1)<=n) {//右边的枕头比床少,求和范围:1-mid
sum+=(1+mid)*mid/2;
sum+=n-(k+(mid-1));//加上空出来的床上的枕头
}
if(k+(mid-1)>n) {//右边的枕头比床多,求和范围:k-n
sum+=(mid+(mid-(n-k)))*(n-k+1)/2;
}
if((sum-mid)<=m)
return 1;
else
return 0;
}
int main()
{
cin>>n>>m>>k;
ll l=1,r=m,mid,ans;
while(l<=r)
{
mid=(l+r)/2;
if(judge(mid))
{
l=mid+1;ans=mid;
}
else
r=mid-1;
}cout<<ans<<endl;
return 0;
}