思路:二分答案,时间复杂度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的更多相关文章

  1. CodeForces 287B Pipeline (水题)

    B. Pipeline time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  2. [贪心,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 ...

  3. Codeforces 1207C Gas Pipeline (dp)

    题目链接:http://codeforces.com/problemset/problem/1207/C 题目大意是给一条道路修管道,相隔一个单位的管道有两个柱子支撑,管道柱子高度可以是1可以是2,道 ...

  4. redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作

    前段时间在做用户画像的时候,遇到了这样的一个问题,记录某一个商品的用户购买群,刚好这种需求就可以用到Redis中的Set,key作为productID,value 就是具体的customerid集合, ...

  5. Building the Testing Pipeline

    This essay is a part of my knowledge sharing session slides which are shared for development and qua ...

  6. Scrapy:为spider指定pipeline

    当一个Scrapy项目中有多个spider去爬取多个网站时,往往需要多个pipeline,这时就需要为每个spider指定其对应的pipeline. [通过程序来运行spider],可以通过修改配置s ...

  7. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  8. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  9. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

随机推荐

  1. 安装Apache Web 服务器软件

    Apache下载地址:http://pan.baidu.com/s/1kTKCEOn 1: 右击安装 2: 3: 4: 5:安装成功: 6:测试在浏览器地址栏输入:localhost; 会出现一个页面 ...

  2. Node.js:JavaScript脚本运行环境

    Node.js https://nodejs.org/ 2016-08-03

  3. ECMAScript 6十大特性

    ES6入门 http://es6.ruanyifeng.com/ ES6排名前十的最佳特性列表 Default Parameters(默认参数) in ES6 Template Literals (模 ...

  4. js实现瀑布流的一种简单方法实例分享

    现在说瀑布流式布局似乎有点晚了,但是每一项技术都是向着“精”和“简”的方向在不断发展,在发展到极致之前,需要一个相当漫长的过程,因此,从这个角度来说,当瀑布流被应用得越来越多的时候,反而更应该讨论它, ...

  5. MVC之重定向

    MVC的重定向主要通过RedirectResult和RedirectToRouteResult实现.很显然,这两个对象都是MVC返回对象ActionResult的两个继承,具体原理不赘述. 这两个方法 ...

  6. 使用Github总结

    1. 使用Git GUI 首先熟悉一下GUI,如下: 第一步,首先将代码fork到自己的版本库下面,如下: 并获取clone URL,如下图: 然后点击GUI克隆已有版本库,如下图: 点击克隆就可以得 ...

  7. can't add foreign key in mysql?

    create table department (dept_name ), building ), budget numeric(,) ), primary key (dept_name) ); cr ...

  8. hdu 1358 period KMP入门

    Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...

  9. C#微信登录-电脑版扫描二维码登录

    像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 一.创建网站应用 在微信开放平台创建一个网站应用 https:// ...

  10. unity3d游戏开发——新手引导

    GUI实现,如下: 按“G”键开始新手引导 代码如下: using UnityEngine; using System.Collections; public class OkButton : GUI ...