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个枕头,要求你至少每个床分配一个枕头,并且保证每相邻的两个床的枕头数相差绝对值不超过1.

问最多能够在第k个床放多少个枕头。

1、首先我们考虑如果在第K个床放置最多数目的枕头,那么很明显,如果在第k个床上放置了tmp个枕头,那么对应要在k-1和k+1床上至少放置tmp-1个枕头,在k-2,k+2床上至少放置tmp-2个枕头。依次类推,并且保证每个床至少一个枕头,如果枕头有剩余,那么此时随便放置都能够满足第K个床上放置tmp个枕头的条件。

那么我们考虑枚举一个数tmp,使得找到最大满足条件的tmp;

2、但是直接枚举&&贪心显然是会超时的,那么考虑到其单调性:tmp越大,那么剩余的枕头数就越少,那么tmp合法的几率就越小,那么根据这个单调性,我们对tmp进行二分枚举,并且过程记录可行解,输出最后一次记录下来的tmp可行解即可。

Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
ll n,m,k;
ll Slove(ll mid)
{ll sum=0;if(k-(mid-1)>1){sum+=k-(mid-1)-1;sum+=(1+mid)*mid/2;}if(k-(mid-1)==1){sum+=(1+k)*k/2;}if(k-(mid-1)<1){sum+=(mid-(k-1)+mid)*k/2;}if(k+(mid-1)<n){sum+=n-(k+(mid-1));sum+=(1+mid)*mid/2;}if(k+(mid-1)==n){sum+=(1+mid)*mid/2;}if(k+(mid-1)>n){sum+=(mid-(n-k)+mid)*(n-k+1)/2;}if(sum-mid<=m)return 1;else return 0;
}
int main()
{while(~scanf("%I64d%I64d%I64d",&n,&m,&k)){ll ans=0;ll l=1;ll r=m;while(r-l>=0){ll mid=(l+r)/2;if(Slove(mid)==1){ans=mid;l=mid+1;}else r=mid-1;}printf("%I64d\n",ans);}
}

Codeforces 760B Frodo and pillows【贪心+二分】相关推荐

  1. 【CodeForces - 760B 】Frodo and pillows (二分题意,注意细节)

    题干: n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row a ...

  2. [SCOI2005]栅栏(贪心+二分+dfs)难度⭐⭐⭐⭐

    [SCOI2005]栅栏(贪心+二分+dfs) P2329 [SCOI2005]栅栏 题目描述 农夫约翰打算建立一个栅栏将他的牧场给围起来,因此他需要一些特定规格的木材.于是农夫约翰到木材店购买木材. ...

  3. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  4. 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

    题目传送门 1 /* 2 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 3 当然有可能两个数和超过p,那么an ...

  5. LIS最长上升子序列详解(动态规划、贪心+二分、树状数组)

    1.摘要: 关于LIS部分,本篇博客讲一下LIS的概念定义和理解,以及求LIS的三种方法,分别是O(n^2)的DP,O(nlogn)的二分+贪心法,以及O(nlogn)的树状数组优化的DP,最后附上几 ...

  6. Frodo and pillows CodeForces - 760B 二分 注意l和r的选择

    以后写l和r的初始值的时候,在不影响合理性的前提下,尽量写大一点 比如下面这个代码,如果r不加以或者l==0就不行 #include <iostream> #include <cst ...

  7. CodeForces - 967D Resource Distribution(贪心+二分+构造)

    题目链接:点击查看 题目大意:给出 n 个机器,每个机器可以处理 a[ i ] 的工作,现在有两个工作需要处理,工作量分别为 x1 和 x2,可以将一个工作分配给 k 个机器同时完成,需要满足: k ...

  8. CodeForces - 1236D Alice and the Doll(贪心+二分+模拟)

    题目链接:点击查看 题目大意:给出一个n*m的矩阵,矩阵中有k个障碍物,在点(1,1)处有一个洋娃娃,洋娃娃每次的行动路线只能是直走或右拐,初始时洋娃娃面朝正右方向,问洋娃娃能否将所有方格都走一遍,并 ...

  9. 【CodeForces - 722D】Generating Sets(二分,贪心)

    题干: You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct posit ...

  10. CodeForces 670D2 Magic Powder - 2(二分+贪心)

    http://codeforces.com/contest/670/problem/D2 简单的二分,二分所有可以做的饼干数,然后遍历就可以啦 #include <iostream> #i ...

最新文章

  1. SQL Server 2005下的分页SQL
  2. python用户名和密码登录函数_基于Python构建用户ID和密码存储查询系统,python,建立,一个,用户名,UserID,Password,的...
  3. DFT的准备(一)(对离散序列的傅里叶分析大总结)
  4. [C++] Lvalue and Rvalue Reference
  5. BootStrap 效果展示
  6. Asp.net中Js、Css文件压缩辅助类
  7. Tomcat--远程Debug以及参数配置调优
  8. hdu-2112 HDU Today(最短路)
  9. imagej边缘提取
  10. vbs整人代码蓝屏_求大量VBS整人代码.
  11. face_alignment库获取人脸图片landmark示例
  12. 以数据为中心的路由协议_腰部零售企业如何以数据中台为中心,加速数字化落地...
  13. Chrome终极全屏模式Kiosk
  14. 如何在Windows上为代码签名创建自签名证书
  15. echarts 地图和柱状图结合(在地图上显示柱状图)
  16. 【无标题】软件企业认定条件(双软企业认定条件2022)
  17. Android Webrtc使用Wifi Direct无法建立P2P连接
  18. 算法系列之九:计算几何与图形学有关的几种常用算法(二)
  19. luoguP4098「HEOI2013」ALO
  20. 华硕笔记本节能证书_有这本证书的人恭喜啦,国家正式要求,企业必备

热门文章

  1. Anroid在应用层实现开机自启
  2. 201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结
  3. win7系统安装SQL Server 2005开发版步骤详解
  4. php eclipse aptana,eclipse 下如何安装 Aptana插件
  5. eureka自我保护机制EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY‘RE NOT
  6. WIN7家庭版升级到旗舰版操作
  7. matlab中clc、clear、close命令以及函数文件
  8. firebug lite for chrome
  9. vivado hls 笔记
  10. 08年度的佳作——《真・恋姫†無双》玩后感(蜀国篇)+AGTH真正提取大法