最大加权矩形{类似最大子串和}{O(n…

来源:互联网 发布:产品经理所用软件 编辑:程序博客网 时间:2024/05/11 18:06
var
  n,i,j,k,max:longint;
  a:array[1..100,1..100] of longint;
  s:array[1..100,0..100] of longint;
  f:array[1..100] of longint;
begin
  fillchar(s,sizeof(s),0);
  read(n);
  for i:=1 to n do
    for j:=1 to n do
     begin
       read(a[i,j]);
       s[i,j]:=s[i,j-1]+a[i,j];{预处理}
     end;
  max:=-maxlongint;
  for i:=0 to n-1 do
    for j:=i+1 to n do
     begin
       fillchar(f,sizeof(f),0);
       f[1]:=s[1,j]-s[1,i];
       for k:=2 to n do
         if f[k-1]<0then{与求最大子段和一样的决策}
          begin
            f[k]:=s[k,j]-s[k,i];
            if max
           end
         else
          begin
            f[k]:=f[k-1]+s[k,j]-s[k,i];
            if max
           end;
     end;
  writeln(max);
end.
原创粉丝点击