2016 Al-Baath University Training Camp Contest-1 I. March Rain —— 二分
题目链接:http://codeforces.com/problemset/gymProblem/101028/I
2 seconds
64 megabytes
standard input
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?
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).
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.
3
5 2
1 2 3 4 5
7 3
1 3 8 9 10 14 17
5 3
1 2 3 4 20
3
4
2
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 —— 二分的更多相关文章
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 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 ...
- 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 ...
- 2016 Al-Baath University Training Camp Contest-1 B
Description A group of junior programmers are attending an advanced programming camp, where they lea ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- about乘法逆元
本博客部分摘自 hwim 定义 乘法逆元的定义:若存在正整数a,b,p, 满足ab = 1(mod p), 则称a 是b 的乘法逆元, 或称b 是a 的乘法逆元.b ≡ a-1 (mod p),a ...
- 快速比较两个uiimage是否相等防止使用原始dsdata造成界面卡顿问题
UIImage *imageLater = image1; UIImage *imagePre = image2; if (imageLater == imagePre){....}
- ArcGIS for Android地图控件的5大常见操作转
http://blog.csdn.net/arcgis_mobile/article/details/7801467 GIS的开发中,什么时候都少不了地图操作.ArcGIS for Android中, ...
- c++对象内存模型【内存布局】(转)
总结:1.按1继承顺序先排布基于每个父类结构.2.该结构包括:基于该父类的虚表.该父类的虚基类表.父类的父类的成员变量.父类的成员变量.3.多重继承且连续继承时,虚函数表按继承顺序排布函数与虚函数.4 ...
- ubuntu环境准备
一. 桌面方面看起来比较不爽,12的版本用起更不习惯,决定改成命令行登陆 a. vi /ect/default/grub 文件 b. 修改成第二个红框的情况 c. 执行update-grub命令 d ...
- LNMPA遇到504 Gateway time-out错误的解决方法
Nginx的特点是处理静态很给力,Apache的特点是处理动态很稳定,两者结合起来便是LNMPA,nginx处理前端,apache处理后端,这样处理静态会很快,处理动态会很稳定. 当我以为安装完成以后 ...
- 自己定义struts2中action类型转换器
DateAction.java中代码例如以下: package com.itheima.action; import java.util.Date; public class DateAction { ...
- Fighting regressions with git bisect---within git bisect algorithm
https://www.kernel.org/pub/software/scm/git/docs/git-bisect-lk2009.html Fighting regressions with gi ...
- 微信热补丁 Tinker 的实践演进之路
http://dev.qq.com/topic/57ad7a70eaed47bb2699e68e http://dev.qq.com/topic/57a30878ac3a1fb613dd40eb ht ...
- kubernetes之pod中断
系列目录 目标读者: 想要构建高可用应用的应用所有者,因此需要知道pod会发生哪些类型的中断 想要执行自动化(比如升级和自动扩容)的集群管理员. 自愿和非自愿的中断 pod不会自动消息,除非有人(可能 ...