To the Max

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 46788   Accepted: 24774

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

最大子段和的二维版本,把第i行到第j行合并成一行,做法就和一维的一样了,只要枚举i和j,找出最大值即为答案。

 //2016.8.21
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int N = ;
const int inf = 0x3f3f3f3f;
int a[N][N]; int main()
{
int n, tmp;
while(scanf("%d", &n)!=EOF)
{
for(int i = ; i < n; i++)
for(int j = ; j < n; j++)
scanf("%d", &a[i][j]); int ans = -inf;
for(int i = ; i < n-; i++)
{
//把第j行合并到第i行,求出第i行到第j行的最大子段和**************
for(int j = i; j < n; j++)
{
tmp = ;
for(int k = ; k < n; k++)
{
if(j > i) a[i][k]+=a[j][k];//把矩阵合并为一维的数组
if(tmp > ) tmp += a[i][k];
else tmp = a[i][k];
ans = max(ans, tmp);
}
}
//**************************************************************
} cout<<ans<<endl;
} return ;
}

POJ1050(dp)的更多相关文章

  1. poj1050 dp动态规划

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

  2. [POJ1050]To the Max(最大子矩阵,DP)

    题目链接:http://poj.org/problem?id=1050 发现这个题没有写过题解,现在补上吧,思路挺经典的. 思路就是枚举所有的连续的连续的行,比如1 2 3 4 12 23 34 45 ...

  3. POJ1050【DP】

    题意: 求一个最大子矩阵和. 思路: 枚举行区间,然后求一个最大子序列和. 贴一发挫code- #include <iostream> #include <cstdio> #i ...

  4. poj1050(nyoj104 zoj1074)dp问题

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39913   Accepted: 21099 Desc ...

  5. (线性dp 最大子段和 最大子矩阵和)POJ1050 To the Max

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54338   Accepted: 28752 Desc ...

  6. poj1050 To the Max(降维dp)

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49351   Accepted: 26142 Desc ...

  7. DP总结 ——QPH

    常见优化 单调队列 形式 dp[i]=min{f(k)} dp[i]=max{f(k)} 要求 f(k)是关于k的函数 k的范围和i有关 转移方法 维护一个单调递增(减)的队列,可以在两头弹出元素,一 ...

  8. POJ1050:To the max

    poj1050:http://poj.org/problem?id=1050 * maximum-subarray 问题的升级版本~ 本题同样是采用DP思想来做,同时有个小技巧处理:就是把二维数组看做 ...

  9. [POJ1050] To the Max 及最大子段和与最大矩阵和的求解方法

    最大子段和 Ο(n) 的时间求出价值最大的子段 #include<cstdio> #include<iostream> using namespace std; int n,m ...

随机推荐

  1. iOS系统弃用方法更新方法

    -boundingRectWithSize:options:attributes:context:用法 - (CGSize)sizeWithFont:(UIFont *)font constraine ...

  2. Ketama Consisent Hash

    问题描述 有一些目标节点 v1, v2...vn 需要一个算法,能够将任意key映射到目标节点中的一个vx 评价方式 1. 对于一个比较大的key集合,分布在各个目标节点的key的数量要尽可能均匀 2 ...

  3. PHP处理多表查询时的SQL语句拆分与重新组装

    在自己写框架时候会发现,多表查询组装SQL语句<?php $pre = "pre_"; $aid = "44"; $data = array(" ...

  4. ubuntu 系统 opencv3.1.0 安装

    opencv编译安装 编译环境安装: sudo apt-get install build-essential 必需包安装: sudo apt-get install cmake git libgtk ...

  5. centos7中yum安装ntfs-3g

    CentOS默认源里没有ntfs3g,想要添加ntfs支持,无非是自己下载编译安装或者加源yum安装. 新安装了一个CentOS7,用的是添加aliyun的epel源来yum安装的方式,简单易行. 1 ...

  6. 现在都是python 单独开发框架 执行脚本,处理结果,发报告之类的

    现在都是python 单独开发框架 执行脚本,处理结果,发报告之类的

  7. Apache和PHP的优化

    调优 Apache Apache 是一种高度可配置的软件.它具有大量特性,但每一种都代价高昂.从某种程度上来说,调优 Apache 来说就是以恰当的方式分配资源,还涉及到将配置简化为仅包含必要内容. ...

  8. android中广播的使用

    广播消息机制用于进行系统级别的消息通知,每个应用程序可以对感兴趣的广播进行注册,并且将接收广播的方法定义在广播接收器中(Broadcast). 广播可以分为标准广播和有序广播. 注册广播的方法可以动态 ...

  9. matlab unique 顺序不变

    对于一个向量,使用unique去重后会自动排序,为了保持原顺序: A = [5,1,8,5,2,8,3,9,6,1];[i,j] = unique(A,'first');B = A(sort(j)); ...

  10. java求两个集合的差集

    public static void main(String[] args) {Set set = new HashSet();Set set1 = new HashSet();set.add(&qu ...