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. deconstructSigs|探寻cosmic的独特“气质”-mutation signature !

    deconstructSigs-mutation signature看一下你的数据是什么“气质”的? 本文首发于“生信补给站” https://mp.weixin.qq.com/s/k7yzk9hPX ...

  2. 牛客练习赛32B Xor Path (树形dp)

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 给定一棵n个点的树,每个点有权值.定义表示 ...

  3. Nginx代理缓存功能

    Nginx代理缓存功能      Nginx缓存主要是用于减轻后端服务器的负载,提高网站并发量,提升用户体验度. 注意:Nginx反向代理的缓存功能是由ngx_http_proxy_module提供, ...

  4. C#界面设计相关设置

    1.Anchor属性设置 对需要设置的控件,如主窗体中的TextBox,设置Anchor为上下左右都停靠,就会实现随着窗体的变化而变化. 2.AutoScaleMode属性的用法:<转自:htt ...

  5. 松软科技Web课堂:JavaScript While 循环

    只要条件为 true,循环能够一直执行代码块. While 循环 while 循环会一直循环代码块,只要指定的条件为 true. 语法 while (条件) { 要执行的代码块 } 实例 在下面的例子 ...

  6. 报错解决 unable to unroll loop, loop does not appear to terminate in a timely manner (994 iterations) or unrolled loop is too large, use the [unroll(n)] attribute to force an exact higher number

    在 Unity 写 Shader 的时候,在一个循环里面使用了 tex2D 函数,类似与下面这样: fixed2 center = fixed2(0.5,0.5); fixed2 uv = i.uv ...

  7. vue组件之间的通信方式

    组件之间的通信方式有很多种 这里分享4种组件之间的通信方式 props(主要是父传子)  自定义事件(主要是子传父)  pubsub消息订阅与发布  xuex 1.props和自定义事件 app.vu ...

  8. .Net Core 项目发布到Linux - CentOS 7(二)用Supervisor守护netcore进程

    简介 supervisor可以保证程序崩溃后,可以重新把程序启动起来等相关功能. 安装 yum install -y supervisor 安装好后在/etc/会生成一个supervisord.con ...

  9. C#中的Skip()和Take()

    Skip()和Take()方法都是IEnumerable<T> 接口的扩展方法,包括C#中的所有Collections类,如ArrayList,Queue,Stack等等,还有数组和字符串 ...

  10. Lucene&Solr框架之第一篇

    2.信息检索 信息检索是计算机世界中非常重要的一种功能.信息检索不仅仅是指从数据库检索数据,还包括从文件.网页.邮件.用户手输入的内容中检索数据.通过怎样的高效方式将用户想要的信息快速提取出来,是计算 ...