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

 #include<cstdio>
#include<string.h>
using namespace std;
int map[][];
int main()
{
int n;
int flag;
int sum;
while(scanf("%d",&n)!=EOF)
{
memset(map,,sizeof(map));
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
scanf("%d",&flag);
map[i][j]+=map[i][j-]+flag;//先计算每一行的前J列的和
for(int i=; i<=n; i++)
for(int j=; j<=i; j++)//确定子矩阵的左右边界
{
int sum=;
for(int k=; k<=n; k++)//行数
{
if(sum<)
sum=;//如果前几行的和是小于0的,就令sum=0;因为负数越加越小。
sum+=(map[k][i]-map[k][j-]);//一行一行地加起来,第k行的[i,j]列
if(sum>max)
max=sum;//找出最大值!!!
} }
printf("%d\n",max);
}
return ;
}

F - To the Max的更多相关文章

  1. 2016huasacm暑假集训训练五 F - Monkey Banana Problem

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/F 题意:求至上而下一条路径的所经过的值得和最大值,这题比赛时就出了 但当时看不懂题 ...

  2. Codeforces 727 F. Polycarp's problems

    Description 有一个长度为 \(n\) 有正负权值的序列,你一开始有一个值,每次到一个权值就加上,最少需要删掉多少数值才能到序列末尾.\(n \leqslant 750,m \leqslan ...

  3. Codeforces Gym 100286F Problem F. Fibonacci System 数位DP

    Problem F. Fibonacci SystemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...

  4. 【最大流】ECNA 2015 F Transportation Delegation (Codeforces GYM 100825)

    题目链接: http://codeforces.com/gym/100825 题目大意: N(N<=600)个点,每个点有个名字Si,R(R<=200)个生产商在R个点上,F(F<= ...

  5. 2019ccpc-HN省赛-A/C/F/I

    A.最大下降矩阵 时间限制: 1 Sec  内存限制: 512 MB提交: 508  解决: 107[提交] [状态] [讨论版] [命题人:外部导入] 题目描述 我们称一个矩阵是下降矩阵,当且仅当, ...

  6. HDU 1024 Max Sum Plus Plus (动态规划)

    HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...

  7. 树形DP ---- Codeforces Global Round 2 F. Niyaz and Small Degrees引发的一场血案

    Aspirations:没有结果,没有成绩,acm是否有意义?它最大的意义就是让我培养快速理解和应用一个个未知知识点的能力. ————————————————————————————————————— ...

  8. 洛谷大宁的邀请赛~元旦祭F: U17264 photo(线段树)

    标程的写法稍微有点麻烦,其实不需要平衡树也是可以做的. 线段树上维护从左端点开始最远的有拍照的长度,以及区间的最大值. 考虑两段区间合并的时候,显然左区间必须取,右区间的第一个比左区间最大值大的数开始 ...

  9. hdu1024 Max Sum Plus Plus[降维优化好题(貌似以后可以不用单调队列了)]

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. SQL2008游标

    最近让写一个自动生成数据的存储过程,其中会遍历表中数据并做出相应处理,因为数据量不算太大所以使用到了游标,初识游标遇到几个小问题,所以来和大家一起分享一下: 使用游标的五个步骤: 1.声明游标 语法: ...

  2. Cheatsheet: 2015 10.01 ~ 10.31

    .NET Publishing your ASP.NET App to Linux in 5 minutes with Docker Integrating AngularJS with ASP.NE ...

  3. CSS文本与连接

    CSS文本 CSS文本属性可以定义文本外观,通过文本属性,可以改变文本的颜色.字符间距.对齐文本.装饰文本.对文本缩进等等. 常用的文本属性 属性 描述 color 文本颜色 direction 文本 ...

  4. Autofac 解释第一个例子 《第一篇》

    Autofac是一个轻量级的依赖注入的框架,同类型的框架还有Spring.NET,Unity,Castle等. Autofac的使用有一个非常让人郁闷的地方,就是服务器要求安装有Microsoft . ...

  5. linux 之SCP

    一.从本地到远程复制 1.复制文件 * 命令格式: 1.scp -P remote_port local_file remote_username@remote_ip:remote_folder 或者 ...

  6. mac下使用brew安装ffmpeg支持x265

    使用brew install ffmpeg 安装ffmpeg默认是没有支持x265的, 使用brew info ffmpeg 获取安装选项帮助, 使用brew reinstall ffmpeg --w ...

  7. no leveldbjni64-1.8 in java.library.path

    在抽取以太坊Java版本的Trie树部分时,遇到了一个问题: Exception in thread "main" java.lang.UnsatisfiedLinkError: ...

  8. toolkit学习笔记

  9. 20169212《Linux内核原理与分析》第一周作业

    实验 使用touch创建文件: man手册的内容很多,涉及了Linux使用过程中的方方面面,为了便于查找,是做了分册(分区段)处理的,在Research UNIX.BSD.OS X和Linux中,手册 ...

  10. Nexus私服忘记用户名密码,Nexus私服如何找回用户名密码

    Maven搭建私服用Nexus很方便.但是如果忘记了管理员的密码,如何处理呢?Nexus私服如何找回用户名密码? 1. 找到nexus-XXXYYYY-bundle/sonatype-work/nex ...