【Codeforces 760 B Frodo and pillows】+ 二分
来源:互联网 发布:电信4g网络模式是什么 编辑:程序博客网 时间:2024/04/28 22:54
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.
Codeforces (c) Copyright 201
题意 : 给出 N 张床,M 个枕头,每相邻的床之间的枕头数相差不能大于1,Frodo 将睡在第 K 张床上,问 Frodo 最多能得到的枕头数
思路 : 以Frodo为中间点有 N 个枕头,向两边递减 …… N - 2 , N - 1,N, N - 1, N - 2,…每人至少的到一个,对 Frodo 能得到的枕头数二分
AC代码:
#include<bits/stdc++.h>using namespace std;typedef long long LL;LL N,M,K;LL bc(LL x){ LL sum = 0,cut = 0; if(x - K + 1 > 0) cut = (x + x - K + 1) * K / 2; else cut = (1 + x) * x / 2 + K - x; int p = N - K; if(p){ --x; if(x - p + 1 > 0) sum = (x + x - p + 1) * p / 2; else sum = (1 + x) * x / 2 + p - x; } if(sum + cut <= M) return 1; else return 0;}int main(){ scanf("%lld %lld %lld",&N,&M,&K); LL r = M,l = 1,ans = 1; while(l <= r){ LL b = (l + r) / 2; if(bc(b)) ans = b,l = ++b; else r = --b; } printf("%lld\n",ans); return 0;}
- Codeforces 760B Frodo and pillows 【二分】
- 【Codeforces 760 B Frodo and pillows】+ 二分
- Codeforces 760B Frodo and pillows【贪心+二分】
- codeforces-760-B Frodo and pillows(二分+数学)
- 【codeforces 760B】Frodo and pillows
- [codeforces 760B]Frodo and pillows
- CodeForces 760B Frodo and pillows
- Codeforces 760B Frodo and pillows
- codeforce 760 B Frodo and pillows 二分搜索
- B. Frodo and pillows
- Codeforces Round #393 (Div. 2) -B. Frodo and pillows(贪心)
- Codeforces Round #393 (Div. 2) B. Frodo and pillows
- Codeforces Round #393 Frodo and pillows
- Codeforces Round #393 (Div. 2) Frodo and pillows
- CodeForces 760BFrodo and pillows
- codeforces-760【B二分】
- Codeforces 760B (二分)
- codeforces 483b Friends and Presents 二分
- ubuntu16.04下给直接解压免安装的应用程序创建启动器(快捷方式)
- Java集成开发工具
- 批处理合并文件
- leetcode 292 Nim Game
- jvm笔记01:自动内存管理机制
- 【Codeforces 760 B Frodo and pillows】+ 二分
- 树莓派服务器搭建
- Hexo博客搭建
- HDU2510符号三角形(位运算打表)
- 你没中过勒索病毒,不知道备份有多重要
- 栈的压入、弹出序列
- [C++] 基础知识之3、C++中的运算符
- 【LeetCode】 275. H-Index II
- 字符串的排列(字典序)