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个枕头。主人睡在第k个床。分配枕头,每个相邻的床位的枕头数差距不能大于1,给你m个枕头,让你让第k张床的枕头最多。
POINT:
先模拟一下阶梯。让床位1 2 3 4 5 k k-1 k-2 k-3……这样模拟下去。
如果这个数量小于等于m,那么在一起给每个床加一个枕头每次加n。加完就是答案。
如果大于m了,那么说明这个阶梯没那么规律。那么我们模拟跑一遍:
先所有床都给1个枕头。111111,然后给k床一个枕头,然后给k,k-1,k+1一个枕头。让平稳的平地一次次凸起来。
最后得到答案。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <math.h>
#include <map>
using namespace std;
#define LL long long
LL a[100000];
int main()
{
LL n,m,k;
cin>>n>>m>>k;
LL i;
a[1]=0;
for(i=1;i<=40000;i++){
a[i]=a[i-1]+i;
}
LL num=max(k,n-k+1);
if(num==k){
k=n-k+1;
}
LL now;
LL ans;
if(k<40000&&num<40000){
now=a[num]+(a[num-1]-a[num-k]);
if(m>=now){
ans=num+(m-now)/n;
printf("%lld\n",ans);
return 0;
}
}
ans=1;
LL cnt=1;
LL yu=m-n;
if(yu>=1) yu--,ans++;
while(yu){
LL l=k;
LL r=n-k+1;
LL ll=min(l-1,cnt);
LL rr=min(cnt,r-1);
if(ll+rr+1<=yu){
yu-=ll+rr+1;
cnt++;
ans++;
}else{
break;
}
}
printf("%lld\n",ans);
return 0;
}