【HDOJ-1081】To The Max(动态规划)
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(动态规划)的更多相关文章
- HDU 1081 To The Max(动态规划)
题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- 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 ...
- HDOJ 1081(ZOJ 1074) To The Max(动态规划)
Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...
- HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- POJ 1050 To the Max -- 动态规划
题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...
- Hdoj 1176 免费馅饼 【动态规划】
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 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 ...
- HDU 1081 To The Max【dp,思维】
HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...
- Hdu 1081 To The Max
To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
随机推荐
- 网络分析 ANP
在许多实际问题中,各层次内部元素往往是依赖的. 低层元素对高层元素亦有支配作用,即存在反馈. 此时系统的结构更类似于网络结构.网络分析法正是适应这种需要,由AHP延伸发展得到的系统决策方法. AN ...
- ubuntu 18.4 鼠标右键菜单 添加文件
执行以下指令,在template文件夹中,增加一个空文件 touch ~/Templates/Empty\ Document
- MS15-051 修正版Exploit(Webshell可用)
MS15-051简介:Windows 内核模式驱动程序中的漏洞可能允许特权提升 (3057191) , 如果攻击者在本地登录并可以在内核模式下运行任意代码,最严重的漏洞可能允许特权提升. 攻击者可随后 ...
- day13 面向对象练习
//1 public class Area_interface { public static void main(String[] args) { Rect r = new Rect(15, 12) ...
- 【转】Linux vmstat命令实战详解
好久没写博客了,上个月忙的晕头转向的实在没有心情.最近会发几篇PowerDNS的,先预告一下. 这篇是纯转的,原贴地址:http://www.cnblogs.com/ggjucheng/archive ...
- JavaScript:回调模式(Callback Pattern) (转载)
JavaScript:回调模式(Callback Pattern) 函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode() ...
- hdu 1021 Fibonacci Again(变形的斐波那契)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1021 Fibonacci Again Time Limit: 2000/1000 MS (Java/Ot ...
- @class CLASS ;必须加分号,且卸载@interface之前
1. @class CLASS ;必须加分号,且卸载@interface之前 示例代码如下: #import <UIKit/UIKit.h> @class WZProduct; //注意 ...
- M4修改外部晶振8M和25M晶振的方法
共计修改三个参数: 1.HSE_VALUE 具体位置在stm32f4xx.h中 2.PLL_M 具体位置在system_stm32f4xx.c中 3.Keil编译器 工程的Opt ...
- 在js中获取到的页面元素为undefined
在学习js的过程中发现了一个问题就是:在js代码中获取页面元素进行操作的时候发现怎么都没有效果,控制台也不报错,弹出获取的元素结果发现是undefined类型. 后来查找了资料发现:因为我的js是写在 ...