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个枕头,其中Frodo在第K个位置,需要输出的是他最多可以有多少个枕头。要求是相邻两个人的枕头差值不能超过1。
解题思路:
本题采用二分的思想。二分枕头数给Frodo,然后从k向左加和,再向右加和,相加和m比较,看是否符合条件。大致的思路就是这样,不过其中有一些小细节,可以看代码自行体会。(里面的对左右侧枕头求和,都是用的等差数列求和公式)
AC代码:

#include<bits/stdc++.h>
using namespace std;
long long n,m,k,l,r,mid,ans;
long long int check(long long x)
{long long sum=0;if(k<=x)//如果左边位置小与枕头数,则用等差数列来求和 sum+=(x+(x-k+1))*k/2;else // 如果左边位置大于枕头数,那就先算等差数列,其余的数量都是1 sum+=(1+x)*x/2+k-x;        long long t=n-k+1;if(t<=x)//右侧与左边道理相同 sum+=(x+(x-t+1))*t/2;elsesum+=(1+x)*x/2+t-x;sum=sum-x;//因为左边的时候算了一次x,右边又算了一次if(sum<=m)return 1;//sum小与m,说明此种情况满足elsereturn 0;
}
int main()
{cin >> n >> m >> k;l=1,r=m;while(l<=r){mid=(l+r)/2;if(check(mid)){l=mid+1;ans=mid;}elser=mid-1;}printf("%lld",ans);return 0;
}

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. Frodo and pillows CodeForces - 760B 二分 注意l和r的选择

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

  3. Codeforces Roud#393

    B. Frodo and pillows time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. DSP-SLAM:具有深度形状先验的面向对象SLAM

    点云PCL免费知识星球,点云论文速读. 文章:DSP-SLAM: Object Oriented SLAM with Deep Shape Priors 作者:Jingwen Wang Martin ...

  5. 如何征服Webpack 4并构建一个出色的React应用

    This article has been outdated with the new release for babel, kindly check the updated article &quo ...

  6. 入门深度学习,先看看三位顶级大牛Yann LeCun、Yoshua Bengio和Geoffrey Hinton的联合综述

    [编者按]深度学习领域的三位大牛Yann LeCun.Yoshua Bengio和Geoffrey Hinton无人不知无人不晓.此前,为纪念人工智能提出60周年,Yann LeCun.Yoshua ...

  7. 中石油计算机职称考试题,2019年职称英语考试中石油历年真题及答案

    I. Vocabulary Directions: There are 20 incomplete sentences in this part. For each sentence there ar ...

  8. linux添加美式键盘,win8\win server 2012添加【中文--美式键盘】

    关于div的居中的问题 (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-01-11) div水平和垂直居中,text-align和vertical-align不起作用,因为标 ...

  9. CVPR2020检测类论文最全汇总:136篇论文方向细分(附代码和论文打包下载)

    为了方便大家进一步的学习,我们对这CVPR中的1467篇论文进行了整理,本次分享的是所有检测类论文,并将它们细分为3D目标检测.人脸检测.动作检测.视频目标检测.文本检测.行人检测等方向,同时附上了相 ...

  10. 《Nature》纪念人工智能60周年专题:深度学习综述

    来源:网络大数据 摘要:本文是<Nature>杂志为纪念人工智能60周年而专门推出的深度学习综述,也是Hinton.LeCun和Bengio三位大神首次合写同一篇文章. 本文是<Na ...

最新文章

  1. Xcode中的NSLog详解
  2. Maven学习总结(七)——eclipse中使用Maven创建Web项目
  3. WCF从理论到实践(15):响应变化
  4. C、C++的学习───思维方式的转变(2)
  5. tensorflow1中如何设置GPU的使用
  6. Oracle 权限介绍及管理
  7. git批量删除没用的远程分支
  8. oracle实验使用游标,Oracle数据库实验-PLSQL游标、过程、函数、包的使用
  9. 配置MySQL以进行ADF开发
  10. 作者:董诚,华中科技大学计算机科学与技术学院硕士生。
  11. 反病毒软件技术简析与探索(2009年5月18日)
  12. Vuebnb 一个用 vue.js + Laravel 构建的全栈应用
  13. MSsql差异备份总结
  14. 【2019数学建模】国赛C题:机场出租车优化问题(原创)
  15. EXCEL 根据超链接直接显示图片
  16. 如何利用后端给的url,前端下载图片、音频、视频等文件
  17. 网页设计作业 / 动漫网页设计作业,网页设计作业 / 动漫网页设计成品,网页设计作业 / 动漫网页设计成品模板下载
  18. 江苏联亚国际展览中心跨境展批发产品B2B门户线上展会平台matchup expo SEO工作日志
  19. 字节跳动byteDance
  20. 手机link.php,PHP中通过HTTP_USER_AGENT判断是否为手机移动终端的函数代码

热门文章

  1. loadrunner 注册破解
  2. flex java blazeds 注解_flex java blazeds 注解
  3. HTML模拟电池页面,Html 电池图标
  4. 什么是云计算,什么是网格计算,两者之间有什么区别?
  5. win2003系统的序列号
  6. win2003企业版sp2序列号
  7. BLE技术知识点大全
  8. Modbus通信协议格式以及功能码
  9. OpenCASCADE:OCCT WebGL 查看器示例
  10. 计算机组策略定时开机脚本,简单几步实现电脑定时开机