Cod sursa(job #14183)

Utilizator CezarMocanCezar Mocan CezarMocan Data 8 februarie 2007 13:53:33
Problema Elimin Scor 70
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.9 kb
var s,st:array[0..25]of longint;
    v:array[0..20,0..7500]of longint;
    x:array[0..800]of longint;
    i,j,n,m,r,c,stot,max,aux:longint;

procedure qsort(lower, upper : byte);
     var
       left, right, pivot, aux : integer;
     begin
         pivot:=x[(lower+upper) div 2];
         left:=lower;
         right:=upper;

         while left<=right do
         begin
             while x[left]  < pivot do left:=left+1;  { Parting for left }
             while x[right] > pivot do right:=right-1;{ Parting for right}
             if left<=right then   { Validate the change }
             begin
                 aux:=x[left];
                 x[left]:=x[right];
                 x[right]:=aux;
                 left:=left+1;
                 right:=right-1;
             end;
         end;
         if right>lower then qsort(lower,right); { Sort the LEFT  part }
         if upper>left  then qsort(left ,upper); { Sort the RIGHT part }
     end;


procedure back(k,no:longint);
var i,j,ss,min,nr:longint;
    ok:boolean;
begin
if k=no then
        begin
        nr:=0;
        for i:=1 to no do
                if st[i]=1 then
                        begin
                        inc(nr);
                        s[nr]:=i;
                        end;
        if nr=r then
                begin
                ss:=stot;
                for i:=1 to n do
                        x[i]:=v[0,i];
                for i:=1 to nr do
                        for j:=1 to n do
                                dec(x[j],v[s[i],j]);
                for i:=1 to nr do
                        ss:=ss-v[s[i],0];
                qsort(1,n);
                for i:=1 to c do
                        ss:=ss-x[i];
                if ss>max then
                        max:=ss;
                end
        end
else
        for i:=1 downto 0 do
                begin
                st[k+1]:=i;
                back(k+1,no);
                end;
end;

begin
assign(input,'elimin.in');reset(input);
assign(output,'elimin.out');rewrite(output);
readln(m,n,r,c);
if n<m then
        begin
        aux:=n;
        n:=m;
        m:=aux;
        aux:=r;
        r:=c;
        c:=aux;
        for i:=1 to n do
                for j:=1 to m do
                        begin
                        read(v[j,i]);
                        inc(v[j,0],v[j,i]);
                        stot:=stot+v[j,i];
                        end;
        for i:=1 to m do
                for j:=1 to n do
                        inc(v[0,j],v[i,j]);
        end
else begin
for i:=1 to m do
        for j:=1 to n do
                begin
                read(v[i,j]);
                inc(v[i,0],v[i,j]);
                stot:=stot+v[i,j];
                end;
for i:=1 to n do
        for j:=1 to m do
                inc(v[0,i],v[j,i]);
end;
back(0,m);
writeln(max);
close(input);close(output);
end.