Cod sursa(job #19279)

Utilizator bigsarpeadrian bigsarpe Data 19 februarie 2007 09:19:29
Problema Ghiozdan Scor 12
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.71 kb
{8:16-9:07 cu mult timp degeaba}
const maxn=75000;maxbuc=200;oo=30000;
var t:text;
   Count:Array[0..maxbuc]of longint;
   j,modu,invu,i,n,cit,G,rest,tmp:longint;
   R,Scade,Q,V,Veu: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:longint;
   begin
      hi:=0;if rec then fillchar(R,sizeof(R),0);
      for i:=G downto 0 do
      begin Scade[i]:=hi;Veu[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 div tip;
            B[i]:=A[i];
            {pun}
            while (lo<=hi)and(V[hi]>Veu[i])do dec(hi);
            inc(hi);Q[hi]:=i;V[hi]:=veu[i];
            while (lo<=hi)and(Scade[Q[lo]]>Scade[i]+j)do inc(lo);
            tmp:=V[lo]-Scade[i];
            if {(lo<=hi)and}(B[i]>tmp)then
            begin
               B[i]:=tmp;
               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:=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);
   for i:=1 to maxbuc Do
   begin
      modu:=i and 1;invu:=1-modu;
      Dinamica(true,i,Count[i],Best[invu],Best[modu],R);
   end;
end.