hdu 1081(最大子矩阵)

To The Max

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

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

实质上是最大子段和问题,最大子矩阵我们将其所有的行都枚举出来,然后将其合并成一行,然后就可以用最大连续子段和求最大值,得到最大子矩阵。

最大子段和:
    令b[j]表示以位置 j 为终点的所有子区间中和最大的一个
    子问题:如j为终点的最大子区间包含了位置j-1,则以j-1为终点的最大子区间必然包括在其中
    如果b[j-1] >0, 那么显然b[j] = b[j-1] + a[j],用之前最大的一个加上a[j]即可,因为a[j]必须包含
    如果b[j-1]<=0,那么b[j] = a[j] ,因为既然最大,前面的负数必然不能使你更大

   状态转移方程 dp[j] = max(dp[j-1]+a[j],a[j])(0<j<=n)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include <string.h>
#include <math.h>
using namespace std;
const int N = 101;
int mp[N][N],b[N];
int n;

int getMax()
{
    int t = 0,mx = -1;
    int dp[N+1]= {0};
    for(int i=1; i<=n; i++)///从1开始枚举
    {
        if(dp[i-1]>0) dp[i] = dp[i-1]+b[i-1];
        else dp[i]=b[i-1];
        mx = max(mx,dp[i]);
    }
    return mx;
}
int solve()
{
    int mx = -1;
    for(int i=0; i<n; i++)
    {
        for(int j=i; j<n; j++)
        {
            memset(b,0,sizeof(b));
            for(int k=0; k<n; k++)
                for(int l=i; l<=j; l++)
                    b[k]+=mp[l][k];
            mx = max(mx,getMax());
        }
    }
    return mx;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
                scanf("%d",&mp[i][j]);
        }
        int mx = solve();
        printf("%d\n",mx);
    }
    return 0;
}
时间: 2024-08-30 00:18:30

hdu 1081(最大子矩阵)的相关文章

hdu 1081最大子矩阵的和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-rectan

HDU 1081 最大子矩阵和

To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11715    Accepted Submission(s): 5661 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectan

HDU 1003 &amp;&amp; HDU 1081(最大子列和,最大子矩阵和).

最大子列和,及其扩展最大子矩阵和,都是比较经典的dp,把这两题写在一起,共大家参考学习. ~~~~ lz弱菜啊,到现在还只能写这么水的DP...orz. ~~~~ 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 http://acm.hdu.edu.cn/showproblem.php?pid=1081 题意应该比较好理解,不解释了. 1003: #include<cstdio> #include<iostream> #inc

hdu 1081 &amp; poj 1050 To The Max(最大和的子矩阵)

转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum

HDU 1081 To The Max(DP)

题意  求一个n*n矩阵的最大子矩阵和 HDU 1003 max sum 的升级版   把二维简化为一维就可以用1003的方法去做了  用mat[i][j]存  第i行前j个数的和   那么mat[k][j]-mat[k][i]就表示第k行  第i+1个数到第j个数的和了   再将k从一枚举到n就可以得到这个这个宽度为j-i的最大矩阵和了   然后i,j又分别从1枚举到n就能得到结果了   和1003的方法一样  只是多了两层循环 #include<cstdio> #include<cs

hdu 1559最大子矩阵

一直很少练dp~这几天再学学~~ 在本题中:a[i][j]的值表示左上角为(1,1)右下角为(i,j)的矩阵的所有元素之和~ 给你一个m×n的整数矩阵,在上面找一个x×y的子矩阵,使子矩阵中所有元素的和最大. Input 输入数据的第一行为一个正整数T,表示有T组测试数据.每一组测试数据的第一行为四个正整数m,n,x,y(0<m,n<1000 AND 0<x<=m AND 0<y<=n),表示给定的矩形有m行n列.接下来这个矩阵,有m行,每行有n个不大于1000的正整数

hdu1559,1081最大子矩阵和的两种题型

最大子矩阵是一种典型的dp问题.某种程度上说是最大连续子序列和问题的扩展. 1081 原题地址 这是最常见的最大子矩阵问题的体型.简单的解决方案就是把列累加,遍历任意两行的累加值的差值,然后就转换成了普通的最大连续子序列和问题.从而将二维问题转换为一维.时间复杂度较高为O(N^3) 代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAX=10

hdu 1081

To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7927    Accepted Submission(s): 3831 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectang

HDU 1081 To The Max--DP--(最大子矩阵)

题意:输入一个二维数组,求和最大的子矩阵 分析:一看到和最大的子XX,我就联想到和最大子序列,只不过那是一维这是二维,所以做法都差不多.把二维压缩成一维:你想啊一个矩阵的和不是可以先垂直方向相加把所有行压缩为一行然后这一行相加嘛.压缩过后找最大和的方法跟一维一模一样.但我自己做的时候写了四个循环,唉.....数组可以存放之前求过的和,那么求以下一行为结尾的和的时候只要在原来的数组上加这一行的数就行了,不需要从头循环求和! dp[k]=a[i][k]+.....+a[j][k] 第 k 列第 i