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. python 的 from import 机制

    [A.py] from B import D class C:pass [B.py] from A import C class D:pass 为什么执行A的时候不能加载D呢? 如果将A.py改为:i ...

  2. 编写一个JSP程序实现用户登录

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. Linux 休眠,挂起(待机),关机等几个命令的区别及如何实现;如何启用Ubuntu的休眠模式

    这里对linux 的几个命令整理下,有:休眠,挂起,待机,关机等几个命令的区别及如何实现. 休眠是一种更加省电的模式,它将内存中的数据保存于硬盘中,所有设备都停止工作.当再次使用时需按开关机键,机器将 ...

  4. C# 调用 Outlook发送邮件实例

    添加引用:Microsoft.Office.Interop.Outlook using System; using System.Collections.Generic; using System.L ...

  5. 16s及宏基因组测序公司资源--20161104

    锐羿基因: http://www.realbio.cn/index.php?c=list&cs=keyandongtai& 科研动态更新及时 青岛过程能源所: http://www.c ...

  6. Python之SQLAlchemy学习

    alchemy 英[ˈælkəmi] 美[ˈælkəmi] n. 炼金术; 炼丹术; (改变事物.物质的)魔力(或方法) ; (事物.物质的) 神秘变化; SQLAlchemy是Python中最有名的 ...

  7. Android pulltorefresh使用

    pulltorefresh插件可以轻松实现上拉下拉刷新,github.com上直接搜索进行下载. 布局文件: <RelativeLayout xmlns:android="http:/ ...

  8. golang csv问题

    go语言自带的有csv文件读取模块,看起来好像不错,今天玩玩,也算是系统学习go语言的一部分--^_^ 一.写csv文件 函数: func NewWriter(w io.Writer) *Writer ...

  9. php_match/preg_match_all 默认有字符串长度限制

    php_match/preg_match_all 默认有字符串长度限制:52500(或许你的服务器环境是更长,或者更短),当字符串长度大于52500,只能匹配到52500数据,超出的部分会被系统自己截 ...

  10. C++类的交叉引用

    对于C++中,两个类中相互引用对方,当然只能是在指针的基础上,于是我们知道.也就是说在A类的有一个指针引用B类的成员函数或成员对象,而B类中又有一个指针来访问A中的成员函数或对象.这就是C++中类的交 ...