【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 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 ≤ 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个人(包括Frodo)在Frodo家里过夜,家里有n张床和m个枕头,每个人都至少一张床和一个枕头,但是每个人都想得到尽可能多的枕头,但是如果有任何一个人的枕头至少比他的邻居少两个,那么就会受伤。(对应那句but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.)Frodo睡在第k个位置(k<=n),问:在没有人受伤的情况下,Frodo最多能得到多少个枕头。

解题报告:

    这题如果构造的话,情况就太多了,,但是我们可以枚举枕头数啊,因为当主人公的枕头数定下来之后,就很好得到每一次的最优构造了,就是一个简单的数学求和公式了。代码写的很冗长,但是思路很简单。我只是分了情况(在两边和不在两边)。

AC代码:

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll n,m,k;
bool ok1(ll x) {ll res = 0;if(n < x) res = ((x-n+1) + x) * n / 2;else res = (1+x)*x/2;return res <= m;
}
bool ok2(ll x) {ll res1,res2;if(k < x) res1 = ((x-k+1)+x)*k/2;else res1 = (1+x)*x/2;if(n-k+1 < x) res2 = ((x-(n-k+1)+1)+x)*(n-k+1)/2;else res2 = (1+x)*x/2;return res1 + res2 - x <= m;
}
int main()
{cin>>n>>m>>k;//n人 m枕头 在第k个 if(n == m) {printf("1");return 0;}m=m-n;//默认每个人有一个ll l = 0,r = m;ll ans = 0;ll mid = (l+r)>>1;if(k == 1 || k == n) {k=1;while(l <= r) {mid = (l+r)>>1;if(ok1(mid)) {ans=mid;l=mid+1;}else r=mid-1;}}else {while(l<=r) {mid = (l+r)>>1;if(ok2(mid)) {ans = mid;l=mid+1;}else r=mid-1;}}printf("%lld\n",ans+1);return 0 ;}

总结:

  注意一个细节就是ok2函数中构造的时候,中间那条边会被计算两次,举个例子

  样例:

   4 8 2

   应该输出3,结果输出了2。就是因为,x=3的时候,本来用不到m块枕头,但是你重复计算了一次,所以就多算了枕头数,所以就返回了false了、、、

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441402.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

eclipse wsdl2java_使用Eclipse的wsdl2java工具

一、用Eclipse调用Axis的wsdl2java1.在eclipse里面新建一个项目或已有的项目&#xff1b;2.导入activation.jaraxis.jarcommons-discovery.jarcommons-logging-1.0.3.jarjaxrpc.jarsaaj.jarwsdl4j-1.5.2.jar包3右击你的工程&#xff0d;Run As&#xff0d;Run...&#xff0d;右…

【POJ - 2785】4 Values whose Sum is 0 (二分,折半枚举)

题干&#xff1a; The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a b c d 0 . In the following, we assume that all lists have the same…

java好的博客_推荐5个万博爆款Java开源博客,是我目前用过最好用的博客系统

1.OneBlog一个简洁美观、功能强大并且自适应的Java博客&#xff0c;使用springboot开发&#xff0c;前端使用Bootstrap&#xff0c;支持移动端自适应&#xff0c;配有完备的前台和后台管理功能。功能简介多种编辑器、自动申请友情链接、百度推送、评论系统、权限管理、SEO、实时…

【UVALive - 3126】Taxi Cab Scheme (二分图,最小路径覆盖)

题目大意&#xff1a; 有n个出车安排&#xff0c;一辆车能接到这个安排的条件是&#xff1a;1、这辆车第一次发车&#xff1b;2、这辆车接了上一个安排&#xff0c;回到这个安排的起点的时间正好是这个安排的前一分钟或者更早 解题报告&#xff1a; 建图然后跑最小路径覆盖。…

java await signal_Java中的await()/signal()用法

二、方法await()/signal()在JDK5.0以后&#xff0c;JAVA提供了新的更加健壮的线程处理机制&#xff0c;包括了同步、锁定、线程池等等&#xff0c;可以实现更小粒度上的控制。await()和signal()就是其中用来同步的两种方法&#xff0c;功能基本上和wait()/notify()相同&#xf…

【HDU - 1083 】Courses (二分图)

题干&#xff1a; Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions:…

java 主备切换_keepalived 实现 Java 服务的高可用(主备切换)

前言本文要说的是基于 keepalived 实现两台服务器之间的主备切换&#xff0c;从而实现 Java 服务的高可用。keepalived 的原理不多做介绍&#xff0c;自行搜索了解&#xff0c;keepalived 的安装部署请参考 keepalived 的安装及使用 。个人建议不要沉迷于 死扣 和 理解 原理&am…

【HDU - 2444】The Accomodation of Students(二分图判断 + 匈牙利算法求最大匹配)

题干&#xff1a; There are a group of students. Some of them may know each other, while others dont. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other. Now you are given all pairs of studen…

最长上升子序列 java_最长上升子序列 O(nlogn)解法 (java)

最长递增子序列问题&#xff1a;在一列数中寻找一些数&#xff0c;这些数满足&#xff1a;任意两个数a[i]和a[j]&#xff0c;若i 设dp[i]表示以i为结尾的最长递增子序列的长度&#xff0c;则状态转移方程为&#xff1a; dp[i] max{dp[j]1}, 1<j 这样简单的复杂度为O(n^2)&a…

C++关于引用的注意事项 总结知识点

对函数的引用&#xff1a; #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #def…

中介者模式java_图解Java设计模式之中介者模式

智能家庭项目1)智能家庭包括各种设备&#xff0c;闹钟、咖啡机、电视机、窗帘等2)主人要看电视时&#xff0c;各个设备可以协同工作&#xff0c;自动完成看电视的准备工作&#xff0c;比如流程为 &#xff1a;闹铃响起 - 》咖啡机开始做咖啡 -》窗帘自动落下 -》电视机开始播放…

