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. 正睿多校联盟训练Week6

    并没有参加 Problem A.阿瓦分蛋糕输入文件: cake.in输出文件: cake.out时间限制: 1 second空间限制: 512 megabytes阿瓦为了庆祝自己自己成长为了一只可爱的 ...

  2. PHP arrray_filter(), array_map()区别与应用

    array_filter()用回调函数过滤数组中的元素.依次将数组中的元素传递给回调函数,如果回调函数返回true,则被过滤的元素作为返回数组的元素,并最终一起返回.数组的键名保持不变.array_m ...

  3. Unix\Linux | 总结笔记 | 查看文件的方式

    0 目录 vi cat head tail more less 1.vi vi编辑器的内置命令 有些内置命令使用键盘组合键即可完成,有些内置命令则需要以冒号“:”开头输入.常用内置命令如下: Ctrl ...

  4. 暑期训练狂刷系列——Lightoj 1084 - Winter bfs

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1084 题目大意: 有n个点在一条以零为起点的坐标轴上,每个点最多可以移动k, ...

  5. 2-SAT问题(白书)

    1. 定义 给定一个布尔方程,判断是否存在一组布尔变量的真值指派使整个方程为真的问题,被称为布尔方程的可满足性问题(SAT).SAT问题是NP完全的,但对于满足一定限制条件的SAT问题,还是能够有效求 ...

  6. synchronized(2)修饰方法之:普通方法

    synchronized方法 [同一个对象的该方法一次只有一个线程可以访问,该对象的其它同步方法也被阻塞] 方法声明时使用,放在范围操作符(public等)之后,返回类型声明(void等)之前.这时, ...

  7. android开发学习 ------- volley网络请求的实例

    在  http://www.sojson.com/httpRequest/  上对http进行访问,将此访问在android中的应用 ********************************* ...

  8. AJPFX总结泛型概念和使用

    泛型泛型(generic)概述和基本使用                泛型把明确数据类型的操作放到创建对象或者调用方法的时候再明确.                                J ...

  9. Kickstart Round D 2017 : A

    思路: 动态规划. large数据的时间范围很大,无法设计入状态中.转换思路为定义dp[i][j]为当前在景点i,并且已经游览了j个景点所花费的最小时间,这种思想与leetcode45类似.于是转移方 ...

  10. vuetifyjs简介及其使用

    何为 vuetify 一个vue ui库,提供vue组件供使用.根据 Google Material Design 指南实现(https://material.io/).Vuetify支持SSR(服务 ...