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

Source

 

题目大意:给一个n*n的整数矩阵,找出一个子矩阵使其和最大。

解题思路:• 该题其实就是最大连续和问题在二维空间上的推广。

• 先来看一下一维的最大连续和问题:

             ♣ 给出一个长度为n的序列A1,A2,A3.....An,求一个连续子序列Ai,Ai+1,....Aj使得元素总和最大。

           ♥ 我们以temp[i]表示以Ai结尾的子段中的最大子段和。在已知temp[i]的情况下,求temp [i+1]的方法是:

如果temp[i]>0 temp [i+1]= temp[i]+ai(继续在前一个子段上加上ai),否则temp[i+1]=ai(不加上前面的子段);

也就是说 状态转移方程:temp[i] = (temp[i-1]>0?temp[i-1]:0)+buf[i];

 int getMax(int buf[],int n){
int temp[],max=n*(-);
memset(temp,,sizeof(temp));
for(int i=;i<=n;i++){
temp[i] = (temp[i-]>?temp[i-]:)+buf[i];
if(max<temp[i])
max=temp[i];
}
return max;
}//求n元素序列buf[]的最大连续和函数

• 对于本题可以暴力枚举i到j行,针对每一个i到j行的一列元素求和就将i到j行的2维情况转化为1维情况:如:

0    -2  -7  0

9    2  -6  2

-4  1  -4   7

-1  8  0   -2

       取i=2,j=4,压缩为4(9 -4 -1),11(2 1 8),-10(-6 -4 0),7(2 7 -2)新的一维buf[]={4,11,-10,7},

然后求出buf[]的最大连续和就是2到4行范围内的最大矩阵的值。这样2层循环暴力所有i到j的情况取最大值即可!

 #include<iostream>
using namespace std;
int rect[][];//2维矩阵
int n,Max;;
int buf[];//中间1维矩阵
int getMax(){
int Temp[],max=n*(-);
memset(Temp,,sizeof(Temp));
for(int i=;i<=n;i++){
Temp[i]=(Temp[i-]> ? Temp[i-] : )+buf[i];
if(max<Temp[i])
max=Temp[i];
}
return max;
}//求最大连续和
void read(){
for(int i=;i<n;i++)
for(int j=;j<n;j++)
cin>>rect[i][j];
}//读入
int solve(){
Max=-*n;
for(int i=;i<n;i++){
for(int j=i;j<n;j++){//2层循环暴力所有i到j组合
memset(buf,,sizeof(buf));//压缩,2维变1维
for(int k=;k<n;k++)
for(int L=i;L<=j;L++)
buf[k]+=rect[k][L];
int d=getMax();//获得最大连续和
if(d>Max)Max=d;//更新Max值
}
}
return Max;
}//2维变1维暴力
int main(){
while(cin>>n){
read();
solve();
cout<<Max<<'\n';
}return ;
}

[ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)的更多相关文章

  1. POJ 1050 To the Max -- 动态规划

    题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...

  2. POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)

    传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  3. poj - 1050 - To the Max(dp)

    题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...

  4. poj 1050 To the Max(线性dp)

    题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...

  5. poj 1050 To the Max(最大子矩阵之和)

    http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here  也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...

  6. POJ 1050 To the Max (最大子矩阵和)

    题目链接 题意:给定N*N的矩阵,求该矩阵中和最大的子矩阵的和. 题解:把二维转化成一维,算下就好了. #include <cstdio> #include <cstring> ...

  7. POJ 1050 To the Max 二维最大子段和

    To the MaxTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 52281 Accepted: 27633Description ...

  8. [poj]1050 To the Max dp

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

  9. POJ 1050 To the Max 暴力,基础知识 难度:0

    http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...

随机推荐

  1. C#十种语法糖

    语法糖 指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用.通常来说使用语法糖能够增加程序的可读性,从而减少程序代码出错的机会.需要声明的是"语法糖" ...

  2. iOS 设置button文字过长而显示省略号的解决办法

    UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];button.titleLabel.adjustsFontSizeToF ...

  3. nvidia 各种卡

    cudnn是针对maxwell优化的啊, maxwell下的各种卡都是游戏卡,具体可以见: https://developer.nvidia.com/maxwell-compute-architect ...

  4. ArcGIS删除部分数据后全图范围不正确

      我有一个全国地图的图层,现在删除图层中其他省份,只保留山东省的图形,但是点击全图后,全图范围仍然是全国地图时候的全图范围,使用的版本是ArcGIS9.3,数据存放在9.3的个人数据库中(Perso ...

  5. locutus(phpjs) 的使用

    今天来介绍一个js的框架,这个框架的主要功能呢,是通过加载该类库,来实现php函数的调用 当然了,这并不是说php中所有的函数都能在js中使用,但很大一部分是可以的. 环境:mac + node v5 ...

  6. SQL Server 查看物理页存储

    创建测试表 Use Test create table dbo.employee( emp_lname varchar(12) not null, emp_fname varchar(12)not n ...

  7. Oracle 的基本使用--基本命令<一>

    sql*plus 的常用命令 连接命令 1.conn[ect] 用法:conn 用户名/密码@网络服务名[as sysdba/sysoper]当用特权用户身份连接时,必须带上 as sysdba 或是 ...

  8. IAP 破解漏洞验证

    IAP支付有个漏洞,用户使用的可能是IAP Free 或者俄罗斯破解什么的,所产生的交易号:170000029449420 product_id:com.zeptolab.ctrbonus.super ...

  9. NX图标

    可以从%UGII_BASE_DIR%\ugii\menus目录下men文件或TBR文件中查找 关键字BITMAP 对应的名称

  10. ycsb-命令及参数-与生成的负载类型相关

    loadbin/ycsb load mydb -P workloads/workloada -p "mydb.host=127.0.0.1" -p "mydb.port= ...