【codeforces 760B】Frodo and pillows
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;
}
【codeforces 760B】Frodo and pillows的更多相关文章
- Codeforces 760B:Frodo and pillows(二分)
http://codeforces.com/problemset/problem/760/B 题意:有n张床m个枕头,每张床可以有多个枕头,但是相邻的床的枕头数相差不能超过1,问第k张床最多能拥有的枕 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- 【2017 Multi-University Training Contest - Team 7】 Euler theorem
[Link]:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1005&cid=765 [Description] 问你a ...
- 词向量 word2vec
看的这一篇的笔记 http://licstar.net/archives/328 看不太懂. 要学的话,看这里吧,这里把一些资料做了整合: http://www.cnblogs.com/wuzhitj ...
- 邮件协议与port
电子邮箱的协议有SMTP.POP2.POP3.IMAP4等.都隶属于TCP/IP协议簇,默认状态下.分别通过TCPport25.110和143建立连接.针对不同的用途和功能,我们在邮件se ...
- 疯狂html5讲义(二):HTML5简的经常使用元素与属性(二):html5新增的通用属性
1.contentEditable属性 true时可直接编辑html元素里面的内容.且具有"可继承"的特点. 编辑后不要刷新页面.否则编辑的内容会丢失,能够通过该元素的innerH ...
- 前端面试题(JavaScript)
(前端面试题大全,持续更新) 箭头函数特点?箭头函数和普通函数的区别 手写懒加载(考虑防抖和重复加载问题) 手写bind(为什么要加预参数,为什么要加new) apply, call, bind ne ...
- Java基础学习总结(53)——HTTPS 理论详解与实践
前言 在进行 HTTP 通信时,信息可能会监听.服务器或客户端身份伪装等安全问题,HTTPS 则能有效解决这些问题.在使用原始的HTTP连接的时候,因为服务器与用户之间是直接进行的明文传输,导致了用户 ...
- Java Web学习总结(4)——HttpServletResponse对象入门
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即然代表请求和响应,那我们 ...
- 洛谷—— P2234 [HNOI2002]营业额统计
https://www.luogu.org/problem/show?pid=2234 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业 ...
- 洛谷 P2693 [USACO1.3]号码锁 Combination Lock
P2693 [USACO1.3]号码锁 Combination Lock 题目描述 农夫约翰的奶牛不停地从他的农场中逃出来,导致了很多损害.为了防止它们再逃出来,他买了一只很大的号码锁以防止奶牛们打开 ...
- js进阶 12 jquery事件汇总
js进阶 12 jquery事件汇总 一.常用事件 页面载入事件 ready() 文档就绪事件(当 HTML 文档就绪可用时) 鼠标事件 click() 触发.或将函数绑定到指定元素的 click 事 ...