2016 Al-Baath University Training Camp Contest-1 I. March Rain —— 二分
来源:互联网 发布:淘宝无人机 编辑:程序博客网 时间:2024/05/18 10:57
题目链接:http://codeforces.com/problemset/gymProblem/101028/I
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.
35 21 2 3 4 57 31 3 8 9 10 14 175 31 2 3 4 20
342
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 = 250000;int a[100005], T, n,k;int test(int len){ int now= 0, cnt = 0; for(int i = 0; i<n; i++) { if(a[i]>now) { cnt++; now = a[i]+len-1; if(cnt==k)//如果用完了k条时,判断是否已经覆盖完 { if(now>=a[n-1]) return 1; else return 0; } if(now>=a[n-1])//如果没用完k条,就覆盖完的话,肯定可以 return 1; } } return 0;}int main(){ scanf("%d",&T); while(T--) { scanf("%d%d",&n,&k); for(int i = 0; i<n; i++) { scanf("%d",&a[i]); } int h = 1, t = a[n-1]; while(t%k) t++;//注意这一步,将t自加到能整除k为止 int mid; while(h<=t) { mid = (h+t)/2; if(test(mid)) t = mid-1; else h = mid+1; } printf("%d\n",h); } return 0;}
- 2016 Al-Baath University Training Camp Contest-1 I. March Rain —— 二分
- 2016 Al-Baath University Training Camp Contest-1(gym101028)
- 2016 Al-Baath University Training Camp Contest-1(gym101028)
- 2016 Al-Baath University Training Camp Contest-1
- Codeforce 2016 Al-Baath University Training Camp Contest-1
- 2016 Al-Baath University Training Camp Contest-1
- 2013 Multi-University Training Contest 1——I-number
- RMQ+二分——GCD ( HDU 5726 ) ( 2016 Multi-University Training Contest 1 1004 )
- 2016 Multi-University Training Contest 1 GCD【RMQ+二分】
- 2014 Multi-University Training Contest 1 —I Turn the pokers
- 2016 Multi-University Training Contest 1
- 2016 Multi-University Training Contest 1
- 2016 Multi-University Training Contest 1
- 2016 Multi-University Training Contest 1
- 2014 Multi-University Training Contest 1 — D. Task
- 2013 Multi-University Training Contest 1 I-number 大数
- 2016 Multi-University Training Contest 1 1004 hdu 5726 二分+RMQ
- 2016 Multi-University Training Contest 1-1005---HDU 5727 Necklace(枚举+二分图匹配)
- 数据类型和变量
- STM32 SPI DMA 的使用
- matlab中如何使用外部工具箱
- Spring学习之缓存机制EhCache---Key
- Redis学习总结_1_底层数据结构
- 2016 Al-Baath University Training Camp Contest-1 I. March Rain —— 二分
- uml类图的图示方法
- NOI 题库 4.5之动态规划算法 3368 Sanguo
- BootStrap-全局样式
- Android框架之路——ToolBar的使用
- 关于输入后接收回车符问题
- kudu源码分析之async_logger
- 控制EditText是否弹出键盘的方法
- 使用Jquery解析Json基础知识