Description

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.
4
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

#include<cstdio>
#include<cstring>
using namespace std;
const int N=105;
int a[N][N];
int b[N];
int main ()
{
int r,max=0;
scanf("%d",&r);
memset(a,0,sizeof(a));
for(int i=1;i<=r;i++)
{
for(int j=1;j<=r;j++)
{
scanf("%d",&a[i][j]);
a[i][j]+=a[i-1][j];
}
}
max=a[1][1];
for(int i=1;i<=r;i++)//从第i行开始到底j行。。。。转化成一维的
{
for(int j=i;j<=r;j++)
{
memset(b,0,sizeof(b));
for(int k=1;k<=r;k++)
{
if(b[k-1]>=0)
b[k]=b[k-1]+a[j][k]-a[i-1][k];
else
b[k]=a[j][k]-a[i-1][k];
if(max<b[k])
max=b[k];
}
}
}
printf("%d\n",max);
return 0;
}

1025:To the max(DP)的更多相关文章

  1. 51Nod 1049:最大子段和(dp)

    1049 最大子段和  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 N个整数组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+ ...

  2. 九度OJ 1153:括号匹配问题 (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5193 解决:2248 题目描述: 在某个字符串(长度不超过100)中有左括号.右括号和大小写字母:规定(与常见的算数式子一样)任何一个左括 ...

  3. poj - 1050 - To the Max(dp)

    题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...

  4. SCUT 125 :笔芯回文(DP)

    https://scut.online/p/125 125. 笔芯回文 题目描述 bxbx有一个长度一个字符串SS,bxbx可以对其进行若干次操作. 每次操作可以删掉一个长度为k(1 \leq k \ ...

  5. POJ 3267:The Cow Lexicon(DP)

    http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submi ...

  6. HPU第三次积分赛-D:Longest Increasing Subsequence(DP)

    Longest Increasing Subsequence 描述 给出一组长度为n的序列,a1​,a2​,a3​,a4​...an​, 求出这个序列长度为k的严格递增子序列的个数 输入 第一行输入T ...

  7. 九度OJ 1077:最大序列和 (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5600 解决:1637 题目描述: 给出一个整数序列S,其中有N个数,定义其中一个非空连续子序列T中所有数的和为T的"序列和&qu ...

  8. HDU 1231:最大连续子序列(DP)

    pid=1231">最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  9. HDU 1081 To The Max (dp)

    题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

随机推荐

  1. 日期和时间特效-查看"今天是否为节假日"

    ———————————————— <script type="text/javascript">                    function start() ...

  2. 怎么利用GitHub

    我们一直用GitHub作为 免费的远程仓库,如果是个人的开源项目,放到GitHub上完全没有问题,其实GitHub就是一个开源协作社区,既可以让 别人参与你的开源项目,也可以参与别人的开源项目,在Gi ...

  3. karma+jasmine自动化测试

    1.安装nodejs,进入项目目录 2.安装karma和相关插件 npm install karma --save-dev npm install karma-jasmine karma-chrome ...

  4. Hadoop 中关于 map,reduce 数量设置

    map和reduce是hadoop的核心功能,hadoop正是通过多个map和reduce的并行运行来实现任务的分布式并行计算,从这个观点来看,如果将map和reduce的数量设置为1,那么用户的任务 ...

  5. 缩放系列(三):一个可以手势缩放、拖拽、旋转的layout

    弄了一个下午,终于搞出来了,PowerfulLayout 下面是一个功能强大的改造的例子: 可以实现以下需求: 1.两个手指进行缩放布局 2.所有子控件也随着缩放, 3.子控件该有的功能不能丢失(像b ...

  6. icon大小

    ldpi mdpi hdpi xhdpi xxhdpi

  7. JS IIFE写法

    IIFE 博客分类: 前端开发   介绍IIFE IIFE的性能 使用IIFE的好处 IIFE最佳实践 jQuery优化 在Bootstrap源码(具体请看<Bootstrap源码解析>) ...

  8. WPF从我炫系列4---装饰控件的用法

    这一节的讲解中,我将为大家介绍WPF装饰控件的用法,主要为大家讲解一下几个控件的用法. ScrollViewer滚动条控件 Border边框控件 ViewBox自由缩放控件 1. ScrollView ...

  9. 1988: Sn 爆long long 的处理方法

    题目描述 给你两个数 n, p(0 < n,p <= 10^15); a1 = 1;  a2 = 1+2;  a3 = 1+2+3;  ... an = 1+2+3+...+n    Sn ...

  10. CentOS 下配置CUPS

    一.共享打印机的设置 1.在http://www.openprinting.org/printer/HP/HP-LaserJet_1010检查打印机的支持情况,两个企鹅以上表示Mostly,支持大部分 ...