time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard 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.

【题目链接】:http://codeforces.com/contest/760/problem/B

【题解】



题意:

给你n个床,m个枕头.要求每个床最少分配一个枕头.

同时相邻的床的枕头个数之差要小于等于1;

要求第k张床的枕头数最大;

求这个最大值是多少.

做法:

二分枚举第k张床的枕头数为h;



这里n=7,k=4

可以看到想要让k=4的床的枕头数从1变到2,需要添加红颜色的相应枕头.

想要让k=4的床的枕头数从2变到3,需要添加绿颜色的相应的枕头;

这里从h-1变到h需要添加的枕头数

为(h-2)*2+1;

高度为h的总的枕头数是个等差数列求和问题;

看看需要的枕头数目是不是小于等于m,如果是就表示这个高度可行.继续变大.

但是这里需要注意以下情况;



即(h-2)< k-1或者(h-2)>(n-k)

这两种情况分别会左边多出一部分,右边多出一部分;

需要减掉;

这两个可能的多余部分是一个等差数列求和(首项为1,共差为1,项数为(h-2)-(k-1)和(h-2)-(n-k));



【完整代码】

#include <bits/stdc++.h>
#define LL long long using namespace std; LL n,m,k; bool ok(LL h)
{
LL temp = (2*h-2)*(h-1)/2;
if (h-2<=k-1 && h-2 <= n-k)
return n + temp <= m;
if (h-2>k-1 && h-2 > n-k)
{
temp+=n;
LL temp1 = (1 + h-2-(k-1))*(h-2-(k-1))/2;
LL temp2 = (1 + h-2-(n-k))*(h-2-(n-k))/2;
return temp-temp1-temp2 <= m;
}
if (h-2>k-1 && h-2 <= n-k)
{
temp+=n;
LL temp1 = (1 + h-2-(k-1))*(h-2-(k-1))/2;
return temp-temp1<=m;
}
if (h-2<=k-1 && h-2>n-k)
{
temp+=n;
LL temp2 = (1 + h-2-(n-k))*(h-2-(n-k))/2;
return temp-temp2<=m;
}
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
cin >> n >> m >>k;
LL l = 2,r = 1+m-n;
if (r==1)
puts("1");
else
{
LL ans = 2;
while (l <= r)
{
LL m = (l+r)>>1;
if (ok(m))
{
ans = m,l = m+1;
}
else
r = m-1;
}
cout << ans << endl;
}
return 0;
}

最新文章

  1. Bootstrap学习笔记(一)
  2. linux 多线程 LinuxThreads(转)
  3. MSP430F149学习之路——UART
  4. 用cmd命令合并N个文件
  5. 李洪强iOS开发之自定义cell的使用
  6. jdbc_odbc SQLserver 驱动安装及测试
  7. Nodejs解析HTML网页模块 jsdom
  8. js-json教程从入门到使用
  9. angularjs上传图片插件使用
  10. Win64下编译集成GEOS和Proj4的GDAL
  11. (网页)习惯了CS回车操作人员,操作BS网页表单也是回车666
  12. vue 在.vue文件里监听路由
  13. Linux中如何安装RAR
  14. ssh agent-forward
  15. inotifywait实现目录监控--http://man.linuxde.net/inotifywait
  16. Will Georgia Tech&#39;s $7K online M.S. in computer science program make the grade?
  17. [转帖] Linux 下面栈空间大小的实验
  18. tophat-fusion 鉴定融合基因
  19. 快速切题 cf118A
  20. 如何在微软Windows平台上打造出你的Linux开发环境(转载)

热门文章

  1. 洛谷 P1303 A*B Problem
  2. 学习笔记(一):offset
  3. 【习题 6-10 UVA - 246】10-20-30
  4. CodeVs——T 4919 线段树练习4
  5. ORACLE 11G R2 DG_BROKER 之SWITCH OVER
  6. cocos2dx--vs2012+lua开发环境搭建
  7. Android 软键盘弹出,界面整体上移的问题
  8. 【Codeforces Round #434 (Div. 2) B】Which floor?
  9. python的sorted() 函数
  10. POJ 1270 Following Orders 拓扑排序