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. Linux Shell : Test命令参数解析

    格式: test conditions test -n string : string 不为空 test -z string : string 为空 test int1 -eq int2  : int ...

  2. java编码问题

    工作中经常遇到java编码问题,由于缺乏研究,总是无法给出确切的答案,这个周末在网上查了一些资料,在此做些汇总. 问题一:在java中读取文件时应该采用什么编码? Java读取文件的方式总体可以分为两 ...

  3. malloc without free, what happens?

    It's per-process. Once your process exits, the allocated memory is returned to the OS for use by oth ...

  4. Entity Framework 学习中级篇4—存储过程(下)

    在EF中,各个实体的插入.更新和删除也都通过使用存储过程来完成,以便提高点性能.这个类似于数据集.其步骤是:先定义存储过程,然后在VS的可视化设计器,设置存储过程映射即可. 下面,以为Supplier ...

  5. Linux KVM 安装配置

    --------------------------一.前言二.环境三.安装与配置四.创建kvm虚拟机 一.前言 KVM,即Kernel-based Virtual Machine的简称,是一个开源的 ...

  6. 关于安卓HTTP请求用HttpUrlConnection还是HttpClient好

    安卓和JAVA应用开发少不了要提交HTTP请求,而基本上目前有两个实现方式:HttpUrlConnection(即URL.openConnection)和HttpClient. 网上不少人都认为Htt ...

  7. 四种xml的解析方式

    这篇文章是我上网找资料,加上自己总结了一些而得 资料来源: http://www.cnblogs.com/allenzheng/archive/2012/12/01/2797196.html http ...

  8. Sea Battle

    Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  9. PID控制学习笔记(二)

    不管是基本的PID控制还是变形的PID控制算法,其核心都是对输入信号(设定值信号.测量信号或者偏差信号等)做基本的比例.积分.微分运算,最终提供给被控过程良好的调节信号. 在过程控制仪表,特别是在数字 ...

  10. python 基本的序列和映射规则

    >>> def checkIndex(key):...     if not isinstance(key,(int,long)):raise TypeError...     if ...