[BZOJ1084] [SCOI2005]最大子矩阵

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=1084

题目大意

给定一个矩阵选取其中不相交的t个矩阵使其权值和最大

题解

m=1或2
m=1
dp[i,j]=max{dp[i1,j],max{dp[k,j1]+sum[i]sum[k]}}  (0<=k<i)
m=2
dp[i,j,k]=max{dp[i1,j,k],dp[i,j1,k]}
dp[i,j,k]=max{dp[i,j,k],dp[l,j,k1]+sum1[i]sum1[l]}  (0<=l<i)
dp[i,j,k]=max{dp[i,j,k],dp[i,l,k1]+sum2[j]sum2[l]}  (0<=l<j)
dp[i,j,k]=max{dp[i,j,k],dp[l,l,k1]+sum1[i]sum1[l]+sum2[i]sum2[l]}  (i=j0<=l<i)

var
 sum1,sum2:array[0..200]of longint;
 dp1:array[0..200,0..200]of longint;
 dp2:array[0..200,0..200,0..200]of longint;
 i,j,k,l:longint;
 n,m,t,a,b:longint;
function max(a,b:longint):longint;
begin
 if a>b then exit(a) else exit(b);
end;

begin
 readln(n,m,t);
 if m=1 then begin
  sum1[0]:=0;
  for i:=1 to n do
   begin
    readln(a);
    sum1[i]:=sum1[i-1]+a;
   end;
  for i:=1 to n do
   for j:=1 to t do
    begin
     dp1[i,j]:=dp1[i-1,j];
     for k:=0 to i-1 do
      dp1[i,j]:=max(dp1[i,j],dp1[k,j-1]+sum1[i]-sum1[k]);
    end;
  writeln(dp1[n,t]);
 end
 else begin
  sum1[0]:=0; sum2[0]:=0;
  for i:=1 to n do
   begin
    readln(a,b);
    sum1[i]:=sum1[i-1]+a; sum2[i]:=sum2[i-1]+b;
   end;
  for i:=1 to n do
   for j:=1 to n do
    for k:=1 to t do
     begin
      dp2[i,j,k]:=max(dp2[i-1,j,k],dp2[i,j-1,k]);
      for l:=0 to i-1 do
       dp2[i,j,k]:=max(dp2[i,j,k],dp2[l,j,k-1]+sum1[i]-sum1[l]);
      for l:=0 to j-1 do
       dp2[i,j,k]:=max(dp2[i,j,k],dp2[i,l,k-1]+sum2[j]-sum2[l]);
      if i=j then
      for l:=0 to i-1 do
       dp2[i,j,k]:=max(dp2[i,j,k],dp2[l,l,k-1]+sum1[i]-sum1[l]+sum2[i]-sum2[l]);
     end;
  writeln(dp2[n,n,t]);
 end;
end.

你可能感兴趣的:([BZOJ1084] [SCOI2005]最大子矩阵)