To the Max


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Problem

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 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.

The input consists of an N x 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.

Input

4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1
8 0 -2

Output

15

代码如下:

 # include<stdio.h>
# include<string.h>
# define N
int main(){
int a[N][N],b[N];
int n,i,j,k;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<n;i++)
for(j=;j<n;j++)
scanf("%d",&a[i][j]);
int max= -;
for(i=;i<n;i++) //第i行到第j行的最大子矩阵和
{
memset(b,,sizeof(b));
for(j=i;j<n;j++)
{
int sum=;
for(k=;k<n;k++)
{
b[k] += a[j][k];
sum += b[k];
if(sum<) sum=b[k];
if(sum>max) max=sum;
}
}
}
printf("%d\n",max);
}
return ;
}

ZOJ 1074 To the Max(DP 最大子矩阵和)的更多相关文章

  1. ZOJ 1074 To the Max

    原题链接 题目大意:这是一道好题.在<算法导论>这本书里面,有一节是介绍如何求最大子序列的.这道题有点类似,区别是从数组变成了矩阵,求最大子矩阵. 解法:完全没有算法功底的人当然不知道最大 ...

  2. ZOJ 3703 Happy Programming Contest(DP)

    题目链接 输出路径,搞了一个DFS出来,主要是这里,浪费了好长时间. #include <cstdio> #include <string> #include <cstr ...

  3. ZOJ 3211 Dream City(DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3374 题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上 ...

  4. ZOJ 2702 Unrhymable Rhymes(DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1702 题目大意:给定有很多数字组成的诗,譬如 “AABB”, “AB ...

  5. ZOJ 3471 Most Powerful(DP + 状态压缩)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 题目大意:有 n(2<=n<=10) 个原子,每两 ...

  6. ZOJ 2852 Deck of Cards DP

    题意: 一一个21点游戏. 1. 有三个牌堆,分别为1X,2X,3X. 2. 纸牌A的值为1,纸牌2-9的值与牌面面相同,10(T).J.Q.K的值为10,而而joke(F)的值为 任意大大. 3. ...

  7. ZOJ 3623 Battle Ships 简单DP

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3623 题意:给出N种可以建造的船和对方的塔生命值L,每种船给出建造时 ...

  8. ZOJ 4027 Sequence Swapping(DP)题解

    题意:一串括号,每个括号代表一个值,当有相邻括号组成()时,可以交换他们两个并得到他们值的乘积,问你最大能得到多少 思路:DP题,注定想得掉头发. 显然一个左括号( 的最远交换距离由他右边的左括号的最 ...

  9. ZOJ 3211 Dream City(线性DP)

    Dream City Time Limit: 1 Second      Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he se ...

随机推荐

  1. Bzoj 4196: [Noi2015]软件包管理器 树链剖分

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 721  Solved: 419[Submit][Statu ...

  2. Python的基本语法,涵盖数据类型、循环判断、列表、map和set等

    以#开头的语句是注释 当语句以冒号“:”结尾时,缩进的语句视为代码块.一般缩进4个空格 Python程序是大小写敏感的,如果写错了大小写,程序会报错. Python的数据类型 整型 浮点型 字符串 布 ...

  3. 初学python(print使用、条件分支、循环、模块引用)

    import random """ #查看源代码日后爬虫用 import urllib.request # coding=utf-8 url = "http:/ ...

  4. 各种jee服务器的比较,tomcat, jboss, glassfish, websphere, weblogic

    tomcat, 开源,只是一个 servlet jsp 容器. jboss, 开源,是一个j2ee 应用服务器,容器支持 servlet, jsp, ejb,jms等. 稳定且实现了全部j2ee ap ...

  5. 升级OpenSSH详细步骤

    由于系统扫描到OpenSSH版本太低,所以需要将其升级到高版本.网上搜罗数个文章,都多多少少有点疏漏.加上自己之前没升级过SSH,参考好几个文章查缺补漏才升级成功,着实废了不少劲儿.所以综合一下前辈们 ...

  6. Python自动化之session

    request.body 所有请求内容的原生数据 request.META 所有请求头的原生数据 cookie返回存在于响应头里面 session session是保存在服务端的键值对 cookie和 ...

  7. Android——打造万能适配器(CommonAdapter)

    List<T> : ListView -> Adapter extends BaseAdapter -> ViewHolder ViewHolder CommonAdaper ...

  8. 微信支付 V3版

    本人小菜鸟一仅仅.为了自我学习和交流PHP(jquery,linux,lamp,shell,javascript,server)等一系列的知识,小菜鸟创建了一个群.希望光临本博客的人能够进来交流. 寻 ...

  9. myecipse的debug调试操作方法

    在myecipse如果想要查询某个变量的值,或者跟踪程序的执行流程,可以如下操作: 首先在程序中设置好断点(断点的设置方法,就是在想要设置的地方的行首双击,当一个蓝色的圆形实心图标显示出来,就证明你设 ...

  10. 【转】iOS应用崩溃日志揭秘

    这篇文章还可以在这里找到 英语 If you're new here, you may want to subscribe to my RSS feed or follow me on Twitter ...