1084: [scoi2005] maximum submatrix time limit: 10 sec memory limit: 162 MB
Submit: 1129 solved: 578
[Submit] [Status] Description
Here is a matrix of N * M. Please select K sub-matrices to maximize the total score of K sub-matrices. Note: The selected K submatrices cannot overlap with each other.
Input
First behavior n, m, K (1 ≤ n ≤, 1 ≤ m ≤ 2, 1 ≤ k ≤ 10 ), the following n lines describe the values of each element in each row of the matrix (the absolute value of each element's score cannot exceed 32767 ).
Output
Only one row is the maximum value of the score of K sub-matrix.
Sample input3 2 2
1-3
2 3
-2 3
Sample output9
Hint Source
Question:
Difficulties of DP:
1. It is the most important to use several-dimensional variables to represent the complete information of a State.
2. Status transfer enumeration: it is relatively easy to know what status the status may be transferred to or what status may be transferred.
If the status transfer is complicated, it may be that the status representation is too simple.
If you cannot think of a good method, you need to take the status into details.
For this question
F [I, j, k] indicates that I is obtained from the left column, J is obtained from the right, K is obtained.
Of course it is not all, or the decision on the left is I, and the decision on the right is J. The K decision should be made.
The transfer of status is very obvious.
Code:
1 uses math; 2 const maxn=100+10;maxk=15; 3 var g:array[0..maxn,0..maxk] of longint; 4 s:array[1..2,0..maxn] of longint; 5 f:array[0..maxn,0..maxn,0..maxk] of longint; 6 i,j,k,l,t,n,m,x:longint; 7 procedure init; 8 begin 9 readln(n,m,t);10 for i:=1 to n do11 begin12 for j:=1 to m do begin read(x);s[j,i]:=s[j,i-1]+x;end;13 readln;14 end;15 end;16 procedure work1;17 begin18 fillchar(g,sizeof(g),0);19 for k:=1 to t do20 for i:=k to n do21 begin22 g[i,k]:=g[i-1,k];23 for j:=k-1 to i-1 do24 g[i,k]:=max(g[i,k],g[j,k-1]+s[1,i]-s[1,j]);25 end;26 writeln(g[n,t]);27 end;28 procedure work2;29 begin30 fillchar(f,sizeof(f),0);31 for k:=1 to t do32 for i:=0 to n do33 for j:=0 to n do34 begin35 if (i=0) and (j=0) then continue;36 f[i,j,k]:=max(f[i-1,j,k],f[i,j-1,k]);37 for l:=0 to j-1 do38 f[i,j,k]:=max(f[i,j,k],f[i,l,k-1]+s[2,j]-s[2,l]);39 for l:=0 to i-1 do40 f[i,j,k]:=max(f[i,j,k],f[l,j,k-1]+s[1,i]-s[1,l]);41 if i=j then42 for l:=0 to i-1 do43 f[i,j,k]:=max(f[i,j,k],f[l,l,k-1]+s[1,i]-s[1,l]+s[2,j]-s[2,l]);44 end;45 writeln(f[n,n,t]);46 end;47 48 begin49 assign(input,‘input.txt‘);assign(output,‘output.txt‘);50 reset(input);rewrite(output);51 init;52 if m=1 then work1 else work2;53 close(input);close(output);54 end.
View code