码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1081最大子矩阵的和DP

时间:2014-08-16 17:12:10      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:acm   dp   

To The Max

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8210    Accepted Submission(s): 3991


Problem Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.

As an example, the maximal sub-rectangle of the array:

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

is in the lower left corner:

9 2
-4 1
-1 8

and has a sum of 15.
 

Input
The input consists of an N x N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N 2 integers separated by whitespace (spaces and newlines). These are the N 2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
 

Output
Output the sum of the maximal sub-rectangle.
 

Sample Input
4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2
 

Sample Output
15
这道题题意很清楚:给一个N*N的矩阵,求它的子矩阵(元素和最大);
(1)首先得清楚 对于一维数组如何求它的最大子段和;
int sum=0,Max=-9999999;
for(int i=1;i<=n;i++)//a数组一共用n个元素,遍历一遍即可
{
 if(sum<0)sum=0; //如果sum都小于0了,前面的就可以不要了,从头再来,初始化0
 sum+=a[i];
 if(sum>Max)Max=sum;   //不断更新Max,最终的Max就是最大字段和
} 
(2)清楚一维最大字段和的解法之后,再接着看二维的,经典方法就是将二维的压缩为一维的,如果这个压缩过程想明白了就简单了;
举个例子:4*4的矩阵
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
1.将第一列看成一维的求出最大的Max;
2.将第一列和第二列合并 看成一维的求出最大的Max
3.将第一列,第二列,第三列合并,看成一维的求出最大的Max;
4.将第一列,第二列,第三列,第四列合并,看成一维的求出最大的Max;
 

5.将第二列看成一维 求最值
6.将第二列,第三列合并成一维的求最值
7.将第二列,第三列,第四列合并一维 求最值

8.将第三列看成一维的,求最值
9.将第三列,第四合并一维求最值;

10.将第四列看成一维的,求最值
End ~~~~~~压缩过程就是这样的~~应该能看懂!!! 那么 压缩之前稍微处理一下: dp[i][j]表示 第i行前j列元素之和;
那么比如合并 第二列和第三列的时候就是这样 : dp[1][3]-dp[1][2-1] 就是说第一行的前3个元素的和减去第一行的前1个元素的和当作一维的处理~~~依次类推


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
const int maxn=110;
using namespace std;
int dp[maxn][maxn];
int main()
{
 int n;
 while(cin>>n)
 {
  memset(dp,0,sizeof(dp));
  for(int i=1;i<=n;i++)  //dp[i][j]表示第i行的前j个元素
   for(int j=1;j<=n;j++)
   {
    cin>>dp[i][j];
    dp[i][j]+=dp[i][j-1];
   }

  int Max=-99999999;
  for(int i=1;i<=n;i++)
   for(int j=i;j<=n;j++)
   {
     int sum=0;
     for(int k=1;k<=n;k++)
     {
       if(sum<0)sum=0;
       sum+=dp[k][j]-dp[k][i-1];  //dp[k][j]-dp[k][i-1]表示第k行第i到第j个元素的和
       if(sum>Max)Max=sum;
     }
   }
   cout<<Max<<endl;
 }
 return 0;
}


hdu 1081最大子矩阵的和DP,布布扣,bubuko.com

hdu 1081最大子矩阵的和DP

标签:acm   dp   

原文地址:http://blog.csdn.net/liusuangeng/article/details/38614037

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!