Remove the Stone
The main idea: a group of cows jumping on a stone in the river, and now ask you how to remove the M-stone, so that the shortest distance the cow jump stone to become the largest?
This problem is more classical, the Classical division of the law, the dichotomy can be very convenient to deal with this issue, we just understand the comparison function this thing can be.
Template:
while (...) { 2; if (Judge_c (...)) Else RB = mid; }
While judging the condition can be based on the shape or floating-point flexible transformation, Judge_c is the comparison function, almost all of the partition algorithm can be summed up, we just find the appropriate comparison function can be
For this problem, his comparison function can see this, we first remove the stone m, and then put in these positions, the maximum of the shortest distance.
In this way, we can only use a good limit (upper limit is length+1)
1#include <functional>2#include <iostream>3#include <algorithm>4 5 using namespacestd;6 7 voidSearch (Const int,Const int);8 BOOLJudge_c (Const int,Const int,Const int);9 Ten Static introck[50005]; One Static intMin_step; A - intMainvoid) - { the intLength, M, block_sum; - while(~SCANF ("%d%d%d", &length, &block_sum, &M)) - { - for(inti =1; I <= block_sum; i++) +scanf"%d", &rock[i]);//location of rock storage - +rock[0] =0; ARock[block_sum +1] = Length;//Place both the starting position and the end position in the array at -Sort (Rock, rock + block_sum +2); - Search (M, block_sum); - } - return 0; - } in - BOOLJudge_c (Const intMConst intBlock_sum,Const intmin_distance) to { + intLast =0, pos =0; - the for(inti =0; i < block_sum-m; i++) * { $pos = last +1;Panax Notoginseng while(Pos <= block_sum && Rock[pos]-Rock[last] <min_distance) -pos++; the if(pos = = Block_sum +1) + return false; ALast =Pos; the } + return true; - } $ $ voidSearch (Const intMConst intblock_sum) - { - intLB =0, RB = Rock[block_sum +1] +1, mid; the - while(Rb-lb >1)Wuyi { theMid = (lb + rb)/2; - if(Judge_c (M, Block_sum, mid)) Wu //judgment C (x): Remove the M-stones as a block_sum-m a stone in these positions - //Note that the upper bounds are l+1 and then approximated with two points. AboutLB =mid; $ ElseRB =mid; - } -printf"%d\n", LB); -}
Divide and Conquer:river Hopscotch (POJ 3258)