Frodo and pillows _CF760B

题目

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个霍比特人排成一行准备睡觉,他们有n张床和m个枕头,弗拉多作为主人要给这些人分枕头。每个霍比特人都希望自己有尽可能多的枕头,而每个霍比特人只要比跟他相邻的人少一个以上的枕头(不包括一个),他就会受到伤害,问弗拉多在不会伤害任意一个人的情况下能获得最多多少枕头。

 算法:二分 贪心

代码 

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	long long n,m,k;
	cin>>n>>m>>k;
	long long low=1,high=m,mid=(low+high)/2;
	while(low<=high)
	{
		long long sum=n+mid-1;
		
		
		if(mid-(k-1)>=1) sum+=(2*mid-k-2)*(k-1)/2;
		else sum+=(mid-2)*(mid-1)/2;
		
		if(mid-(n-k)>=1) sum+=(2*mid-3-n+k)*(n-k)/2;
		else sum+=(mid-2)*(mid-1)/2;
			
	
		if(sum>m) {high=mid-1;mid=(high+low)/2;} 
		else {low=mid+1;mid=(high+low)/2;}
	} 
	cout<<mid;


	return 0;
}


 代码解析

 从k这个位置,左右阶梯状散发,但是左右有可能很快就到头了,所以需要判断

代码中的第一个if else就是用来判断左端并且根据情况计算

相应的第二个if  else是判断右边

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cherish_lii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值