1. 程式人生 > >jzoj 1897. 【2014.8.17NOIP普及組模擬】憤怒的牛

jzoj 1897. 【2014.8.17NOIP普及組模擬】憤怒的牛

const
  maxn=100000;
var
  a:array[0..maxn] of longint;
  n,l,r,c:longint;

procedure init;
var
  i:longint;
begin
  readln(n,c);
  for i:=1 to n do
    readln(a[i]);
end;

procedure qsort(l,r:longint);
var
  i,j,k:longint;
begin
  if l>=r then exit;
  i:=l;j:=r;
  k:=a[(l+r) div 2];
  repeat
    while
a[i]<k do inc(i); while a[j]>k do dec(j); if i<=j then begin a[0]:=a[i];a[i]:=a[j];a[j]:=a[0]; inc(i);dec(j); end; until i>j; qsort(i,r);qsort(l,j); end; function find(x:longint):boolean; var i,j,k:longint; begin k:=1; j:=a[1]+x; for i:=2 to
n do while j<=a[i] do begin inc(k); j:=a[i]+x; if k=c then break; end; if k>=c then exit(true) else exit(false); end; begin assign(input,'aggr.in'); reset(input); assign(output,'aggr.out');rewrite(output); init; qsort(1,n); l:=1; r:=(a[n]-a[1
]) div (c-1); while l<r do begin if find((l+r) div 2) then l:=(l+r) div 2 else r:=(l+r) div 2; if l+1=r then break; end; if find(r) then writeln(r) else writeln(l); close(input);close(output); end.