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<iostream>
#include<cstring>
#include<cstdio> using namespace std;
const int N = + ;
int mat[N][N]; int maxsum(int n){
int matsum[N][N], ret, sum;
int i, j, k;
for(i = ; i < n; i++)
for(matsum[i][j = ] = ; j < n; j++)
matsum[i][j + ] = mat[i][j] + matsum[i][j];
for(ret = mat[][j = ]; j < n; j++)
for(k = j; k < n; k++)
for(sum = , i = ; i < n; i++)
sum = (sum > ? sum:) + matsum[i][k + ] - matsum[i][j], ret = (sum > ret?sum:ret);
return ret;
} int main(){
int n;
while(scanf("%d", &n) == ){
for(int i = ; i < n; i++)
for(int j = ; j < n; j++) scanf("%d", &mat[i][j]);
printf("%d\n", maxsum(n));
}
}

最大连续和 Medium的更多相关文章

  1. 3 Longest Substring Without Repeating Characters(最长不重复连续子串Medium)

    题目意思:求字符串中,最长不重复连续子串 思路:使用hashmap,发现unordered_map会比map快,设置一个起始位置,计算长度时,去减起始位置的值 eg:a,b,c,d,e,c,b,a,e ...

  2. LeetCode:152_Maximum Product Subarray | 最大乘积连续子数组 | Medium

    题目:Maximum Product Subarray Find the contiguous subarray within an array (containing at least one nu ...

  3. 配置ASP.NET Web应用程序, 使之运行在medium trust

    这文章会向你展示, 怎么配置ASP.NET Web应用程序, 使之运行在medium trust.   如果你的服务器有多个应用程序, 你可以使用code access security和medium ...

  4. STM32F207 两路ADC连续转换及GPIO模拟I2C给MT9V024初始化参数

    1.为了更好的方便调试,串口必须要有的,主要打印一些信息,当前时钟.转换后的电压值和I2C读出的数据. 2.通过GPIO 模拟I2C对镁光的MT9V024进行参数初始化.之前用我以前公司SP0A19芯 ...

  5. Oracle分析函数及常用函数: over(),rank()over()作用及用法--分区(分组)求和& 不连续/连续排名

    (1)   函数:  over()的作用及用法:    -- 分区(分组)求和. sum() over( partition by column1 order by column2 )主要用来对某个字 ...

  6. Leetcode 1004. 最大连续1的个数 III

    1004. 最大连续1的个数 III  显示英文描述 我的提交返回竞赛   用户通过次数97 用户尝试次数143 通过次数102 提交次数299 题目难度Medium 给定一个由若干 0 和 1 组成 ...

  7. 【LEETCODE】65、字符分类,medium&easy级别,题目:20、647、3

    今天的字符类还比较简单 package y2019.Algorithm.str.easy; import java.util.HashMap; import java.util.Map; import ...

  8. 介质访问控制子层-Medium Access Control Sublayer:多路访问协议、以太网、无线局域网

    第四章 介质访问控制子层-Medium Access Control Sub-layer 4.1介质访问控制子层概述 MAC子层不属于之前提到的OSI或TCP/IP架构的任何一层,这也是为什么这一层被 ...

  9. Redis简单案例(三) 连续登陆活动的简单实现

    连续登陆活动,或许大家都不会陌生,简单理解就是用户连续登陆了多少天之后,系统就会送一些礼品给相应的用户.最常见的 莫过于游戏和商城这些.游戏就送游戏币之类的东西,商城就送一些礼券.正值国庆,应该也有不 ...

随机推荐

  1. idea万能快捷键,你不知道的17个实用技巧!!!

    IDEA里有一个万能快捷键(alt enter),功能非常强大,同一个快捷键,可以根据不同的语境提示你不同的操作,很多人可能还不了解这些功能,在处理代码的时候还手动处理,了解这些技巧之后,你编码也是一 ...

  2. js判断条件为“假”的情况

    以下6种情况判断结果为"假": 1.false(布尔类型) 2.null(用于定义空的或者不存在的引用) 3.undefined(未定义) 4.0(数值0) 5.''(空字符串) ...

  3. SQL 以逗号分隔查询;调用自定义函数

    select col from [dbo].[GetInPara]('101,102,103',',') USE [xxx] GO /****** Object: UserDefinedFunctio ...

  4. SecureRandom的坑

    之前写随机数的时候一直用SecureRandom.getInstanceStrong()方法生成SecureRandom实例,进而调用其各种next方法.突然有一次,发现next方法卡住了, 每一次调 ...

  5. Windows+anaconda+jupyter notebook+R+python3.6

    Windows+anaconda+jupyter notebook+R+python3.6 环境配置 1. 设置国内清华大学镜像 打开 anaconda prompt,输入命令 conda confi ...

  6. 洛谷P2482 [SDOI2010]猪国杀——题解

    猪国杀,模拟题的一颗耀眼的明珠,成长大牛.锻炼码力必写题! 模拟题没什么思维难度.只要按部就班地去做就是.模拟简单在这,难也在这.因为题面巨长,条件巨多,忽疏一点都有可能全盘皆输.故推荐考试时碰见了, ...

  7. Springboot(八):emoji表情保存到mysql出错的解决办法

    emoji表情保存到mysql出错的解决办法 今天,在前端的说明信息中输入emoji表情的时候,插入数据库会报错 百度了一下,是因为mysql数据库的字符编码集不正确,utf8无法存入表情字符,只能将 ...

  8. WebClient上传下载文件,小白篇

    WebClient的上传文件一直报错,各种百度各种稀奇古怪的东西,终于百度到一篇小白学习篇 转自: https://www.cnblogs.com/cncc/p/5722231.html 使用C#We ...

  9. 【错误记录】windows python 路径中的一个转义错误:'rawunicodeescape' codec can't decode bytes in position 112-113: truncated \uXXXX

    ur"D:\work\结构化\CSV\useful\内容.csv" 报错 编码错误原因,当路径中有\u这种字串时,即使是包含在r"" 中也会进行转义,然后转义出 ...

  10. 同一个tomcat部署多个项目

    在开发项目中,有时候我们需要在同一个tomcat中部署多个项目,小编之前也是遇到了这样的情况,碰过不少的壁,故整理总结如下,以供大家参考.(以Linux为例,其他系统同样适用) 一.首先将需要部署的项 ...