2016 Al-Baath University Training Camp Contest-1 I. March Rain —— 二分

题目链接: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.

 

 



题解:

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



代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<vector>
 9 #include<map>
10 #include<string>
11 #include<set>
12 using namespace std;
13 #define pb push_back
14 #define ms(a, b) memset(a,b,sizeof(a));
15 typedef long long LL;
16 const int inf = 0x3f3f3f3f;
17 const int maxn = 250000;
18 
19 int a[100005], T, n,k;
20 
21 int test(int len)
22 {
23     int now= 0, cnt = 0;
24     for(int i = 0; i<n; i++)
25     {
26         if(a[i]>now)
27         {
28             cnt++;
29             now = a[i]+len-1;
30             if(cnt==k)//如果用完了k条时,判断是否已经覆盖完
31             {
32                 if(now>=a[n-1])
33                     return 1;
34                 else
35                     return 0;
36             }
37             if(now>=a[n-1])//如果没用完k条,就覆盖完的话,肯定可以
38                 return 1;
39         }
40     }
41     return 0;
42 }
43 
44 int main()
45 {
46     scanf("%d",&T);
47     while(T--)
48     {
49         scanf("%d%d",&n,&k);
50         for(int i = 0; i<n; i++)
51         {
52             scanf("%d",&a[i]);
53         }
54 
55         int h = 1, t = a[n-1];
56         while(t%k) t++;//注意这一步,将t自加到能整除k为止
57 
58         int mid;
59         while(h<=t)
60         {
61             mid = (h+t)/2;
62             if(test(mid))
63                 t = mid-1;
64             else
65                 h = mid+1;
66         }
67 
68         printf("%d\n",h);
69     }
70     return 0;
71 }
View Code

 


 

posted on 2017-04-14 23:27  h_z_cong  阅读(258)  评论(0编辑  收藏  举报

导航