hdu 1081 To The Max(最大子矩阵)

原创 2015年11月18日 00:50:09

To The Max

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


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
 


#include<cstring>
#include<cstdio>
#include<iostream>
#define N 111
#define INF 1e8

using namespace std;

int sum[N][N];
int n;

int main() {
    //freopen("test.in","r",stdin);
    while(~scanf("%d",&n)) {
        memset(sum,0,sizeof sum);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++) {
                int x;
                scanf("%d",&x);
                sum[i][j]=sum[i][j-1]+x;
            }
        int ans=-INF;
        for(int l=1; l<=n; l++) {
            for(int r=l; r<=n; r++) {
                int all=0;
                for(int i=1; i<=n; i++) {
                    all+=sum[i][r]-sum[i][l-1];
                    ans=max(ans,all);
                    if(all<0)all=0;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

hdu 1081 To The Max 最大子矩阵和(dp)

题意: 代码: #include #include #include #include #include #include #include #include...

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

题意:输入一个二维数组,求和最大的子矩阵 分析:一看到和最大的子XX,我就联想到和最大子序列,只不过那是一维这是二维,所以做法都差不多。把二维压缩成一维:你想啊一个矩阵的和不是可以先垂直方向相加把所有...

hdu1081 To The Max(动态规划-最大子矩阵)

20分钟就a了,突然感觉自己好强啊,嗯♂?先是最基本的思路,枚举所有子矩阵,必定超时计算所有子矩阵中的计算有大量重复,尝试定义状态dp[i][j],表示以i行j列元素为矩阵右下角的子矩阵所得到的最大子...
  • zjtzyrc
  • zjtzyrc
  • 2015年04月03日 13:02
  • 156

hdu 1081 To The Max 【最大子矩阵和】

To The MaxTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total...

【最大子矩阵和】HDU1081-To The Max

To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot...

hdu 1081 To The Max ( 最大子矩阵 )

To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su...

hdu-1081 To The Max (最大子矩阵和)

http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Other...
  • wudi_00
  • wudi_00
  • 2016年07月17日 16:19
  • 62

hdu 1081 To The Max ****poj 1050(最大子矩阵和)DP

最大子矩阵和 思想:从i到j (1 #include #include using namespace std; int main() { int n,matrix[120][...
  • wconvey
  • wconvey
  • 2012年04月10日 15:27
  • 324

hdu1081 To The Max--DP(最大子矩阵和)

原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=1081 一:原题内容 Problem Description Given a...
  • LaoJiu_
  • LaoJiu_
  • 2016年03月27日 09:55
  • 282

经典动态规划——HDU1081 To The Max 最大子矩阵问题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 题目大意:给定1个n*n矩阵,求最大子矩阵(矩阵元素之和最大)。 解题思路: 这个题目和...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1081 To The Max(最大子矩阵)
举报原因:
原因补充:

(最多只允许输入30个字)