Time
limit:1000MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64 U SubmitStatusPracticePOJ 2456
Description
Farmer John had built a new long barn, with N (2 <= n <= 100,000) stalls. The stalls is located along a straight line at positions X1,..., XN (0 <= XI <= 1,000,000,000).
His C (2 <= C <= N) cows don ' t like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting all other, FJ want to assign the cows to the stalls, such that the minimum distance bet Ween any and them is as large as possible. What's the largest minimum distance?
Input
* Line 1:two space-separated integers:n and C
* Lines 2..n+1:line i+1 contains an integer stall location, xi
Output
* Line 1:one integer:the largest minimum distance
Sample Input
5 312849
Sample Output
3
Hint
OUTPUT DETAILS:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.
1#include <stdio.h>2#include <string.h>3#include <math.h>4#include <algorithm>5 using namespacestd;6 Const intinf=1e9+7;7 8 intn,m;9 inta[100005];Ten One BOOLCintd) A { - intlast=1; - for(intI=1; i<m;i++) the { - intcrt=last+1; - while(Crt<=n && a[crt]-a[last]<d) - { +crt++; - } + if(crt==n+1) A return false; atlast=CRT; - } - return true; - } - - intMain () in { - inti,j; to while(SCANF ("%d%d", &n,&m)! =EOF) + { - for(i=1; i<=n;i++) thescanf"%d",&a[i]); *Sort (A +1, a+n+1); $ intlb=0, ub=INF;Panax Notoginseng while(ub-lb>1) - { the intMid= (LB+UB)/2; + if(C (mid)) Alb=mid; the Else +ub=mid; - } $printf"%d\n", LB); $ } - return 0; -}
View Code
Two-part Example 2