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. DataGridView单元格美化

          #region 重绘Column.Row           int _RowHeadWidth = 41;         ///            /// 重绘Column.Row ...

  2. 遗传算法在JobShop中的应用研究(part 2:编码)

    编码 在上一篇博客中我们讨论了车间调度问题的编码,具体说就是根据工件的个数和每个工件的工序数来生成12122这样的数字排列,具体的说一个工件包含多少道工序,那么这个工件的编号就出现多少次.从12122 ...

  3. 用Git导出项目

    Git没有SVN的导出功能,不能像 svn export url 那样,将某个版本的代码导出为不带版本控制文件的文件夹.   Git提供了archive命令,可以把版本的文件流导出.   可以将Git ...

  4. ssh框架介绍

    SSH 为 struts+spring+hibernate 的一个集成框架,是目前较流行的一种JAVA Web应用程序开源框架. Struts Struts是一个基于Sun J2EE平台的MVC框架, ...

  5. 响应式 css

    1.class 样式一般用class,命名:中横线分隔,如:div-logo id 一般用于js快速地区别和获取元素,命名:驼峰命名法,如:divLogo (中间首字母大写) 2.必不可少的图片,用& ...

  6. Linux运行级详解

    对于那些在DOS/Win9x/NT平台下的高级用户而言,Linux似乎是一个怪物.没有config.sys,没有 autoexec.bat,具有个人特色的机器配置不知道从何开始. 需要说明的是,很多人 ...

  7. CentOS PPTP配置FreeRADIUS+DaloRADIUS实现高级用户控制+流量控制

    前提条件 阅读本文前,您需要搭建好PPTP,如果仍未搭建,可以参考:http://www.xj123.info/2301.html 如果您需要配置DaloRADIUS,那么您还需要安装LAMP,可以参 ...

  8. 【Jenkins】Windows下安装&访问jenkins

    1. 下载jenkins.war包 下载地址:http://jenkins-ci.org/ 2. 之后在cmd里启动jenkins,命令如下: java -jar (放置war包路径,最好没有中文)j ...

  9. html5的spellcheck属性(拼写、文法检查)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. Windows Store App 用户库文件分组

    在Windows应用商店应用程序中浏览用户库中的文件时,可以将文件或者文件夹分组显示,以便于进行分类浏览,这类似于音乐库中的文件可以按照艺术家名称.创建日期或者评级等多种方式进行分类.本节内容将会介绍 ...