POJ 3258 Minimum maximum binary search

Source: Internet
Author: User

Test instructions: To the other side of the river, on a line perpendicular to the bank, there are n stones in the river, given the river's width l, and the distance of each stone from the bank of the Ox,

Now remove the M-block stone and ask for the maximum of the minimum distance between the remaining stones and the stone and the river bank after the M-block stone is removed.

First to understand test instructions, remove some stones, so that the shortest distance jumping is the largest, the distance of the jump must be a value and must be less than the total distance, at the same time we can know is that if the removal of a few stones, to a certain shortest distance jumps are satisfied, less than the shortest distance of words must be satisfied, This is not necessarily the shortest distance, so the advantage of binary search is that it can be accurately determined by the previous judgment on the next scope of the lock, to determine the maximum minimum distance. When judging, we can pass the continuous sequence of temptation, if the jump distance is less than the minimum required distance, you need to add a removed stone. And then the distance is to remove the stone after the distance, note that there is a sequence. In the past. I was in the main point of the end of the distance with doubts, in fact, the end of the paragraph although the end can not be removed, but as long as the number of stones to remove less than equals N, can be removed by removing the stone before the implementation. If you want to remove n+1 blocks of stone that obviously does not meet the requirements of m<=n.

Two points of difficulty:

1, binary search shortest distance value

2, the order of the value to determine whether to delete some stone is satisfied (the number of stones actually deleted and the number of deleted), the distance should also pay attention to the beginning of the change.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 #defineMAXN 500056 using namespacestd;7 intNUM[MAXN];8 intl,n,m;9 BOOLSolveintDist) {Ten     intans=0, j=0; One      for(inti =1; i<= n+1; i++){ A         if(Num[i]-num[j]<Dist) { -ans++; -         } the         Else -         { -j =i; -         } +     } -     if(Ans >m) +         return false; A     Else at         return true; - } - intMain () { -     inti; -scanf"%d%d%d",&l,&n,&m); -num[0]=0; in      for(i =1; I <= N; i++){ -scanf"%d",&num[i]); to     } +Sort (num,num+n+1); -num[n+1] =l; the     intleft =0, right =l; *     intMID =0 ; $     intval =l;Panax Notoginseng      while(left<=Right ) { -Mid = left + (right-left)/2; the         if(Solve (mid)) { +val =mid; ALeft = mid +1; the         } +         Else -right = mid-1; $     } $cout<<val<<Endl; -     return 0; -}

POJ 3258 Minimum maximum binary search

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.