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. 基于ASP.NET MVC定时执行任务调度

    相对FluentScheduler实现定时调度任务的使用简单,配置少的特点,Quartz.Net则配置稍微复杂一些.下面我们就接合一个 ASP.NET MVC网站应用程序的定时执行任务调试的小实例来了 ...

  2. Cheatsheet: 2015 07.01 ~ 07.31

    Java JBoss Drools Tutorial for Beginners Other A Simple File System RebornDB: the Next Generation Di ...

  3. Java开发中经典的小实例-(字符串比较)

    //输入字符串然后与自己定义的数组进行对比,并输出重复次数. public class Test11 {    public static void main(String[] args) {     ...

  4. 三种DSO(标准DSO、写优化DSO、直接更新DSO)、标准DSO覆盖合计规则

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. Json不知道key值情况下获取第一个键值对

    JObject jsonData = new JObject(); jsonData.Add("1", "1"); jsonData.Add("2&q ...

  6. linux 之SCP

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

  7. python collections defaultdict

    class_counts  = defaultdict(int) 一.关于defaultdict 在Python里面有一个模块collections,解释是数据类型容器模块.这里面有一个collect ...

  8. java高薪之路__001_类

    Java中内部类分四种:成员内部类.局部内部类.静态内部类和匿名内部类.要注意静态内部类的调用方式与其他不同,采用的是类似调用类中的静态属性.静态方法的方式 Multi Level 调用不同类中的相同 ...

  9. spring aop搭建redis缓存

    SpringAOP与Redis搭建缓存 近期项目查询数据库太慢,持久层也没有开启二级缓存,现希望采用Redis作为缓存.为了不改写原来代码,在此采用AOP+Redis实现. 目前由于项目需要,只需要做 ...

  10. es6箭头函数中this

    普通函数: $scope.$on('$stateChangeSuccess',function(){this.list = this.getList();}); 箭头函数: $scope.$on('$ ...