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]:以第 ...
随机推荐
- skipping archived logs of thread 1 from sequence 29 to 46; already backed up
问题描述:删除归档的备份,在进行归档的重新备份,提示:skipping archived logs of thread 1 from sequence 29 to 46; already backed ...
- vue-cli3配置webpack generate-asset-plugin
最近尝试将vue项目中的后台URL抽离到打包后的配置文件中,看到有使用generate-asset-plugin在build时生成配置文件的做法,倒腾了一下午使该webpack plugin在vue- ...
- python基础知识第二篇(字符串)
基本数据类型 数字 整形 int ---int 将字符串 ...
- 去掉 Idea 中注入 Mapper 警告的方法
使用 Idea 的时候,自动装配 Mybatis 的 mapper.会一直出现红色波浪线的警告.看着难受.下面提供几种方式 方式一 为 @Autowired 注解设置required = false ...
- 利用Python多线程来测试并发漏洞
需求介绍 有时候想看看Web应用在代码或者数据库层有没有加锁,比如在一些支付.兑换类的场景,通过多线程并发访问的测试方式可以得到一个结论. 步骤 1. Burp Suite安装插件 安装一个Copy ...
- PHP 正则匹配h1的数据报错 preg_match(): Unknown modifier 'h' in
问题: $str = "<h1>this is test msg</h1>"; $ruler = "/^<h1>(.*?)</h ...
- 元类, pymysql
元类, pymysql 一.元类 自定义元类 ''' 1.什么是元类? - 类的类就是type,其实type就是元类 2.元类的作用? 3.如何创建元类以及使用? ''' # # 1.一切皆对象 # ...
- VS2008 激活
序列号:PYHYP-WXB3B-B2CCM-V9DX9-VDY8T 如果没有序列号输入框需要使用crackvs2008forwindows7工具进行修复
- Redis 常用知识
Redis 介绍 Redis 一个开源的使用ANSI C语言编写.遵守BSD协议,内存中的数据结构存储系统,它可以用作Key-Value数据库.缓存和消息中间件,并提供多种语API. 支持多种数据结构 ...
- 使用hexo、github Pages搭建博客
1. 安装node 如果本机已经有node,为避免安装出现问题,建议先升级到最新版.参考:https://juejin.im/post/5b9739d1e51d450e9f66ee3b 2. 安装he ...