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

这道题其实就是求最大子段和,需要把原题数据变化一下,例如
0 -2 -7 0 
9 2 -6 2 
-4 1 -4 1 
-1 8 0 -2 
这个矩阵我选择的是
9 2
-4 1
-1 8
那么我还可以把这个选择过程看待为求数组 4 11 -10 1 的最大子段和,很显然是选择4 11,答案为15
那么4 11 -10 1是怎么来的呢,是我把2 3 4行数组组合成一个数组得来的
那么这道题的解法就出来了,不断枚举行区间,得到一个新数组,然后求最大子段和
#include"iostream"
#include"cstring"
using namespace std;
const int maxn=;
int b[maxn],a[maxn][maxn];
int n; void Init()
{
int t;
memset(a,,sizeof(a));
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>t;
a[i][j]=a[i-][j]+t;
}
}
} int main()
{
while(cin>>n)
{
Init();
int sum,ans,temp;
sum=;
ans=-;
int c=;
for(int i=;i<=n;i++)
for(int j=i;j<=n;j++)
{
sum=;
for(int k=;k<=n;k++)
{
temp=a[j][k]-a[i-][k];
sum+=temp;
if(sum>ans)
{
ans=sum;
}
if(sum<)
{
sum=;
}
}
}
cout<<ans<<endl;
}
return ;
}

集训第五周动态规划 F题 最大子矩阵和的更多相关文章

  1. 集训第五周 动态规划 B题LIS

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Des ...

  2. 集训第五周动态规划 H题 回文串统计

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

  3. 集训第五周动态规划 D题 LCS

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  4. 集训第五周动态规划 I题 记忆化搜索

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  5. 集训第五周动态规划 G题 回文串

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  6. 集训第五周动态规划 C题 编辑距离

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  7. 集训第五周 动态规划 K题 背包

    K - 背包 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  8. 集训第五周动态规划 J题 括号匹配

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  9. 集训第五周动态规划 E题 LIS

    Description The world financial crisis is quite a subject. Some people are more relaxed while others ...

随机推荐

  1. 利用动态扫描和定时器1在数码管上显示出从765432开始以1/10秒的速度往下递减 直至765398并保持此数,与此同时利用定时器0以500MS速度进行流水灯从上至下移动 ,当数码管上数减到停止时,实验板上流水灯出停止然后全部开始闪烁,3秒后(用 T0定时)流水灯全部关闭,数码管上显示出“HELLO”,到此保持住

    #include <reg52.h> #include <intrins.h> #define uchar unsigned char #define uint unsigne ...

  2. c++中快速排序

    (一)为什么要用c++标准库里的排序函数 Sort()函数是c++一种排序方法之一,学会了这种方法也打消我学习c++以来使用的冒泡排序和选择排序所带来的执行效率不高的问题!因为它使用的排序方法是类似于 ...

  3. 关于Swing中JFrame等顶级容器的层次还有设置背景的方式

    资料来自:http://blog.csdn.net/qq_32006373/article/details/49659129 http://yuncode.net/code/c_5196327caac ...

  4. 转】MongoDB主从复制实验 master/slave

    原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/4/ 感谢! Posted: May 31, 2013 Ta ...

  5. JDK集合框架--综述

       接下来的几篇博客总结一下对jdk中常用集合类知识,本篇博客先整体性地介绍一下集合及其主要的api: 从整体上来说,集合分两大类collection和map: 首先来看看Collection: c ...

  6. hihocoder offer收割编程练习赛11 A hiho字符串

    思路: 我用的尺取. 注意题目描述为恰好2个'h',1个'i',1个'o'. 实现: #include <iostream> #include <cstdio> #includ ...

  7. Android 重定向 init.rc中服务的输出

    在init.rc中运行的服务,由于系统启动的时候将标准输出重定向到了/dev/null, 所以服务中的打印信息都不可见. 但调试时可能需要看到其中的打印信息,因此就有了logwrapper这个工具:l ...

  8. MTK处理器手机 解锁Bootloader 教程

    目前很多手机都需要解锁Bootloader之后才能进行刷机操作   本篇教程教你如何傻瓜式解锁Bootloader 首先需要在设置-关于手机 找到版本号(个别手机可能是内核版本号,甚至其他) 然后 快 ...

  9. JavaScript操作DOM与jQuyer操作DOM的对比

    1.通过jQuery方法包装后的对象,是一个类数组对象.它与DOM对象完全不同,唯一相似的是它们都能操作DOM. 2.通过jQuery方法包装后的对象,是一个类数组对象.它与DOM对象完全不同,唯一相 ...

  10. 按键精灵txt判断

      句子 = "度阿斯达娘阿婶是大的百度知道" 词 = "百度知道" MyPos = Instr(句子, 词) If MyPos > 0 Then Tra ...