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个枕头,主人公在第k个位置;每个床上必须要有一个枕头,然后每张床的枕头差值应<=1,比如第5张床有5个枕头,那第4张床和第6张床必须各有4-6个枕头,最后询问主人公所在的床最多能有几个枕头。
题解:二分枚举即可(我的写法是先令主人公床上的枕头减少1,因为每张床的枕头最少必须为1,然后考虑边缘的床是否为0,因为存在这样的情况1 2 3 2 1 ,因为我预处理减少了1,所以在代码里的情况是0 1 2 1 0,如果边缘为0的话,计算枕头的个数公式不一样),但最后输出答案应应考虑mid-1(这里wa了超久)因为二分姿势是l = mid + 1 ,所以可能有l的情况可能已经是最优了,但还是往上+了1,所以应特判。。。
第一次写博客呃呃呃呃,就讲那么多啦`
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#define maxn 120000+5
typedef long long ll;
using namespace std;
ll n,m,k;
bool check(ll x){
x--;
ll ans = n;
if(x-(k-1)<=0){
ans+=(1+x)*x/2;
}
else{
ans+=(x-(k-1)+x)*k/2;
}
if(x-(n-k)<=0){
ans+=(1+x)*x/2;
}
else{
ans+=(x-(n-k)+x)*(n-k+1)/2;
}
ans-=x;
//printf("<%lld>\n",s);
if(ans > m)return false;
else return true;
}
int main() {
scanf("%lld%lld%lld",&n,&m,&k);
ll l = 1,r = 1e9;
ll mid;
while(l<=r){
mid = (l+r)>>1;
//printf("<%d %d>\n",l,r);
if(check(mid))l = mid+1;
else r = mid-1;
}
if(check(mid))printf("%lld",mid);
else printf("%lld",mid-1);
return 0;
}