Cod sursa(job #19273)

Utilizator bigsarpeadrian bigsarpe Data 19 februarie 2007 09:12:32
Problema Ghiozdan Scor 6
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.67 kb
{8:16-9:07 cu mult timp degeaba}
const maxn=75000;maxbuc=200;oo=25000;
var t:text;
   Count:Array[0..maxbuc]of longint;
   modu,invu,i,n,cit,G:longint;
   R,Scade,Q,V:Array[0..maxn]of integer;lo,hi:longint;
   Best:Array[0..1,0..maxn]of integer;
   Procedure Dinamica(rec:boolean;tip,dist:longint;var A,B,R:array of integer);
   var i,j,rest:longint;
   begin
      hi:=0;if rec then fillchar(R,sizeof(R),0);
      for i:=G downto 0 do
      begin Scade[i]:=hi;V[i]:=A[i]+hi;if i mod tip=0 then inc(hi)end;
      for rest:=0 to tip-1 do
      begin
         lo:=1;hi:=0;i:=rest;
         while i<=G do
         begin
            if i>=tip*dist then j:=dist else j:=(i-rest)div tip;
            B[i]:=A[i];
            {pun}
            while (lo<=hi)and(V[Q[hi]]>V[i])do dec(hi);inc(hi);Q[hi]:=i;
            while (lo<=hi)and(Scade[Q[lo]]>Scade[i]+j)do inc(lo);
            if (lo<=hi)and(B[i]>V[Q[lo]]-Scade[i])then
            begin
               B[i]:=V[Q[lo]]-Scade[i];
               if Rec then R[i]:=Q[lo];
            end;
            inc(i,tip);
         end;
      end;
   end;
begin
   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);
   for i:=1 to G do Best[0,i]:=oo;
   for i:=1 to maxbuc Do
   begin
      modu:=i and 1;invu:=1-modu;
      Dinamica(false,i,Count[i],Best[invu],Best[modu],R);
   end;
   for i:=1 to maxbuc Do
   begin
      modu:=i and 1;invu:=1-modu;
      Dinamica(false,i,Count[i],Best[invu],Best[modu],R);
   end;
   for i:=G downto 0 do if Best[modu][i]<>oo then break;
   assign(T,'ghiozdan.out');rewrite(T);writeln(t,i,' ',Best[modu][i]);close(t);
end.