HDOJ To The Max 1081【动态规划-详解求最大子矩阵】


To The Max

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


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
 


经典动态规划:

求最大子矩阵。

解题思路:

①主要是先会求一维的,然后把二维的看成一维的计算即可。递推公式: d [ i ][ j ] 代表的 i 是起始行,j 是终止行。把i-j行进行捆绑,然后考虑成一维的即可。

先看一维是怎么算的,设有数组a0,a1…an,找除其中连续的子段,使它们的和达到最大。

假如,t[i]表示以ai结尾的子段中的最大子段和。

在已知t[i]的情况下,求t[i+1]的方法是:

如果t[i]>0, t [i+1]= t[i]+ai(继续在前一个子段上加上ai),否则t[i+1]=ai(不加上前面的子段),也就是说

状态转移方程为:

t[i] = (t[i-1]>0?t[i-1]:0)+a[i];

②只要求出一维的,二维的如果我们知道如何进行捆绑那么这题就可以解决了。

由于是 2 3 4行,所以我们可以将这3行”捆绑”起来,变为求 4(9-4-1),11(8+2+1),-10(-6-4+0),7(7+2-2)的最大子段和,ok,问题成功转化为一维的情况!然后就按一维求解即可。

③捆绑的时候就是i 到 j 行的所有同一列的元素相加之后的值,看成是一个一维上的值,然后我们就可以利用一维的状态转移方程进行求解了。



AC代码:


#include <stdio.h>
#include <math.h>
#include <vector>
#include <queue>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;

int get(int f[110],int n)
{
	int t[110];
	int maxn=-99999999;
	memset(t,0,sizeof(t));
	for(int i=1;i<=n;i++)
	{
		t[i]=(t[i-1]>0?t[i-1]:0)+f[i];
		if(maxn<t[i])
			maxn=t[i];
	}
	return maxn;
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        int f[110];
        int v[110][110];
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&v[i][j]);
            }
        }
        int ans=-99999999;
        for(int i=1;i<=n;i++){
            for(int j=i;j<=n;j++){
                memset(f,0,sizeof(f));
                for(int q=1;q<=n;q++){
                    for(int k=i;k<=j;k++)f[q]+=v[k][q];
                }
                ans=max(ans,get(f,n));
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}




你可能感兴趣的:(HDOJ To The Max 1081【动态规划-详解求最大子矩阵】)