Cod sursa(job #18296)

Utilizator pelaspateSorin Fagateanu pelaspate Data 18 februarie 2007 11:18:41
Problema Ghiozdan Scor 90
Compilator fpc Status done
Runda preONI 2007, Runda 2, Clasele 11-12 Marime 2.05 kb
{$q-,r-,s-,d-,i-}
const maxbuc=206;oo=maxlongint div 4;
var t:Text;
    Sol,Count:array[0..maxbuc*2]of longint;
    maxim,first,ux,uy,scade,pune,Gmax,Nmin,tmp,k,j,N,G,i,cit:longint;
    V,Tata:array[0..maxbuc*2,0..maxbuc]of longint;
{    var t1:longint;t2:longint absolute $0:$046c;}
begin
{   t1:=t2;}
   assign(T,'ghiozdan.in');reset(T);readln(t,n,G);
   for i:=1 to N do begin readln(t,cit);inc(count[cit]);end;close(T);
   i:=maxbuc;
   while i>0 do
   begin
      if count[i]=0 then begin dec(i);continue;end;
      if Gmax+i>G then break;
      inc(Gmax,i);Dec(count[i]);inc(Sol[i]);inc(Nmin);
   end;
   for i:=1 to maxbuc*2 do for j:=0 to maxbuc do V[i,j]:=oo;V[0,0]:=0;
   for i:=1 to maxbuc*2 do
   for j:=1 to maxbuc do
   begin
      V[i,j]:=V[i,j-1];k:=1;
      while (k<=Count[j])and(k*j<=i) do
      begin
         if V[i,j]>V[i-k*j,j-1]+k then
         begin V[i,j]:=v[i-k*j,j-1]+k;Tata[i,j]:=k;end;
         inc(k);
      end;
   end;first:=Nmin;maxim:=Gmax;
   for scade:=1 to maxbuc do if Sol[scade]>0 then
   for pune:=maxbuc*2 downto 1 do
   if (V[pune,maxbuc]<>oo)and (maxim>=scade)and(maxim-scade+pune<=G) then
      if (Gmax<maxim-scade+pune)or
         (Gmax=maxim-scade+pune)and(Nmin>V[pune,maxbuc]+first-1)then
      begin
         Gmax:=maxim-scade+pune;
         Nmin:=V[pune,maxbuc]+first-1;
         ux:=pune;uy:=scade;
      end;
   if (gmax<>maxim)or(first<>nmin) then
   begin
      dec(sol[uy]);inc(count[uy]);{a mutat}
{      reconstruieste;}
       uy:=maxbuc;
       repeat
          if tata[ux,uy]=0 then dec(uy) else
          begin
             tmp:=tata[ux,uy];
             inc(sol[uy],tmp);dec(count[sol[uy]],tmp);
             ux:=ux-tmp*uy;dec(uy);
          end;
       until (ux=0){or (uy=0)};
   end;
   gmax:=0;nmin:=0;
   for i:=0 to maxbuc do begin inc(gmax,Sol[i]*i);inc(nmin,sol[i]);end;
   assign(T,'ghiozdan.out');rewrite(T);writeln(t,Gmax,' ',Nmin);
            {ghiozdan.ok}
   for i:=1 to maxbuc do for j:=1 to sol[i] do writeln(t,i);close(T);
{   writeln((t2-t1)/18.2:0:6);}
end.