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-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

Source

Greater New York 2001

题意:

题解:

 1 /******************************
 2 code by drizzle
 3 blog: www.cnblogs.com/hsd-/
 4 ^ ^    ^ ^
 5  O      O
 6 ******************************/
 7 //#include<bits/stdc++.h>
 8 #include<iostream>
 9 #include<cstring>
10 #include<cmath>
11 #include<cstdio>
12 #define ll long long
13 #define mod 1000000007
14 #define PI acos(-1.0)
15 using namespace std;
16 int main()
17 {
18     int a[105][105];
19     int b[105];
20     int n;
21     while(scanf("%d",&n)!=EOF)
22     {
23         for(int i=1;i<=n;i++)
24         {
25             for(int j=1;j<=n;j++)
26                 scanf("%d",&a[i][j]);
27         }
28         int maxn=-100000;
29         for(int i=1;i<=n;i++)
30         {
31             memset(b,0,sizeof(b));
32             for(int j=i;j<=n;j++)
33             {
34                 int sum=0;
35                 for(int k=1;k<=n;k++)
36                 {
37                     b[k]+=a[j][k];
38                     sum+=b[k];
39                     if(sum<0) sum=b[k];
40                     if(sum>maxn) maxn=sum;
41                 }
42             }
43         }
44         printf("%d\n",maxn);
45     }
46     return 0;
47 }
时间: 2024-12-24 20:32:46

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): 10920    Accepted Submission(s): 5229 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