Cod sursa(job #57354)

Utilizator gurneySachelarie Bogdan gurney Data 1 mai 2007 20:33:01
Problema Lapte Scor 60
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.45 kb
program lapte;
  const
    fin='lapte.in';
    fout='lapte.out';
    nmax=100;
type
  milk=record
    a,b,i:integer;
    end;
var
  a:array[1..nmax] of milk;
  st,dr,mid,n,t,tmax,i,j,x,y:integer;
  l1,l2:array[1..nmax] of integer;

procedure scufunda(i,n:integer);
  var
    aux:milk;
    p:integer;
  begin
    p:=i;
    if i shl 1<=n then
      if (a[i shl 1].b>a[i].b)or((a[i shl 1].b=a[i].b)and(a[i shl 1].a<a[i].a))then
        p:=i shl 1;
    if i shl 1 or 1<=n then
      if (a[i shl 1 or 1].b>a[i].b)or((a[i shl 1 or 1].b=a[i].b)and(a[i shl 1 or 1].a<a[i].a))then
        p:=i shl 1 or 1;
    if i<>p then
      begin
        aux:=a[i];a[i]:=a[p];a[p]:=aux;
        scufunda(p,n);
      end;
  end;

procedure sort(n:integer);
  var
    i:integer;
    aux:milk;
  begin
    for i:=n shr 1 downto 1 do
      scufunda(i,n);
    for i:=n downto 2 do
      begin
        aux:=a[1];a[1]:=a[i];a[i]:=aux;
        scufunda(1,i-1);
      end;
  end;

function check(tmax:integer):boolean;
  var
    c1,c2,c:integer;
  begin
    c1:=0;c2:=0;
    for i:=1 to n do
      inc(c1,tmax div a[i].a);
    if c1>=t then
    for i:=1 to n do
      begin
        c:=(c1-t);
        if c>tmax div a[i].a then
          c:=tmax div a[i].a;
        l2[i]:=(c*a[i].a+tmax mod a[i].a)div a[i].b;
        l1[i]:=tmax div a[i].a-(tmax-l2[i]*a[i].b) div a[i].a;
        inc(c2,l2[i]);
        dec(c1,l1[i]);
      end;
    check:=(c1>=t)and(c2>=t);
  end;

procedure scrie(tmax:integer);
  var
    c1,c2,c:integer;
  begin
    c1:=0;c2:=0;
    for i:=1 to n do
      inc(c1,tmax div a[i].a);
    for i:=1 to n do
      begin
        c:=(c1-t);
        if c>tmax div a[i].a then
          c:=tmax div a[i].a;
        l2[a[i].i]:=(c*a[i].a+tmax mod a[i].a)div a[i].b;
        l1[a[i].i]:=(tmax-l2[a[i].i]*a[i].b) div a[i].a;
        inc(c2,l2[a[i].i]);
        dec(c1,tmax div a[i].a-l1[a[i].i]);
      end;
    for i:=1 to n do
      writeln(l1[i],' ',l2[i]);
  end;

begin
  assign(input,fin);
    reset(input);
    readln(n,t);
    for i:=1 to n do
      readln(a[i].a,a[i].b);
    for i:=1 to n do
      a[i].i:=i;
  close(input);
  assign(output,fout);
    rewrite(output);
    sort(n);
st:=1;dr:=100;
    while st<dr do
      begin
        mid:=(st+dr)shr 1;
        if check(mid) then
          dr:=mid
        else
          st:=mid+1;
      end;
    writeln(st);
    scrie(st);
  close(output);
end.