1084: [SCOI2005] Maximum sub-matrix time limit:10 Sec Memory limit:162 MB
submit:1533 solved:773
[Submit] [Status] [Discuss] 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 line 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 Output91<=m<=2 .... directly classified discussion, but m=2 's idea is really strange (perhaps I see little) did not think out. The m=1 is very weak. first, regardless of M equals a few, the prefix and is maintained for each column. M=1, it is easy to think of f[i][k] means the first I is divided into J (to note that each paragraph can be adjacent), F[i][k]=max (F[j][k-1]+sum[i]-sum[j]) (j<i). Final output F[N][K];for m=2, F[i][j][k] represents the first I row of column one, the first J row of the second column, divided into K-matrix maximum and, F[i][j][k]=max (f[l][j][k-1]+s1[i]-s1[l],f[i][l][k-1]+s2[j]- S2[l]) (l<i;l<j;i!=j) F[i][j][k]=max (F[l][j][k-1]+s1[i]-s1[l],f[i][l][k-1]+s2[j]-s2[l],f[l][l]+s1[i]-s1[l] +s2[j]-s2[l]) (L<I;L<J,I=J); final output F[N][N][K];
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;intn,m,kk,a[1001],b[1001],s[1001],s1[1001],s2[1001],dp[1001][ One],f[1001][1001][ One];intMain () {scanf ("%d%d%d",&n,&m,&KK); if(m==1) { for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); S[i]=s[i-1]+A[i]; } for(intI=1; i<=n;i++) for(intk=1; k<=kk;k++) {Dp[i][k]=dp[i-1][k]; for(intj=0; j<i;j++) Dp[i][k]=max (dp[i][k],dp[j][k-1]+s[i]-S[j]); } printf ("%d", Dp[n][kk]); } if(m==2) { for(intI=1; i<=n;i++) {scanf ("%d%d",&a[i],&B[i]); S1[i]=s1[i-1]+A[i]; S2[i]=s2[i-1]+B[i]; } for(intk=1; k<=kk;k++) for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) {F[i][j][k]=max (f[i-1][j][k],f[i][j-1][k]); for(intL=0; l<i;l++) F[i][j][k]=max (f[i][j][k],f[l][j][k-1]+s1[i]-S1[l]); for(intL=0; l<j;l++) F[i][j][k]=max (f[i][j][k],f[i][l][k-1]+s2[j]-S2[l]); if(i==j) for(intL=0; l<i;l++) F[i][j][k]=max (f[i][j][k],f[l][l][k-1]+s1[i]-s1[l]+s2[j]-S2[l]); } printf ("%d", F[n][n][kk]); } return 0;}
[SCOI2005] [BZOJ1084] Maximum sub-matrix