【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 ...
随机推荐
- 杨氏矩阵C++实现
何为杨氏矩阵?这个网上的介绍很多,下面给出杨氏矩阵搜索算法: #include <iostream> using namespace std; // 杨氏矩阵查找算法 ], int N, ...
- DOS下常用命令
0,想进入某个驱动器,直接输入盘符即可.如:“d:”1,CD--进入指定目录 2,cls--清除显示器屏幕上的内容,使DOS提示符到屏幕左上角. 3,time--显示和设置DOS的系统时间 4,dir ...
- January 23 2017 Week 4 Monday
Knowledge is long, life is short. 吾生也有涯,而知也无涯. I often feel that I have a lot of things to learn, ne ...
- 关于项目中的DAL数据接入层架构设计
摘要:项目中对关系型数据库的接入再寻常不过,也有海量的ORM工具可供选择,一个一般性的DAL数据接入层的结构却大同小异,这里就分享一下使用Hibernate.Spring.Hessian这三大工具对D ...
- Ajax向Controller发送请求并接受数据需要注意的一个细节
想用Ajax想向Controller发送请求和接收返回的字符等等.Controller中要使用@ResponseBody注解. <script type="text/javascrip ...
- Django 导出csv文件 中文乱码问题
import csvimport codecsimport datetimefrom django.db import connectionfrom django.contrib.auth.model ...
- checkbox的readonly属性设置
方式一: checkbox没有readOnly属性,如果使用disabled=“disabled”属性的话,会让checkbox变成灰色的,用户很反感这种样式可以这样让它保持只读: 设置它的oncli ...
- Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round 4) C. Connect Three 【模拟】
传送门:http://codeforces.com/contest/1087/problem/C C. Connect Three time limit per test 1 second memor ...
- [转]HTTP报文接口及客户端和服务器端交互原理
1. 协议 a. TCP/IP整体构架概述 TCP/IP协议并不完全符合OSI的七层参考模型.传统的开放式系统互连参考模型,是一种通信协议的7层抽象的参考模型,其中每一层执行某一特定任务.该模型的目的 ...
- Java实现 lower_bound() 和 upper_bound()
Java实现 lower_bound() 和 upper_bound() lower_bound() 函数 lower_bound() 在 [begin, end) 进行二分查找,返回 大于或等于 t ...