python 自动驾驶线性识别路段

python视觉库 OpenCV:OpenCV是一个开源的计算机视觉库,提供了丰富的图像处理和计算机视觉算法。它可以用于图像处理、对象识别、特征提取、图像分割等。 Matplotlib:Matplotlib是一个绘图库,可用于创建高质量的二维图表和绘图。它提供了类似于MATLAB的绘图接口,使用户可以…

【CodeForces - 202A】LLPS (思维,字符串)

题干&#xff1a; This problems actual name, "Lexicographically Largest Palindromic Subsequence" is too long to fit into the page headline. You are given string s consisting of lowercase English letters only. Find its lexicographically largest p…

elementui表格宽度适应内容_elementui表格中的列怎么实现自适应列宽

elementui表格中的列怎么实现自适应列宽发布时间&#xff1a;2020-12-28 14:58:04来源&#xff1a;亿速云阅读&#xff1a;53作者&#xff1a;Leah这期内容当中小编将会给大家带来有关elementui表格中的列怎么实现自适应列宽&#xff0c;文章内容丰富且以专业的角度为大家分析和…

【POJ - 2398】Toy Storage (计算几何,二分找位置,叉积,点和直线的位置关系)

题干&#xff1a; Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza is rebellious and obeys his parents by simply throwing …

【POJ - 1269 】Intersecting Lines (计算几何,直线间的位置关系)

题干&#xff1a; We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of …

java servlet 转发和重定向_JavaWeb(一)Servlet中乱码解决与转发和重定向的区别

前言前面其实已经把Servlet中所有的内容都介绍完了&#xff0c;这篇讲补充一点乱码和重定向与转发之间的区别&#xff01;一、request请求参数出现乱码问题1.1、get请求1)乱码示例get请求的参数是在url后面提交过来的&#xff0c;也就是在请求行中。结果&#xff1a;Servlet_de…

【POJ - 3304 】Segments(计算几何,思想转化,直线和线段相交)

题干&#xff1a; Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common. Input Input begins with a…

java调用lingo_使用Lingo增强JMS

虽然activemqjencks的jms轻量级解决方案已经很好地在psa中work了&#xff0c;尤其spring的JmsTemplate使得代码更简单&#xff0c;但是还是存在问题。问题来自暑期做psa的时候&#xff0c;linke突然提出要求&#xff0c;需要MDP返回些处理信息&#xff0c;比如处理结果、异常&a…

jsk Star War (线段树维护区间最小最大值 + 二分)

Description 公元20XX年&#xff0c;人类与外星人之间的大战终于爆发。 现有一个人类军团&#xff0c;由n名士兵组成&#xff0c;第i个士兵的战斗力值对应一个非负整数ai (1 \leq i \leq n1≤i≤n)。 有一天&#xff0c;某个战力爆表的外星人NaN单独向地球人宣战&#xff0c…