题目链接:http://codeforces.com/problemset/gymProblem/101028/I

I. March Rain
time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output

It is raining again! Youssef really forgot that there is a chance of rain in March, so he didn't fix the roof of his house. Youssef's roof is 1-D, and it contains n holes
that make the water flow into the house, the position of hole i is denoted as xi where
(0 ≤ i < n). Youssef has to put strips at the bottoms of those holes in order to prevent the water from flowing. Let's say
there is a hole in position 4 and another hole in position 6, and Youssef decided to use a strip of length 3 to cover those two holes, then he places the strip from position 4 to 6 (it covers positions 4,5,6) and it covers the two holes. He can buy exactly k strips,
and he must pay a price equal to the longest strip he buys. What is the minimum length l he can choose as the longest strip in order
to keep his house safe?

Input

The input consists of several test cases. The first line of the input contains a single integer T, the number of the test cases. Each
test case consists of two lines: the first line contains two space-separated integers, n and k (1 ≤ k < n ≤ 100000),
denoting the number of the holes in the roof, and the number of the strips he can buy respectively. The second line of the test case contains n integers (x0, x1, ..., xn - 1):
(0 ≤ xi ≤ 109),
denoting the positions of holes (these numbers are given in an increasing order).

Output

For each test case print a single line containing a single integer denoting the minimum length l he can choose in order to buy k strips
(the longest of them is of length l) and cover all the holes in his house using them.

Example
input
3
5 2
1 2 3 4 5
7 3
1 3 8 9 10 14 17
5 3
1 2 3 4 20
output
3
4
2
Note

In the second test case the roof looks like this before and after putting the strips.

题解:

一开始以为是区间覆盖问题。后来想到可以用二分来寻找答案。由于付款金额依照最长长条的长度。所以就把每条长条都想成是最长的。二分长度,然后判断当前长度是否能覆盖完所有漏洞。

代码如下:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#include<set>
using namespace std;
#define pb push_back
#define ms(a, b) memset(a,b,sizeof(a));
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = ; int a[], T, n,k; int test(int len)
{
int now= , cnt = ;
for(int i = ; i<n; i++)
{
if(a[i]>now)
{
cnt++;
now = a[i]+len-;
if(cnt==k)//如果用完了k条时,判断是否已经覆盖完
{
if(now>=a[n-])
return ;
else
return ;
}
if(now>=a[n-])//如果没用完k条,就覆盖完的话,肯定可以
return ;
}
}
return ;
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
for(int i = ; i<n; i++)
{
scanf("%d",&a[i]);
} int h = , t = a[n-];
while(t%k) t++;//注意这一步,将t自加到能整除k为止 int mid;
while(h<=t)
{
mid = (h+t)/;
if(test(mid))
t = mid-;
else
h = mid+;
} printf("%d\n",h);
}
return ;
}

2016 Al-Baath University Training Camp Contest-1 I. March Rain —— 二分的更多相关文章

  1. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  2. 2014-2015 Petrozavodsk Winter Training Camp, Contest.58 (Makoto rng_58 Soejima contest)

    2014-2015 Petrozavodsk Winter Training Camp, Contest.58 (Makoto rng_58 Soejima contest) Problem A. M ...

  3. 2016 Al-Baath University Training Camp Contest-1 E

    Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...

  4. 2016 Al-Baath University Training Camp Contest-1 B

    Description A group of junior programmers are attending an advanced programming camp, where they lea ...

  5. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  6. 2016 Al-Baath University Training Camp Contest-1 J

    Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...

  7. 2016 Al-Baath University Training Camp Contest-1 I

    Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...

  8. 2016 Al-Baath University Training Camp Contest-1 H

     Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...

  9. 2016 Al-Baath University Training Camp Contest-1 G

    Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...

  10. 2016 Al-Baath University Training Camp Contest-1 F

    Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...

随机推荐

  1. 笔记-迎难而上之Java基础进阶6

    import java.io.*; public class InputStreamDemo{ public static void main(String[] args) throws IOExce ...

  2. js文件/图片从电脑里面拖拽到浏览器上传文件/图片

    1.效果展示 2.html 代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!DOCTYPE html> <html lang=& ...

  3. Interface Builder中的技巧

    在我工作中经常会遇到有人吐槽Xcode中的interface builder(以下简称IB)不好用的开发者.在我看来,IB是一个非常棒的可视化开发工具,可以非常快捷的设置UI控件的大部分常用属性.下面 ...

  4. cocos2d-x3.0 PageView

    .h加入例如以下代码: void pageViewEvent(Ref *pSender, PageViewEventType type); .m layout = Layout::create(); ...

  5. yii框架:CDbConnection failed to open the DB connection: could not find driver的解决的方法

    这个问题是由于php中缺少pdo mysql造成的. 解决方法是为php加入此扩展.前往你最早的php安装文件,进入ext/pdo_mysql/文件夹下,然后./configure --with-ph ...

  6. requests(爬虫常用)库的使用

    Requests库的使用 基于urllib改写的库 示例: import requests response=requests.get('http://www.baidu.com')#get请求 pr ...

  7. react request.js 函数封装

    1.request.js  函数封装 import { Toast } from 'antd-mobile'; import axios from 'axios'; import store from ...

  8. Redhat hadoop2.7.2安装笔记

    本次安装是在windows7环境下安装redhat虚拟机进行的,所须要的软件例如以下: VirtualBox-5.0.16-105871-Win.exe rhel-server-5.4-x86_64- ...

  9. Go语言中的单引号、双引号、反引号

    =Start= 搜索关键字: golang single quotes golang double quotes golang back quotes 参考结果: 结论写在最前:在Go语言中不倾向于使 ...

  10. mqtt client python example

    This is a simple example showing how to use the [Paho MQTT Python client](https://eclipse.org/paho/c ...