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

解题报告:这道题真的是感人,状态转移方程干到我怀疑人生,最后终于搞明白了,下面附上理解图,希望能便于大家理解此题的DP方程

#include <bits/stdc++.h>
using namespace std; int map[110][110],dp[110][110]; int main()
{
//freopen("input.txt","r",stdin);
int N,a;
while(~scanf("%d",&N) && N)
{
memset(map,0,sizeof(map));
memset(dp,0,sizeof(dp)); for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++)
{
scanf("%d",&a);
map[i][j] = map[i][j-1] + a;
//map[i][j]表示第i行前j列的和
} int Max = -0xffffff0; for(int j = 1; j <= N; j++)
for(int i = 1; i <= j; i++)
{
dp[i][j] = 0; for(int k = 1; k <= N; k++)
{
dp[i][j]= max(dp[i][j]+map[k][j]-map[k][i-1],map[k][j]-map[k][i-1]);
if(dp[i][j] > Max) Max = dp[i][j];
}
} printf("%d\n",Max);
}
return 0;
}

(POJ - 1050)To the Max 最大连续子矩阵和的更多相关文章

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

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

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

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

  3. [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)

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

  4. poj 1050 To the Max(最大子矩阵之和,基础DP题)

    To the Max Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...

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

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

  6. hdu 1081 &amp; poj 1050 To The Max(最大和的子矩阵)

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

  7. poj 1050 To the Max 最大子矩阵和 经典dp

    To the Max   Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

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

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

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

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

随机推荐

  1. 显著水平alpha

    http://blog.minitab.com/blog/adventures-in-statistics-2/understanding-hypothesis-tests:-significance ...

  2. VS code docker 调试 asp.net core

    前言 .net core的诞生就是为了解决跨平台的事情的,所以.net core app运行在linux.macOS.docker上也不是什么新鲜事了. 相信已经有不少.net core的项目已经部署 ...

  3. css总结18:HTML 表单和inut各个常用标签

    1 HTML 表单和输入 1.1 HTML 表单介绍 表单是一个包含表单元素的区域. 表单元素是允许用户在表单中输入内容,比如:文本域(textarea).下拉列表.单选框(radio-buttons ...

  4. 编写高质量代码改善C#程序的157个建议——建议60:重新引发异常时使用Inner Exception

    建议60:重新引发异常时使用Inner Exception 当捕获了某个异常,将其包装或重新引发异常的时候,如果其中包含了Inner Exception,则有助于程序员分析内部信息,方便代码调试. 以 ...

  5. 设计模式03: Builder 生成器模式(创建型模式)

    Builder生成器模式(创建型模式) Builder模式缘起假设创建游戏中的一个房屋House设施,该房屋的构建由几个部分组成,且各个部分富于变化.如果使用最直观的设计方法,每个房屋部分的变化,都将 ...

  6. 20169219 TCP_IP网络协议攻击实验报告

    (1) ARP缓存欺骗 RP 缓存是 ARP 协议的重要组成部分.ARP 协议运行的目标就是建立 MAC 地址和 IP 地址的映射,然后把这一映射关系保存在 ARP 缓存中,使得不必重复运行 ARP ...

  7. 测试中常用到的linux命令

    1. man         格式化以及列出命令的(在线)操作手册.         使用方法                 man [ -options ] name       man man ...

  8. Alpha项目复审

    队名 优点 缺点 排名 拉登是我罩的 1.最底层.从无到有实现的软硬件结合的俄罗斯方块游戏. 2.从画电路原理图.PCB电路板设计.接线.操作系统(没用到操作系统).驱动程序.应用程序开发,串口通信. ...

  9. 从头开始学eShopOnContainers——Visual Studio 2017环境配置

    一.安装和配置Docker环境 1.安装Docker CE for Windows 从官方网站下载并安装,https://docs.docker.com/docker-for-windows/inst ...

  10. LoadRunner---杂问题&接口测试

    问题1] 响应时间是系统完成事务执行准备后所采集的时间戳和系统完成待执行事务后所采集的时间戳之间的时间间隔,是衡量特定类型应用事务性能的重要指标,标志了用户执行一项操作大致需要多长时间.[问题2] 系 ...