CodeForces 287B Pipeline
思路:二分答案,时间复杂度O(nlgn).
若个数为x,那么算出这种情况下提供的水管的最大值和最小值与n比较即可,注意x个分离器需要减去x-1个水管。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long int
using namespace std;
LL n, k;
LL calSum(LL ba, LL i){
return ((2*ba + i - 1) * i)/2;
}
LL bin_search(LL l, LL r){
LL ans = 0x7fffffff;
while(l <= r){
LL mid = (l + r) >> 1;
LL up = min(n + mid - 1, k);
LL tmp1 = calSum(2, mid), tmp2 = calSum(up - mid + 1, mid);
if(tmp1 <= n + mid - 1 && tmp2 >= n + mid -1){
ans = min(ans, mid);
r = mid - 1;
}
else if(tmp1 > n + mid - 1) r = mid - 1;
else if(tmp2 < n + mid - 1) l = mid + 1;
}
if(ans != 0x7fffffff) return ans;
else return -1;
}
int main(){
while(cin >> n >> k){
if(n == 1) printf("0\n");
else cout << bin_search(1, k-1) << endl;
}
return 0;
}
CodeForces 287B Pipeline的更多相关文章
- CodeForces 287B Pipeline (水题)
B. Pipeline time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- [贪心,dp] Educational Codeforces Round 71 (Rated for Div. 2) C. Gas Pipeline (1207C)
题目:http://codeforces.com/contest/1207/problem/C C. Gas Pipeline time limit per test 2 seconds memo ...
- Codeforces 1207C Gas Pipeline (dp)
题目链接:http://codeforces.com/problemset/problem/1207/C 题目大意是给一条道路修管道,相隔一个单位的管道有两个柱子支撑,管道柱子高度可以是1可以是2,道 ...
- redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作
前段时间在做用户画像的时候,遇到了这样的一个问题,记录某一个商品的用户购买群,刚好这种需求就可以用到Redis中的Set,key作为productID,value 就是具体的customerid集合, ...
- Building the Testing Pipeline
This essay is a part of my knowledge sharing session slides which are shared for development and qua ...
- Scrapy:为spider指定pipeline
当一个Scrapy项目中有多个spider去爬取多个网站时,往往需要多个pipeline,这时就需要为每个spider指定其对应的pipeline. [通过程序来运行spider],可以通过修改配置s ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
随机推荐
- Raphael:JS矢量图形库
Raphael:JS矢量图形库 2016-08-29 http://dmitrybaranovskiy.github.io/raphael/
- Node.js:JavaScript脚本运行环境
Node.js https://nodejs.org/ 2016-08-03
- c语言贪吃蛇
思路:函数gotoxy(x,y)使光标移植屏幕的x,y坐标(屏幕左上角为0,0),用来绘制蛇和界面,color()函数用来设置绘制的颜色.设有snakelong节,第i节蛇的x坐标为x[i],y坐标为 ...
- WebService 学习总结
一.概念 Web Web应用程序 Web服务( Web Serivce), SOAP, WSDL, UDDI .Net 框架 ASP.net IIS C#, 代理(委托) 二.实践 1.创建WebSe ...
- UIViewCotroller 的生命周期函数
Viewcontroller 的所有生命周期函数 重写时 一定要先写 父类 方法 就是(super +生命周期函数) LoadView ViewDidLoad ViewDidUnload: 在iOS ...
- nodejs基础安装
安装Nodejs需要从官网上下载一个最新的安装包,运行.我这里是win764位系统. 下载版本6.5.0 由于去外国的镜像上下载东西比较慢,淘宝为我们准备了国内的镜像.我们需要安装国内镜像的使用工具. ...
- linux(centos)如何查看文件夹大小
参考http://zhidao.baidu.com/link?url=OrfDgdHvyA1pSDAy6ql-IgPBWtvcS5AR9bc543zTr1hLIDfCd42nYtNBplAl2pHvM ...
- javascript和jquery动态创建html元素
1.javascript创建元素 创建select var select = document.createElement("select"); elect.opti ...
- ubuntu下的翻译软件goldendict
转自ubuntu下的翻译软件 看着一些API虽然能看懂一个大概,但总想知道每个单词的意思.问题是英语水平有限,所以只能来找一些翻译软件,像windows下来用的有道估计是不行了(也没去试到定行不行), ...
- 如何从软硬件层面提升 Android 动画性能?
若是有人问如何解决动画性能不佳的问题,Dan Lew Codes 总会反问:你是否使用了硬件层? 动画放映过程中每帧画面可能都要重绘.如果使用视图层,,渲染过的视图可以存入离屏缓存以待将来重用,而无需 ...