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]:以第 ...
随机推荐
- var和let部分浅析
ES6中新增了let命令,用于声明变量,但所声明的变量只在let命令的代码块内有效. 举个例子: var a = []; for(var i=0;i<10;i++){ a[i] = functi ...
- Blockchain 基本知识
本文是前奏,本来要介绍Azure上的Azure Blockchain Service,发现,需要从什么是区块链开始讲起... 什么是区块链?我们从比特币说起, 2008年11月,中本聪提出了比特币白皮 ...
- ASP.NET CORE 使用Consul实现服务治理与健康检查(2)——源码篇
题外话 笔者有个习惯,就是在接触新的东西时,一定要先搞清楚新事物的基本概念和背景,对之有个相对全面的了解之后再开始进入实际的编码,这样做最主要的原因是尽量避免由于对新事物的认知误区导致更大的缺陷,Bu ...
- 如何编写一个工程文件夹下通用的Makefile
新建工程文件夹,在里面新建 bsp.imx6ul.obj 和project 这 3 个文件夹,完成以后如图所示: 新建的工程根目录文件夹 其中 bsp 用来存放驱动文件:imx6ul 用来存放跟芯片有 ...
- centos6.5安装supervisor
centos6.5安装supervisor,有很多种方法,但是有很多坑,为了以后不重复踩坑,这里记录一下. 一.如果用yum install supervisor, 默认安装的是2.1.9版本,2.x ...
- html中的框架frameset和frame及iframe
通过使用框架,你可以在同一个浏览器窗口中显示不止一个页面. 通过使用框架,你可以在同一个浏览器窗口中显示不止一个页面,简而言之,就是在一个窗口中显示多个页面. 每个页面称之为一个框架.并且每个框架独立 ...
- oracle 11gR2 RAC 停库和启库
grid设置环境变量后可以在任意目录下执行,如root没设置的话需要带绝对路径export ORACLE_HOME=/u01/app/11.2.0/gridexport PATH=$ORACLE_HO ...
- PWA 学习笔记(五)
离线与缓存 资源请求的拦截代理: 1.资源请求的判断: (1)fetch 事件会拦截页面上所有的网络资源请求,但我们通常只对部分资源请求进行处理, 其余的请求会继续走浏览器默认的资源请求流程 (2)f ...
- sql server一些快捷方式和操作技巧
1.注释(ctrl+k+c) 和 取消注释(ctrl+k+u) 2.行号显示,如图:
- stream根据条件过滤List<Object>
List<String> filterUser= new ArrayList<>(); filterUser.add("张三"); List<User ...