Description
Here is a n*m matrix, please select the K sub-matrix, so that the K sub-matrix score of the sum of the largest. Note: The selected K-matrices cannot overlap each other.
Input
The first behavior is n,m,k (1≤n≤100,1≤m≤2,1≤k≤10), and the next n lines describe the score of each element in each row of the matrix (the absolute value of each element does not exceed 32767).
Output
There is only one behavior k the sum of the sub-matrices is the maximum number.
Sample Input3 2 2
1-3
2 3
-2 3Sample Output9Ideas
See the title: Very difficult ah. No idea at all.
And then see the data range m<=2, are you kidding me? Why use rectangles to scare? is two one-dimensional maximum sub-matrix problem!
Then use f[i][j][k] to indicate that the first column uses I, and the second column uses J, which already has the maximum value of the K rectangle.
So f[i][j][k]=max{f[i ' -1][j][k-1]+sum[1][i '][i],f[i][j ' -1][k-1]+sum[2][j '][j]}
There is also a shift when i==j, that is, two lines together to form a rectangle.
F[i][i][k]=max{f[i ' -1][i ' -1][k-1]+sum[1][i '][i]+sum[2][i '][i]}
Where sum[i][j][k] means that column I from the number of J added to the number of K.
There is a special case is all negative, or the number of non-negative numbers is not enough K, first select all the non-negative, and then add the largest number of negative numbers on the line.
But there seems to be no such situation in the data tat. Cut open Sen.
1#include <iostream>2#include <cstring>3#include <string>4#include <cstdio>5#include <cstdlib>6#include <cmath>7#include <algorithm>8#include <queue>9#include <stack>Ten#include <map> One#include <Set> A#include <list> -#include <vector> -#include <ctime> the#include <functional> - #definePRITNF printf - #defineScafn scanf - #defineSACNF scanf + #definefor (i,j,k) for (int i= (j); i<= (k);(i) + +) - #defineClear (a) memset (A,0,sizeof (a)) + using namespacestd; Atypedef unsignedintUint; at Const intinf=0x3fffffff; - ///==============struct declaration============== - - ///==============var declaration================= - Const intmaxn= the; - introw,col,k,temp=0; in inta[maxn][Ten],sum[maxn][5]; - intf[maxn][maxn][ the]; to ///==============function declaration============ + intcmpintAintb) {returnA>b;} - ///==============main code======================= the intMain () * { $ #definefile__Panax Notoginseng #ifdef file__ -Freopen ("input","R", stdin); theFreopen ("Output","W", stdout); + #endif Ascanf"%d%d%d",&row,&col,&k); the intplus=0; + for(intI=1; i<=row;i++) - for(intj=1; j<=col;j++){ $scanf"%d",&a[i][j]); $sum[i][j]=sum[i-1][j]+A[i][j]; - if(a[i][j]>=0){ -plus++; thetemp+=A[i][j]; - }Wuyi } the for(intr1=1; r1<=row;r1++) - for(intR2=1; r2<=row;r2++) Wu for(intp=1;p <=k;p++){ -F[r1][r2][p]=max (f[r1][r2-1][p],f[r1-1][r2][p]); About for(intNewr=1; Newr<=max (R1,R2); newr++){ $ if(newr<=R1) -F[r1][r2][p]=max (f[r1][r2][p],f[newr-1][r2][p-1]+sum[r1][1]-sum[newr][1]); - if(newr<=R2) -F[r1][r2][p]=max (f[r1][r2][p],f[r1][newr-1][p-1]+sum[r2][2]-sum[newr][2]); A if(r1==R2) { +F[r1][r2][p]=max (f[newr-1][newr-1][p-1]+sum[r1][1]-sum[newr][1]+sum[r2][2]-sum[newr][2],f[r1][r2][p]); the } - } $ } the if(plus>=k) theprintf"%d\n", F[row][row][k]); the Else{ the intarr[maxn*2]; - for(intI=1; i<=row;i++){ inarr[i*2-1]=a[i][1];if(arr[i*2-1]>0) arr[i*2-1]=-INF; thearr[i*2]=a[i][2];if(arr[i*2]>0) arr[i*2]=-INF; the } AboutSort (arr+1, arr+1+row*2, CMP); the for(inti=plus+1; i<=k;i++) thetemp+=arr[i-Plus]; theprintf"%d\n", temp); + } - return 0; the }Bayi ///================fuction code====================
Bzoj 1084
"SCOI2005" Maximum sub-matrix Bzoj 1084