Cod sursa(job #37824)

Utilizator ProtomanAndrei Purice Protoman Data 25 martie 2007 12:45:12
Problema Shop Scor 100
Compilator fpc Status done
Runda preONI 2007, Runda 4, Clasa a 9-a si gimnaziu Marime 1.54 kb
var i,n,j,g,m:longint; s,y,id,l:int64; a,b,c,x,z:array[1..30] of longint; f1,f2:text;

procedure pozitie(var m:longint; p,u:longint);
var i,j,di,dj,aux:longint;
begin
di:=0;
dj:=-1;
i:=p;
j:=u;
while i<j do begin
if a[i]>a[j] then
begin
aux:=di;
di:=-dj;
dj:=-aux;
aux:=a[i];
a[i]:=a[j];
a[j]:=aux;
aux:=b[i];
b[i]:=b[j];
b[j]:=aux;
end;
i:=i+di;
j:=j+dj;
end;
m:=i;
end;

procedure quick(p,u:longint);
var m:longint;
begin
if p<u then begin
pozitie(m,p,u);
quick(p,m-1);
quick(m+1,u);
end;
end;

procedure search(li,ls:longint);
begin
m:=(li+ls) div 2;
if z[i]=a[m] then x[i]:=m
             else if li<ls then if z[i]<a[m] then search(li,m-1)
                                             else search(m+1,ls);
end;

begin
        assign(f1,'shop.in');
        reset(f1);
        assign(f2,'shop.out');
        rewrite(f2);
        read(f1,n,g,l);
        for i:=1 to n do begin read(f1,a[i],b[i]); z[i]:=a[i]; end;
        quick(1,n);
        for i:=1 to n do search(1,n);
        for i:=n downto 1 do begin
                y:=0;
                id:=1;
                if g<>1 then for j:=1 to a[i] do id:=id*g
                        else id:=g;
                while (b[i]>0)and(y+id<=l) do begin
                        dec(b[i]);
                        y:=y+id;
                        inc(c[i]);
                        inc(s);
                end;
                l:=l-y;
        end;
        writeln(f2,s);
        for i:=1 to n do write(f2,c[x[i]],' ');
        close(f1);
        close(f2);
end.