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

题意:给你一个n,然后给你一个n*n的矩阵,求出最大的子矩阵的和

思路:我们知道一种求最大子段和的方法(什么你不知道?),就是O(n)遍历这个一维的数组,把当前遍历的数加入一个变量(tmp),在这个过程中记录最大值,如果这个变量变成负数,
那么就把这个变量置零,继续往下遍历。
为什么呢?
如果我们加入的这个数是一个正数,那正和我们意(我们意是什么鬼),因为正数可以让变量(tmp)更大,我们需要的就是一个最大值,如果加入的数是一个负数的话,分两种情况
1、tmp >= 0
        这样的话对于后面加入的数来说,我们前面所加的数是有意义的,因为变量还是一个正数(虽然减小了),它仍可以使得后面加入的数变大(哲学的声音?)
2、tmp < 0
        这样对于后面加入的数来说,我们前面所加的数毫无意义,它使得后面的数反而更小了,所以我们就不要前面的数了(一脸嫌弃),将tmp置零。 那么给你一个二维数组,求一个最大的子矩阵,和这个有什么关系呢? 一维数组 == n*1*1的二维矩阵
这么一看我们好像已经完成了对于一个特殊二维矩阵求最大子矩阵和。 那么对于题目给出的二维矩阵,我们可以转换为我们的特殊矩阵。我们枚举i、j,表示将i~j行看成一维数组,我们将mar【i】【k】 += mar【j】【k】(对应位置相加),对mar【i】这个一维数组求最大字段和
 #include<cstdio>
#include<iostream>
using namespace std; int n;
int mar[][];
int maxx = -;
int main()
{
scanf("%d",&n);
int lim = ;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&mar[i][j]),lim+=mar[i][j];
for(int i=;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
int tmp = ;
for(int k=;k<=n;k++)
{
if(i != j)mar[i][k] += mar[j][k];
if(tmp > )tmp+=mar[i][k];
else tmp = mar[i][k];
if(tmp > maxx && tmp != lim)maxx = tmp;
}
}
}
printf("%d\n",maxx);
}

To the Max POJ - 1050 (最大子段和)的更多相关文章

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

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

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

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

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

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

  4. 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 ...

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

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

  6. poj 1050 To the Max (简单dp)

    题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...

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

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

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

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

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

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

随机推荐

  1. centos 7.3 设置静态IP

    注:本文来源:张亮博客  的 <centos 7.3 设置静态IP或ping 报name or service not known> 首先把虚拟机配置为桥接模式,然后开启再你打算修改虚拟机 ...

  2. Confluence 6 PostgreSQL 设置准备

    请查看 Supported Platforms 页面来获得 Confluence 系统支持的 PostgreSQL 数据库版本.你需要在安装 Confluence 之前升级你的 PostgreSQL ...

  3. Remove Duplicates from Sorted ListII

    给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例 1: 输入: 1->2->3->3->4->4->5 输出: 1-&g ...

  4. 电子书转换为PDF格式

    目录 一.mobi 转换 pdf 步骤 二.查看转换后的结果目录 三.将PDF还原文件名且移出至新目录 背景:当我们从网上下载一些电子小说或书籍的时候,一般文件的格式可能是.epub..mobi等.这 ...

  5. 用mybatis做数据库处理 代码中的字段大小写 要和mapper映射设置的大小写一致(这TM不废话么,原谅我渣!~~)

    简单描述情况:其实这个问题怎么说呢,有人给你说,你肯定能意识到,必须大小写对应的嘛.emmmm我现在才意识到是因为:自己在下边敲代码,配的mapper文件并没有把属性变量,和数据库里的段单独提出来做映 ...

  6. noip 2018.10.14 模拟赛 砍树

    数学问题... 根据题意,有: 移项,整理,得: 记 于是 那么 可以看到,最多只会有2*个取值(显而易见) 于是对应的,可能产生效果的d也只会有个,于是我们把他们找出来,扔进一个数组里然后排序,去重 ...

  7. ajax对象方法的使用

    change.js文件的内容对象函数关键字:fnjQuery.fn.change = function () { this.css({"background": "red ...

  8. 停止Monkey

    adb shell top | grep monkey adb shell kill id

  9. CPU虚拟化

    1. 为什么需要 CPU 虚拟化 X86 操作系统是设计在直接运行在裸硬件设备上的,因此它们自动认为它们完全占有计算机硬件.x86 架构提供四个特权级别给操作系统和应用程序来访问硬件.  Ring 是 ...

  10. ActiveSync中的http内容组织

    1. POST Request 数据格式 Request-line Request-headers CR/LF Request Body Request-line POST <URI> H ...