HELPPM - Giúp ngài thủ tướng!

Tác giả: ladpro98

Ngôn ngữ: Pascal

program helppm;
const   maxn=500;
        fi='';
var     i,j,m,n,k,lim,res,x1,y1,x2,y2,l:longint;
        a,col:array[0..maxn,1..maxn] of longint;
        f,s:array[1..maxn] of longint;
        sum,t,tp:longint;
        inp:text;
begin
        assign(inp,fi);reset(inp);
        readln(inp,m,n,lim);
        for i:=1 to m do
        begin
                for j:=1 to n do read(inp,a[i,j]);
                readln(inp);
        end;
        for i:=1 to m do
        for j:=1 to n do
        col[i,j]:=col[i-1,j]+a[i,j];
        for i:=1 to m do
        begin
                s[i]:=0;
                for j:=1 to n do inc(s[i],a[i,j]);
        end;
        res:=high(longint);
        for i:=1 to m do
        begin
                tp:=0;
                for k:=i to m do
                if (k-i+1<res) then
                begin
                        t:=0;
                        inc(tp,s[k]);
                        if tp<lim then continue;
                        for j:=1 to n do
                        begin
                                f[j]:=col[k,j]-col[i-1,j];
                        end;
                        l:=1;j:=1;sum:=0;
                        for j:=1 to n do
                        begin
                                if sum+f[j]>=lim then break;
                                inc(sum,f[j]);
                        end;
                        while j<=n do
                        begin
                                inc(sum,f[j]);
                                if sum-f[l]>=lim then
                                begin
                                        while sum-f[l]>=lim do
                                        begin
                                                dec(sum,f[l]);
                                                inc(l);
                                        end;
                                end;
                                if sum>=lim then
                                if res>(j-l+1)*(k-i+1) then
                                begin
                                        res:=(j-l+1)*(k-i+1);
                                        x1:=i;x2:=k;
                                        y1:=l;y2:=j;
                                end;
                                inc(j);
                        end;
                end
                else
                break;
        end;
        if res<>high(longint) then
        begin
                writeln(res);
                write(x1,' ',y1,' ',x2,' ',y2);
        end
        else
        write(-1);
end.

Download