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. 弃 Java 而使用 Kotlin 的你后悔了吗?| kotlin将会是最好的开发语言

    自从 2011 年发布以来,Kotlin 凭借强大的功能在开发者中的欢迎程度与日俱增.且在一年前,Google 宣布 Kotlin 正式成为 Android 官方开发语言,由此引发了从 Java 迁移 ...

  2. easyui的combobox,自动搜索的下拉框

    作者:多来哈米 如图,输入关键字,左匹配检索 HTML代码 <input class="easyui-combobox" name="userId" id ...

  3. pod 使用详解

    cd 进去到 项目目录 包含 xcodeproj 结尾的目录下 1 pod init 创建一个pod 文件 2 打开生产的pod 文件 然后 配置pod 文件 并保存  3 pod install 安 ...

  4. 深入分析Zookeeper的实现原理

    zookeeper 的由来 分布式系统的很多难题,都是由于缺少协调机制造成的.在分布式协调这块做得比较好的,有 Google 的 Chubby 以及 Apache 的 Zookeeper.Google ...

  5. 基于BootStrap的Collapse折叠(包含回显展开折叠的对应状态)

    情况描述:为了改善页面上的input框太多,采用∧∨折叠展开,这个小东西来控制,第一次做,记录一下ヾ(◍°∇°◍)ノ゙下边是Code 代码: //html代码 <div id="col ...

  6. LabView(控件部分)

    1.虚拟仪器的概述: 虚拟仪器是基于计算机的的仪器,计算机和仪器的密切结合是目前仪器的一个发展方向,大概有两种结合方式,一种是将计算机装入仪器中,实例就是只能化的仪器,流行的嵌入式系统的仪器,另一种就 ...

  7. python 内置数据类型之字符串

    1.3 字符串 字符串本身就是一个有序(从左至右)的字符的集合.是序列这种类型的一种,后面还要学习列表与元组. 在这一节中,需要了解字符串的定义,特殊字符,转义与抑制转义:字符串基本操作.格式化等. ...

  8. MyEclipes相关配置

    0. MyEclipes10 相关下载资源(私人珍藏版) 链接:http://pan.baidu.com/s/1eSIdObS密码:0cjy 1. myEclipes连接Tomcat http://w ...

  9. markdown 相关零碎知识

    1.尖括号<>在markdown会被当做html符号,解决办法:用转义字符,如:<测试>  可以写作&lt:测试>

  10. 如果IDEA右上角的tomcat消失了,解决办法

    看了很多博客都没有找到解决办法,还是老师帮我解决的