Codeforces Round #393 (Div. 2) B. Frodo and pillows

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 nm and k (1 ≤ n ≤ m ≤ 1091 ≤ 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.


二分k位置上的枕头数,找满足条件的最大值,所以在judge时应尽量让他满足条件,所以要贪心找当k位置上枕头数为x时,所有人用的最少总枕头数,小于等于m就满足条件,

最少的总枕头数可以找规律求

举个例子:

k=2,n=6

当k上枕头数为3时,所有人的枕头数

2,3,2,1,1,1

更生动一点:

       *

*     *     *

*     *     *     *     *     *

wa了好多发,最后结束了发现改成longlong就对了,虽然n,m,k的范围都在int内,但judge里求最少的总枕头数可能超过int范围,好智障啊,好菜啊

 #include<stdio.h>
#include<algorithm>
using namespace std;
long long int ans=0,minn,t,n,m,k;
int check(long long int x);
int main()
{
	long long int le,ri,mid,ans;
	scanf("%lld %lld %lld",&n,&m,&k);
	le=1,ri=m;
		while(le<=ri)
		{
			mid=(le+ri)>>1;
			if(check(mid))
			{
				ans=mid;
				le=mid+1;
			}
			else
			ri=mid-1;
		}
		printf("%lld\n",ans);


	return 0;
}
long long int jisuan1(long long int x,long long int y)
{
	return ((1+x)*x/2+y-x);
}
long long int jisuan2(long long int x,long long int y)
{
	return ((2*x+1-y)*y/2); 
}
int check(long long int x)
{
	long long int sum=0;
	if(x<=k)
	{
		sum+=jisuan1(x,k);
	}
	else
	{
		sum+=jisuan2(x,k);
	}
	if((x-1)<=(n-k))
	{
		sum+=jisuan1(x-1,n-k);
	}
	else
	{
		sum+=jisuan2(x-1,n-k);
	}
	if(sum<=m)
	return 1;
	else
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值