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. IE浏览器兼容问题(下)——IE6的常见问题

    IE6常见兼容性问题 1.盒模型问题 (1)DTD问题 DTD:文档定义类型,规定了要遵循的书写规范. 如果不写DTD,高级浏览器还是可以正常加载,IE6会以怪异模式进行加载. 盒模型:正常应该是外扩 ...

  2. js数组 标签: javascript 2016-08-03 14:15 131人阅读 评论(0) 收藏

    数组排序 reverse()方法 reverse()方法会反转数组的顺序. sort()方法 默认情况下sort()方法按升序排列数组项.为实现排序sort()方法调用每项的toString(),然后 ...

  3. PHP小错误及分析

    即使是经验丰富的程序猿,在编程的过程中犯个错误也是在所难免的.如果开发人员不能注意到这些错误,也无法了解编译器报错信息的含义,那么这些错误信息不仅毫无用处,还会常常让人感到沮丧,所以更好地理解错误信息 ...

  4. Spark Broadcast内幕解密:Broadcast运行机制彻底解密、Broadcast源码解析、Broadcast最佳实践

    本课主题 Broadcast 运行原理图 Broadcast 源码解析 Broadcast 运行原理图 Broadcast 就是将数据从一个节点发送到其他的节点上; 例如 Driver 上有一张表,而 ...

  5. temp表空间被过多占用处理方法

    这个步骤比较简单,查询v$sort_usage就可以了: (select username,session_addr,sql_id,contents,segtype,blocks*8/1024/102 ...

  6. ThinkPHP最新版本SQL注入漏洞

    如下controller即可触发SQL注入: code 区域 public function test() { $uname = I('get.uname'); $u = M('user')-> ...

  7. HBase的rowkey排序和scan输出顺序

    本文目的:搞清楚HBase里面行与行之间的排序排序规则,如何正序和反序输出扫描结果. 明确: HBase里面同一列的元素按照rowkey进行排序,排序规则是rowkey的ASCII码排序,小的在前大的 ...

  8. 【[USACO09DEC]牛收费路径Cow Toll Paths】

    很妙的一道题,我之前一直是用一个非常暴力的做法 就是枚举点权跑堆优化dijkstra 但是询问次数太多了 于是一直只有50分 今天终于抄做了这道题,不贴代码了,只说一下对这道题的理解 首先点权和边权不 ...

  9. nrf52840蓝牙BLE5.0空中速率测试(nordic对nordic)

    一.基础知识: [1]Data Length:物理层发送一包数据的最大值: [2]MTU: ATT层发送一次数据长度的最大值: [3]GAP Event Length:一个connection eve ...

  10. 交叉熵Cross-Entropy

    1.交叉熵:用来描述通信中将一个概率分布的最优编码用到另一个概率分布的平均比特数 公式: 2.交叉熵是不对称的 3.交叉熵的作用是表达两个概率分布的差异性 设概率分布p(x)和q(x),两个概率分布差 ...