River hopscotch-[Two-point search, greed]

Source: Internet
Author: User

Description

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to R Ock in a river. The excitement takes place in a long, straight river with a rock at the start and another rock at the end, L unit S away from the start (1≤ L ≤1,000,000,000). Along the between the starting and ending rocks, N (0≤ n ≤50,000) more rocks appear Integral distance di from the start (0 < di < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping O Nly from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

Farmer John is proud of he cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rock s placed too closely together. He plans to remove several rocks on order to increase the shortest distance a cow would have the to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he had enough resources to remove up to m Rocks (0≤ MN).

FJ wants to know exactly how much he can increase the shortest distance *before* He starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow have to jump after removing the optimal set of m Rocks.

Input

Line 1:three space-separated integers: L, N, and M
Lines 2.. N+1:each line contains a single integer indicating what far some rock was away from the starting rock. No Rocks share the same position.

Output

Line 1: A single integer which is the maximum of the shortest distance A cow have to jump after removing MRocks

Sample Input

25 5 2214112117

Sample Output

4

Problem Solving Ideas:
At first Test instructions was not very well understood. A little conversion of ideas, given n+2 a stone, from which to choose n+2-m stone to make them the smallest distance between the largest.
After analysis is easy to find, regardless of the increase or decrease, the distance between the stone is limited, the minimum is the minimum distance between the stone min, the largest is L. Then the problem can be converted into this:
Search within [min,l] for distances that meet the following conditions DM:
1) There are only n+2-m stones (including two stones at the start and end points), and the minimum distance between them is di.
(ie: arbitrarily add or subtract a stone, the minimum distance between them is not di)
2) Dm=max{di};
Note: The condition (1) (2) guarantees the uniqueness of the DM.
Then we can find Di by two points, select the stones in the interval of Di, the number is CNT,
If cnt>n+2-m, it means that the DI selection is small;
If cnt<n+2-m, it indicates that Di was selected large;
If the cnt=n+2-m does not meet the condition (2), it is necessary to continue to look up (increase).

The code is as follows:
#include <iostream>#include<cstdio>#include<algorithm>#include<ctime>using namespacestd;#definePrint_time_ printf ("%f\n", Double (Clock ())/clocks_per_sec);#defineMAXN 50000ints[maxn+5];intl,n,m;BOOLJudgeintmid) {    intCnt=0; intI=0;  while(1) {i=lower_bound (S+i, s+n+1, S[i]+mid)-s; if(i<n+1&&s[n+1]-s[i]>=mid) cnt++; Else  Break; }    returncnt-(n-m) >=0;}intBsearch (intAintb) {    if(! Nreturnb; intMid= (A+B)/2; intans=0;  while(a<=b) {        BOOLflag=judge (mid); if(flag) {if(Mid>ans) ans=mid; A=mid+1; Mid= (a+b)/2; }        Else{b=mid-1; Mid= (a+b)/2; }    }    returnans;}intMain () {scanf ("%d%d%d",&l,&n,&M);  for(intI=0; i<n;i++) scanf ("%d", &s[i+1]); s[0]=0; s[n+1]=L; Sort (s, S+n+2); intLow_bound= (1<< to)-1;  for(inti =1; I <= n+1; i++) {Low_bound= Min (Low_bound, s[i]-s[i-1]); } printf ("%d\n", bsearch (Low_bound, L)); //print_time_    return 0;}

River hopscotch-[Two-point search, greed]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.