1084: [SCOI2005] Maximum sub-matrix

Source: Internet
Author: User

1084: [SCOI2005] Maximum sub-matrixDescription

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-sub-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
The absolute value of the score 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 Output9acty true God Cow!!! (Everyone can go to the Bzoj search)—————— the following ————————m Max is only 2pressure bar, with 0,1,2,3,4 to indicate the state of a row0: No one is selected1: Select the left one2: Select the right one3:2 is selected as a whole4:2 separate to chooseDP transfer is not much to say ... The code is a bit ugly, endure it.
#include <stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespacestd;Const intmin=-2000000000;intn,m,v,k,i,j,l,ans,a[ the][3],f[ the][ the][5];intMain () {scanf ("%d%d%d",&n,&m,&v);  for(i=1; i<=n;i++)      for(j=1; j<=m;j++) scanf ("%d",&A[i][j]);  for(i=1; i<=n;i++)      for(j=0; j<=v;j++)       for(k=0; k<=4; k++) F[i][j][k]=Min; f[1][0][0]=0; f[1][1][1]=a[1][1]; f[1][1][2]=a[1][2]; f[1][1][3]=a[1][1]+a[1][2]; f[1][2][4]=a[1][1]+a[1][2];  for(i=1; i<n;i++)      for(j=0; j<=v;j++)     {         if(m==1)         {              for(k=0; k<=1; k++) f[i+1][j][0]=max (f[i+1][j][0],f[i][j][k]); F[i+1][j][1]=max (f[i+1][j][1],f[i][j][1]+a[i+1][1]); F[i+1][j+1][1]=max (f[i+1][j+1][1],f[i][j][1]+a[i+1][1]); F[i+1][j+1][1]=max (f[i+1][j+1][1],f[i][j][0]+a[i+1][1]); //cout<<f[4][3][1]<< ' <<i<< ' <<j<< ' <<f[i][j][0]<< ' << f[i][j][1]<<endl;}Else         {             //0              for(k=0; k<=4; k++) f[i+1][j][0]=max (f[i+1][j][0],f[i][j][k]); //1f[i+1][j][1]=max (f[i+1][j][1],f[i][j][1]+a[i+1][1]); F[i+1][j][1]=max (f[i+1][j][1],f[i][j][4]+a[i+1][1]); F[i+1][j+1][1]=max (f[i+1][j+1][1],f[i][j][1]+a[i+1][1]); F[i+1][j+1][1]=max (f[i+1][j+1][1],f[i][j][0]+a[i+1][1]); F[i+1][j+1][1]=max (f[i+1][j+1][1],f[i][j][4]+a[i+1][1]); //2f[i+1][j][2]=max (f[i+1][j][2],f[i][j][2]+a[i+1][2]); F[i+1][j][2]=max (f[i+1][j][2],f[i][j][4]+a[i+1][2]); F[i+1][j+1][2]=max (f[i+1][j+1][2],f[i][j][0]+a[i+1][2]); F[i+1][j+1][2]=max (f[i+1][j+1][2],f[i][j][2]+a[i+1][2]); F[i+1][j+1][2]=max (f[i+1][j+1][2],f[i][j][4]+a[i+1][2]); //3f[i+1][j][3]=max (f[i+1][j][3],f[i][j][3]+a[i+1][1]+a[i+1][2]); F[i+1][j+1][3]=max (f[i+1][j+1][3],f[i][j][0]+a[i+1][1]+a[i+1][2]); F[i+1][j+1][3]=max (f[i+1][j+1][3],f[i][j][3]+a[i+1][1]+a[i+1][2]); //4f[i+1][j][4]=max (f[i+1][j][4],f[i][j][4]+a[i+1][1]+a[i+1][2]); F[i+1][j+1][4]=max (f[i+1][j+1][4],f[i][j][1]+a[i+1][1]+a[i+1][2]); F[i+1][j+1][4]=max (f[i+1][j+1][4],f[i][j][2]+a[i+1][1]+a[i+1][2]); F[i+1][j+2][4]=max (f[i+1][j+2][4],f[i][j][0]+a[i+1][1]+a[i+1][2]); F[i+1][j+2][4]=max (f[i+1][j+2][4],f[i][j][1]+a[i+1][1]+a[i+1][2]); F[i+1][j+2][4]=max (f[i+1][j+2][4],f[i][j][2]+a[i+1][1]+a[i+1][2]); F[i+1][j+2][4]=max (f[i+1][j+2][4],f[i][j][4]+a[i+1][1]+a[i+1][2]); }} ans=max (f[n][v][0],f[n][v][1]); if(m==2)     for(i=2; i<=4; i++) ans=Max (ans,f[n][v][i]); cout<<ans; return 0;}

1084: [SCOI2005] Maximum sub-matrix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.