http://acm.buaa.edu.cn/problem/1389/
Angry Dzy.
"Problem description"
"Angry Birds" is now a household game, witty WJC recently invented a similar new game: "Angry Dzy". The game is like this: the player has k Dzy, and N is located in a different integer position: X1,x2,...,xn's hay bag. Each dzy can stand in a certain position: X throws bombs, throws bombs, this dzy will hang off. The radius of the bomb is R (and each time the radius of each Dzy is constant, R, and the position of the station X can be changed), the damage range of the x−r~x+r (i.e. the position in X-r to X+r (including x-r,x+r) will be blown up). Now given the number of Dzy K, and the location of the hay heap: x1,x2,...,xn, ask you the smallest radius r that can blow up all the hay heaps.
"Input description"
The first row of two positive integers n,k. Next n rows, one number per line, and then: X1,x2,...,xn.
"Output description"
Only one row, which represents the smallest radius r that can blow up all the hay heaps.
"Sample Input"
9 |
20
25
18
8
10
3
1
"Sample Output"
5
Propositional person: HACKER_WJC
Learning the maximum value of the above problem is minimized, this problem is very good solved, data n card to 10e6
To the position from small to large sorting, enumeration diameter, it is advisable to set a minimum diameter of 1, the largest is the front and back two points distance a[n]-a[1], and then start from a[1] to enumerate whether the diameter is mid, if more than a[i] + mid small take one, otherwise cnt++, with that point as the starting point in judging
The radius is used in the output of the input method
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <iostream>5 using namespacestd;6 Const intMAX =1000000+Ten;7 intA[max];8 intN,k,maxn,minn;9 voidinput ()Ten { OneMinn = MAXN =0; Ascanf"%d%d", &n, &k); - for(inti =1; I <= N; i++) - { thescanf"%d", &a[i]); - } -Sort (A +1, A + n +1); -Minn =1; +MAXN = a[n]-a[1]; - } + BOOLIS_OK (intmid) A { at BOOLOK =true; - intStart =1, End =1; - intCNT =1; - while(End <=N) - { - if(A[end]-A[start] <=mid) in { -end++; to } + Else - { thecnt++; * if(CNT >k) $ {Panax NotoginsengOK =false; - Break; the } +Start =End; A } the } + returnOK; - } $ intSolve () $ { - intx = minn, y =MAXN; - the while(x <=y) - {Wuyi intMid = (x + y)/2; the if(IS_OK (mid)) -y = mid-1; Wu Else -x = mid +1; About } $ returnx; - } - intMain () - { A input (); + intAns =solve (); the if(Ans%2==0) -printf"%d\n", ans/2 ); $ Else theprintf"%d\n", ans/2+1); the}
View Code
BUAA1389 angry Dzy (maximum minimized)