HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)
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
该题和之前做过的求最大连续子序列和有着共通之处,不同的地方是本问题求的是一个最大连续子矩阵,维度上变成了二维,那么我们能不能考虑压缩二维空间变为一维空间呢?
其实这是可以的。可以先选中矩阵的几行,逐列相加变为一个一维数组。(如果是一行的情况,则直接使用序列的最大连续段和方法)
多行变为一行时:例如
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
当i=0, j=2时,则选择0,1,2行,逐列相加压缩为一行a,再从a中寻找最长连续子序列和
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
a: 5 1 -17 3
该问题中i和j需要遍历所有的情况,压缩所有情况为一个一维数组!!!
import java.util.*;
public class test {
public static void main(String[] args) {
int[][] a = new int[100][100];
int[] b = new int[100];
int n;
Scanner in = new Scanner(System.in);
n = in.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
for (int k = 0; k < n; k++) {
b[k] = 0;
for (int l = i; l <= j; l++) {
b[k] += a[l][k];//合并i到j行
}
}
// 动态规划
int sum = 0;//当前和
int max = 0;//最大和
//dp[i] = max(dp[i], dp[i - 1] + a[i]);
for (int k = 0; k < n; k++) {
sum += b[k];// 含有第k个元素的最大连续子段和
if (sum > max) {
max = sum;
}
if (sum < 0) {
sum = 0;
}
}
if (max > ans) {//更新ans
ans = max;
}
}
}
System.out.println(ans);
}
}
HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)的更多相关文章
- hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)
Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...
- HDU 1081 To The Max【dp,思维】
HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...
- 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 1003 Max Sum【动态规划求最大子序列和详解 】
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 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 ...
- Hdu 1081 To The Max
To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1081 To The Max (dp)
题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- 【动态规划】最大连续子序列和,最大子矩阵和,最大m子段和
1.最大字段和问题 求一个序列最大连续子序列之和. 例如序列[-1,-2,-3,4,5,-6]的最大子段和为4 + 5 = 9. ①枚举法 int MaxSum(int n,int *a){ int ...
- 动态规划:最大连续子序列乘积 分类: c/c++ 算法 2014-09-30 17:03 656人阅读 评论(0) 收藏
题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 分析:若暴力求解,需要O(n^3)时间,太低效,故使用动态规划. 设data[i]:第i个数据,dp[i]:以第 ...
随机推荐
- 【Java笔试】对数据库中的分解是否为无损连接和是否保持函数依赖的判定-由牛客网试题引申-保姆式教学
[牛客网数据库原理题目]设关系模式R(A,B,C),F是R上成立的FD集,F={A→B,C→B},ρ={AB,AC}是R的一个分解,那么分解ρ()? 正确答案:C你的答案:A(错误) ( A ) 保持 ...
- 基于TCP协议之socket编程
#服务端 #导入一个socket模块 import socket #想象成买手机打电话:socket.SOCK_STREAM 表示建立tcp连接 ,udp连接socket.SOCK_DGRAM #买了 ...
- 《Java基础知识》Java包装类,拆箱和装箱
虽然 Java 语言是典型的面向对象编程语言,但其中的八种基本数据类型并不支持面向对象编程,基本类型的数据不具备“对象”的特性——不携带属性.没有方法可调用. 沿用它们只是为了迎合人类根深蒂固的习惯, ...
- python基础知识第一篇(认识Python)
开发语言: 高级语言:python java php c++ 生成的字节码 字节码转换为机器码 计算机识别运行 低级语言:C 汇编 生成的机器码 PHP语言:适用于网页,局限性 Python,Java ...
- 关于腾讯云Centos的一些操作
安装mysql wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-commun ...
- Dubbo环境搭建-ZooKeeper注册中心
场景 Dubbo简介与基本概念: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103555224 注: 博客: https:// ...
- UiPath Platform注册 登录 及 访问 Orchestrator
相关步骤: 1.https://platform.uipath.com/portal_/cloudrpa 注册 及 登录 2. Login后 通过Services 连接 访问 UiPath Orche ...
- (办公)记事本_Linux帮助命令
参考:http://www.gulixueyuan.com/course/300/task/7086/show# 帮助命令: .man命令 1.1.man命令是Linux下的帮助指令,通过man指令可 ...
- PHP中使用date获取上月最后一天出现的问题
上次做项目时,发现一个问题,这里记录一下: 问题: 在使用date函数获取上一个月最后一天或下个月最后一天时,如果当前日期是31号,获取的数据有问题. // 2019-12-01 正确应该是 2019 ...
- Mysql 的异常:The last packet successfully received from the server was 90 milliseconds ago. The last packet sent successfully to the server was 43,603,303 milliseconds ago. is longer than the server con
调试一个程序, 调试到一半, 下班回家, 程序卡在了某一行, 第二天早上回来一看, 发现了异常: Wed Sep :: GMT+: WARN: Establishing SSL connection ...