CodeForces - 287B-Pipeline(二分)
Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only one pipe with flowing water. Besides, Vova has several splitters.
A splitter is a construction that consists of one input (it can be connected to a water pipe) and x output pipes. When a splitter is connected to a water pipe, water flows from each output pipe. You can assume that the output pipes are ordinary pipes. For example, you can connect water supply to such pipe if there's water flowing out from it. At most one splitter can be connected to any water pipe.
The figure shows a 4-output splitter
Vova has one splitter of each kind: with 2, 3, 4, ..., k outputs. Help Vova use the minimum number of splitters to build the required pipeline or otherwise state that it's impossible.
Vova needs the pipeline to have exactly n pipes with flowing out water. Note that some of those pipes can be the output pipes of the splitters.
Input
The first line contains two space-separated integers n and k (1 ≤ n ≤ 1018, 2 ≤ k ≤ 109).
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
Print a single integer — the minimum number of splitters needed to build the pipeline. If it is impossible to build a pipeline with the given splitters, print -1.
Examples
Input
4 3
Output
2
Input
5 5
Output
1
Input
8 4
Output
-1
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
long long n,k;
bool f(long long mid) {
long long s;
s=(k+1)*k/2-mid*(mid-1)/2-(k-mid);
if(mid==k) s=mid;
if(s>=n) return true;
else return false;
}
int main() {
scanf("%lld%lld",&n,&k);
long long sum=0;
sum=k*(k+1)/2-k+1;
long long ans=k-1;
long long l=2,r=k+1;
long long mid=(l+r)/2;
if(sum<n) {
printf("-1\n");
} else if(n==1) {
printf("0");
} else {
while(mid-l>0) {
//mid=(l+r)/2;
if(f(mid)) {
l=mid;
mid=l+(r-l)/2;
} else {
mid=l+(mid-l)/2;
}
}
long long ans=0;
ans=k+1-mid;
printf("%lld\n",ans);
}
return 0;
}
CodeForces - 287B-Pipeline(二分)的更多相关文章
- CodeForces 287B Pipeline
思路:二分答案,时间复杂度O(nlgn). 若个数为x,那么算出这种情况下提供的水管的最大值和最小值与n比较即可,注意x个分离器需要减去x-1个水管. #include<cstdio> # ...
- CodeForces 287B Pipeline (水题)
B. Pipeline time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- codeforces 1165F1/F2 二分好题
Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...
- codeforces 732D(二分)
题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...
- CodeForces 359D (数论+二分+ST算法)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...
- CodeForces 163B Lemmings 二分
Lemmings 题目连接: http://codeforces.com/contest/163/problem/B Descriptionww.co As you know, lemmings li ...
- CodeForces - 589A(二分+贪心)
题目链接:http://codeforces.com/problemset/problem/589/F 题目大意:一位美食家进入宴会厅,厨师为客人提供了n道菜.美食家知道时间表:每个菜肴都将供应. 对 ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- CodeForces - 1059D(二分+误差)
链接:CodeForces - 1059D 题意:给出笛卡尔坐标系上 n 个点,求与 x 轴相切且覆盖了所有给出点的圆的最小半径. 题解:二分半径即可.判断:假设当前二分到的半径是 R ,因为要和 x ...
- Letters CodeForces - 978C (二分)
Time limit4000 ms Memory limit262144 kB There are nn dormitories in Berland State University, they a ...
随机推荐
- 使用chrome的F12开发人员工具进行网页前端性能测试
用chrome访问被测网站,定位到你要测试的动作所在页面或被测页面的前一页.按F12调出开发人员工具,其它的功能我就不介绍了,直接切换到性能选项卡Profiles. 点击start,生成ProFile ...
- listen 72
Warmer Temps May Bollux Botanicals Global warming might seem like a botanical boon. After all, milde ...
- 华为机试题 N阶楼梯的走法,每次走一步或者两步
在Stairs函数中实现该功能: 一个楼梯有N阶,从下往上走,一步可以走一阶,也可以走两阶,有多少种走法? (0<n<=30)<> 例如3阶楼梯有3种走法: 1.1.1 1.2 ...
- ambari 维护模式及reset API 操作
Ambari 的维护模式(Maintenance Mode)介绍 Ambari 提供的 Maintenance Mode,是为了让用户在调试或者维护 Service 的时候,抑制不必要的告警(Aler ...
- [原创]如何解决IE10下CkEditor报 --- SCRIPT5007: 无法获取未定义或 null 引用的属性“toLowerCase”
如何解决IE10下CkEditor报 --- SCRIPT5007: 无法获取未定义或 null 引用的属性“toLowerCase” 错误 如果你的IE是IE10,且不是运行在IE的兼容模式你也许会 ...
- Scala学习——array与arraybuffer的区别(初)
1.由于Array是不可变的,所以不能直接地对其元素进行删除操作,只能通过重赋值或过滤生成新的Array的方式来删除不要的元素. 而ArrayBuffer是可变的,本身提供了很多元素的操作,当然包括删 ...
- python 抓取美丽说店铺的宝贝图片及详细信息的实现(爬虫)
对于页面的抓取,我们使用的是requests,现在大部分的网站都支持动态加载,我们在firefox f12后查找动态的url :http://www.meilishuo.com/aj/shop_lis ...
- Balloon Comes! hdu(小数位数处理)
Balloon Comes! Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): A ...
- 创建maven parent project & module project
1.命令方式: 1)Create the top-level root: mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.arc ...
- Deepin安装wxpython教程
环境: 安装报错: 解决: 1.sudo apt-get install libgtk-3-dev -y 2.sudo apt-get install freeglut3-dev libgstrea ...