POJ-3258 River Hopscotch two points

Source: Internet
Author: User

The main idea: to give the river width L and n blocks of stone, now requires the removal of M stone, so that the minimum distance between stones to reach the maximum (the beginning and end of a stone, but these two stones can not be removed)

The idea of solving the problem: the maximum value of the minimum value, must be two points
If there is an optimal distance, then the number of stones removed must be exactly M-block.
When enumerating the number of stones removed, just start enumerating from the starting point, then calculate a few of the stones in the minimum jumping distance, a few to remove a few, and finally decide how many stones to remove.
If the number of moves is greater than m, it means that the enumerated length is too large
If the number of moves is less than m, it means that the enumerated length is too small
There is a special case, that is, the case of n = m, in this case, many lengths of the enumeration are consistent, but the answer is only one, that is L, so the removal of the number equal to M does not mean that the solution has been found, should be added to the length of 1 to judge

#include <cstdio>#include <cstring>#include <algorithm>using namespace Std;#define MAXN 50010int POS[MAXN], L, N, M;void init () { for(inti =0; i < N; i++) scanf ("%d", &POS[i]);POS[n++] = L;POS[n++] =0;Sort(POS,POS+ N);} BOOL Judge (intMID) {intj =0, cnt =0; for(inti =1; i < N; i++) {if(POS[I]-POS[j] < mid) {cnt++;if(CNT > M)returnTrueContinue;    } j = i; }returnfalse;}intSolve () {intL =0, r = L;intAns =0x3f3f3f3f; while(L <= R) {intMid = (L + r)/2;if(Judge (mid)) R = mid-1;ElseL = mid +1; }returnL1;}intMain () { while(SCANF (" %d%d%d", &l, &n, &m)! = EOF) {init ();printf("%d\ n", solve ()); }return 0;}

POJ-3258 River Hopscotch two points

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.