To The Max

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10920    Accepted Submission(s): 5229

Problem Description
Given
a two-dimensional array of positive and negative integers, a
sub-rectangle is any contiguous sub-array of size 1 x 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 x 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
 
实质上是最大子段和问题,最大子矩阵我们将其所有的行都枚举出来,然后将其合并成一行,然后就可以用最大连续子段和求最大值,得到最大子矩阵。
 
最大子段和:
    令b[j]表示以位置 j 为终点的所有子区间中和最大的一个
    子问题:如j为终点的最大子区间包含了位置j-1,则以j-1为终点的最大子区间必然包括在其中
    如果b[j-1] >0, 那么显然b[j] = b[j-1] + a[j],用之前最大的一个加上a[j]即可,因为a[j]必须包含
    如果b[j-1]<=0,那么b[j] = a[j] ,因为既然最大,前面的负数必然不能使你更大
   状态转移方程 dp[j] = max(dp[j-1]+a[j],a[j])(0<j<=n)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include <string.h>
#include <math.h>
using namespace std;
const int N = ;
int mp[N][N],b[N];
int n; int getMax()
{
int t = ,mx = -;
int dp[N+]= {};
for(int i=; i<=n; i++)///从1开始枚举
{
if(dp[i-]>) dp[i] = dp[i-]+b[i-];
else dp[i]=b[i-];
mx = max(mx,dp[i]);
}
return mx;
}
int solve()
{
int mx = -;
for(int i=; i<n; i++)
{
for(int j=i; j<n; j++)
{
memset(b,,sizeof(b));
for(int k=; k<n; k++)
for(int l=i; l<=j; l++)
b[k]+=mp[l][k];
mx = max(mx,getMax());
}
}
return mx;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
scanf("%d",&mp[i][j]);
}
int mx = solve();
printf("%d\n",mx);
}
return ;
}
 

hdu 1081(最大子矩阵)的更多相关文章

  1. hdu 1081(最大子矩阵和)

    题目很简单,就是个最大子矩阵和的裸题,看来算法课本的分析后也差不多会做了.利用最大子段和的O(n)算法,对矩阵的行(或列)进行 i和j的枚举,对于第 i到j行,把同一列的元素进行压缩,得到一整行的一维 ...

  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 1081 &amp; poj 1050 To The Max(最大和的子矩阵)

    转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and ne ...

  5. HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)

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

  6. hdu 1081 dp问题:最大子矩阵和

    题目链接 题意:给你一个n*n矩阵,求这个矩阵的最大子矩阵和 #include<iostream> #include<cstdio> #include<string.h& ...

  7. hdu 1559 最大子矩阵

    最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  8. 【动态规划】HDU 1081 & XMU 1031 To the Max

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1081 http://acm.xmu.edu.cn/JudgeOnline/problem.php?i ...

  9. (DP)To The Max --HDU -- 1081

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1081 这道题使用到的算法是:预处理+最大连续子串和 如果会做最大连续子串和,那么理解这题就相对简单一些, ...

随机推荐

  1. 51nod 1962 区间计数(单调栈+二分)

    维护两个单调递减的栈,当i加进栈,位置x的数弹出的时候,在另一个栈中找到和这个数一样大的数,计算贡献(x-靠右左端点)*(i-x). #include<iostream> #include ...

  2. 非代码抽取的dex加固脱壳

    常见的非代码抽取的dex加固,可以通过修改或者hook源码中的dex解析函数拿到目标dex完成脱壳.该dex解析函数为DexFile* dexFileParse(const u1* data, siz ...

  3. 【神仙DP】【UVa11400】Lighting System Design

    传送门 Description Translation 题目大意:有一个照明系统需要用到n种灯,每种灯的电压为V,电源费用K,每个灯泡费用为C,需要该灯的数量为L.注意到,电压相同的灯泡只需要共享一个 ...

  4. HDU 3605 最大流+状态压缩

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  5. Struts2入门(1)-第一个Struts2程序

    目录结构 C:\WorkSpace\java\StrutsTest\src\main C:\WorkSpace\java\StrutsTest\src\test C:\WorkSpace\java\S ...

  6. 【Android】完善Android学习(六:API 4.0)

    备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...

  7. UVA 1393 Highways

    https://vjudge.net/problem/UVA-1393 题意: a*b的点阵中能画多少条非水平非竖直的直线 方向‘/’ 和 方向 ‘\’ 对称 枚举直线所在矩形的i*j 直线可能重复的 ...

  8. Cppcheck代码分析下

    1.流解析 解析函数中的可能的代码执行流, 函数实际执行中只会执行代码流中的一条流 分析: 分支语句 if-else ,switch-case         循环语句 while, do-while ...

  9. JDK工具学习

    javap: 可以对照源代码和字节码,从而了解很多编译器内部的工作. 查看class字节码:JDK有自带的工具包,使用javap命令打开.class文件就行 javap -c JAVAPTest

  10. 【BZOJ4870】组合数问题 [矩阵乘法][DP]

    组合数问题 Time Limit: 10 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description Input 第一行有四个整数 n ...