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 最大子矩阵(动态规划求最大连续子序列和)的更多相关文章

  1. hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)

    Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...

  2. HDU 1081 To The Max【dp,思维】

    HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...

  3. 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 ...

  4. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. 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 ...

  6. Hdu 1081 To The Max

    To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. HDU 1081 To The Max (dp)

    题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

  8. 【动态规划】最大连续子序列和,最大子矩阵和,最大m子段和

    1.最大字段和问题 求一个序列最大连续子序列之和. 例如序列[-1,-2,-3,4,5,-6]的最大子段和为4 + 5 = 9. ①枚举法 int MaxSum(int n,int *a){ int ...

  9. 动态规划:最大连续子序列乘积 分类: c/c++ 算法 2014-09-30 17:03 656人阅读 评论(0) 收藏

    题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 分析:若暴力求解,需要O(n^3)时间,太低效,故使用动态规划. 设data[i]:第i个数据,dp[i]:以第 ...

随机推荐

  1. poj 1321 棋盘问题 (回溯法)

    棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 69951   Accepted: 33143 Descriptio ...

  2. django基础之day05,F与Q查询,Q查询的高级用法

    #F与Q查询 #*************************** F 查询 ******************** # F 查询数据库中的其他字段!!! #1.查询库存数大于卖出数的书籍 fr ...

  3. 聊聊 Python 的内置电池

    本文原创并首发于公众号[Python猫],未经授权,请勿转载. 原文地址:https://mp.weixin.qq.com/s/XzCqoCvcpFJt4A-E4WMqaA (一) 最近,我突然想到一 ...

  4. CentOS7 安装 Redis 并设置开机启动

    1.下载 https://redis.io/download cd /usr/local/src wget -c http://download.redis.io/releases/redis-3.2 ...

  5. mysql多字段内容并到单字段中的操作

    ; SELECT 序号, ryxm `人员姓名`, cylb `成员类别`, gzdw `工作单位`, zc `职称`, GROUP_CONCAT(zzqmc) AS `著作权名称`--多字段合并到一 ...

  6. 记录MyBatis text类型 查询 更新 数据是null

    数据库表里面存在text或者blob字段.逆向工程自动生成的MyBatis的xml中会多出几个以withBlobs结尾的方法和resultMap 此时查询数据或者更新数据的使用仍然使用selectBy ...

  7. Android组件体系之BroadcastReceiver小结

    1.常见分类    BroadCastReceiver,按注册方式可以分为静态广播接收器和动态广播接收器.    静态广播接收器:不受程序是否启动的约束,当应用程序关闭之后,还是可以接收到广播(一般广 ...

  8. Hive初步认识,理解Hive(一)

    Hive初步认识,理解Hive(一) 用了有一段时间的Hive了,之前一直以为hive是个数据库,类似Mysql.Oracle等数据库一样,其实不然. Hive是实现Hadoop 的MapReduce ...

  9. CentOS7环境下安装docker

    操作系统 : CentOS7.5.1804_x64 docker版本: docker-ce-18.06.3 准备环境 1.如之前安装过移除老旧版本 yum remove docker docker-c ...

  10. WPF应用中对WindowsFormHost内容进行裁剪

    问题1:  WPF中在使用WindowsFormsHost调用WinFrom控件时,若在WindowsFormsHost上层添加了WPF控件,该控件不会显示出来. <Grid> <W ...