To the Max

  • Time Limit: 2000/1000 MS (Java/Others)
  • Memory Limit: 65536/32768 K (Java/Others)

Problem 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 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 * 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的二位数组Matrix,求该二维数组的最大子矩阵和。

题目分析

  • 暴力枚举

用4个循环,枚举出所有的子矩阵,再给每个子矩阵求和,找出最大的,肯定会超时,不可用。

时间复杂度:O(N^6)

  • 动态规划 (标准解法)

把二维转化为一维再求解。

有子矩阵:矩阵中第i行至第j行的矩阵。

用数组ColumnSum[k]记录子矩阵中第k列的和。

最后对ColumnSum算出最大子段和进行求解。

时间复杂度:O(N^3)

关于最大子段和:

有一序列a=a1 a2 ... an,求出该序列中最大的连续子序列。

比如序列1 -2 3 4 -5的最大子序列为3 4,和为3+4=7。

动态转移方程:DP[i]=max(DP[i-1]+a[i], a[i])

时间复杂度:O(N)

(最大子段和的具体过程网上有,我就不多说了)

代码

#include <cstdlib>
#include <cstdio>
using namespace std;
#define inf 0x7f7f7f7f
#define max(a, b) (((a)>(b))?(a):(b))
int N;
int Matrix[110][110];
int Answer = -inf;
int main()
{
scanf("%d", &N);
for(int i = 1; i <= N; ++ i)
for(int j = 1; j <= N; ++ j)
scanf("%d", &Matrix[i][j]);
for(int i = 1; i <= N; ++ i)
{
int ColumnSum[110] = {0};
for(int j = i; j <= N; ++ j)
{
int DP[110] = {0};
for(int k = 1; k <= N; ++ k)
{
ColumnSum[k] += Matrix[j][k];
// 求最大子段和
DP[k] = max(DP[k-1] + ColumnSum[k], ColumnSum[k]);
Answer = max(DP[k], Answer);
}
}
}
printf("%d\n", Answer);
return 0;
}

【HDOJ-1081】To The Max(动态规划)的更多相关文章

  1. HDU 1081 To The Max(动态规划)

    题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

  2. hdu 1081 To The Max(dp+化二维为一维)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Others ...

  3. HDOJ 1081(ZOJ 1074) To The Max(动态规划)

    Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...

  4. HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  5. POJ 1050 To the Max -- 动态规划

    题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...

  6. Hdoj 1176 免费馅饼 【动态规划】

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. dp - 最大子矩阵和 - HDU 1081 To The Max

    To The Max Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1081 Mean: 求N*N数字矩阵的最大子矩阵和. ana ...

  8. HDU 1081 To The Max【dp,思维】

    HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...

  9. Hdu 1081 To The Max

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

  10. [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

随机推荐

  1. Error: A JNI error has occurred, please check your installation and try again

    自己写的maven项目打包以后的一个email测试类jar,放到linux上运行时报错: Error: A JNI error has occurred, please check your inst ...

  2. Maven学习总结(一)——pom.xml文件配置详解

    <build>标签:<plugins>给出构建过程中所用到的插件 <plugins> <plugin> <groupId>org.apach ...

  3. July 11th 2017 Week 28th Tuesday

    No possession, but use, in the only riches. 真正的财富不是占有,而是使用. These days I have bought tens of books a ...

  4. 应用监控Metrics

    应用监控Metrics 一.Metrics简介        应用监控系统Metrics由Metrics.NET+InfluxDB+Grafana组合而成,通过客户端Metrics.NET在业务代码中 ...

  5. Android进阶笔记17:Android手机屏幕坐标系

    1. 手机屏幕坐标系: 整个坐标系是以手机屏幕左上角为原点(0,0),如下:

  6. 阅读MySQL文档第21章摘抄

    触发程序是与表相关的数据库对象. mysql> CREATE TABLE account (acct_num INT, amount DECIMAL(10,2)); Query OK, 0 ro ...

  7. interaction-oriented architecture - MVC

    MVC(Model-View-Controller),它是专门针 对交互系统提出的,所以如果我们要构建一个交互系统,那么我们就可以直接应用MVC模式,然后 在该模式所搭建的场景的启发下去发现Model ...

  8. @SuppressWarnings注解用法详解

    @SuppressWarnings注解用法详解 今天来谈谈@SuppressWarnings注解的作用. J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条 ...

  9. 搭建nlp_server服务器

    这是文档 如何启动斯坦福NLP-Service 1.sudo apt-get install gearman-job-server安装gearman-server 2.启动gearman服务: gea ...

  10. 面试准备——(二)专业知识(4)C/C++语言

    1. 预处理 断言 assert的功能,assert(statement),如果statement为真则程序继续执行,为假则整个程序中断退出 3. #define [ #ifndef DISKSIM_ ...