吃西瓜(压缩压缩!)




样例输入

2 3 4
4 1 2 8
0 5 -48 4
3 0 1 9
2 1 4 9
1 0 1 7
3 1 2 8

样例输出

45
 
 
我……看了题解……
然后我终于懂了怎么做了!这么说来 糖果盒也可以这样做嘛╮(╯▽╰)╭
我的糖果盒用的是貌似悬线法……这个我再去看一下……
于是这种方法要学习 下周再多看一下 重点是压缩再枚举了取几行多高后一列一列地求最大子区间和!
 

program p93;
var
  f,a:array[0..51,0..51,0..33] of longint;
  h,n,m,ans:longint;

procedure init;
var i,j,k:longint;
begin
  assign(input,'p93.in');reset(input);
  assign(output,'p93.out');rewrite(output);
  fillchar(a,sizeof(a),0);
  fillchar(f,sizeof(f),0);
  readln(h,n,m);
  for k:=1 to h do
    for i:=1 to n do
      for j:=1 to m do read(a[i,j,k]);
     
  for k:=2 to h do
    for i:=1 to n do
      for j:=1 to m do a[i,j,k]:=a[i,j,k]+a[i,j,k-1];
  for k:=1 to h do
    for i:=2 to n do
      for j:=1 to m do a[i,j,k]:=a[i,j,k]+a[i-1,j,k];

end;

function max(a1,a2:longint):longint;
begin
  if (a1>a2) then exit(a1) else exit(a2);
end;

procedure main;
var h1,h2,i,n1,n2,tot:longint;
begin
  ans:=0;
  for h2:=1 to h do
    for h1:=1 to h2 do
      for n2:=1 to n do
        for n1:=1 to n2 do begin
          tot:=0;
          for i:=1 to m do begin
            if (tot<0) then tot:=0;
            tot:=tot+a[n2,i,h2]-a[n2,i,h1-1]-a[n1-1,i,h2]+a[n1-1,i,h1-1];
            f[n2,i,h2]:=max(f[n2,i,h2],tot);
            if (ans<f[n2,i,h2]) then ans:=f[n2,i,h2];
          end;
        end;
  writeln(ans);
end;

procedure terminate;
begin
  close(input);close(output);
end;

begin
  init;
  main;
  terminate;
end.

posted on 2011-11-04 16:52  ushiojamie  阅读(168)  评论(0编辑  收藏  举报