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