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.

Sample Input

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

Sample Output

15

题目翻译过来,就是在一个大矩阵里面,找一个(矩阵和最大)的子矩阵。

如题意:  
0 -2 -7 0 
9 2 -6 2  9 2
-4 1 -4 1      -4 1
-1 8 0 -2  -1 8 9+2+(-4)+1+(-1)+8  = = 15 所以输出15 做这题之前,先来了解一下    一维数组子串,找连续数组的最大和 例如 2 3 -7 9 2 -6 9 最大和为(9,2,-6,9) -> 14 如何用算法实现,当然用dp 顺推, 假设数组a 只要a的前项与后项的和大于0,保留,继续比较,在这个过程中要存储max的值 正如 上面例子
  2  3   -7  9 2  -6  9    

遍历一遍   (前项加本项等于本项的值,但形成负数的时候,要归零)

 形成   2    5(2+3)    0 (5+-7小于0,归零)  9     11(9+2)    5 (11-6)   14(5+9)

同样本题是这个思想的延伸,扩展到了二维

第一步:
0 -2 -7 0     -> max=0
9 2 -6 2      -> max=11
-4 1 -4 1     -> max=1 四个合起来 ,得max=11
-1 8 0 -2     -> max=8 第二步(二维压缩成一维 ):
第一行与第二行相加保留在第二行,然后继续压缩,最后压缩成了一维。中间过程要与max比较,取较大值.........


AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#define Max(a,b) ((a)>(b)?:(a):(b))
#define Min(a,b) ((a)<(b)?:(a):(b))
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
int n,a[1010][1010];
/*
int dp(int b[])
{
int i,ans=b[0];
for (i=0;i<n;i++)
printf("%d\t",b[i]);
printf("******");
for (i=0;i<n;i++){
if (b[i]>0)
b[i+1]+=b[i];
if (ans<b[i])
ans=b[i];
printf("%d\t",b[i]);
}
printf("%d\t",b[i]);
return ans;
}*/
int main()
{
int n;
while (cin>>n){
int i,j,k,ans=-inf;
Mem0(a);
for (i=0;i<n;i++){
int tmp=0;
for (j=0;j<n;j++){
cin>>a[i][j];
if (tmp>0)
tmp+=a[i][j];
else tmp=a[i][j];
if (tmp>ans)
ans=tmp;
}
}
for (i=0;i<n-1;i++){
for (j=i+1;j<n;j++){
int tmp=0;
for (k=0;k<n;k++){
a[i][k]+=a[j][k];
if (tmp>0)
tmp+=a[i][k];
else
tmp=a[i][k];
if (tmp>ans)
ans=tmp;
}
}
}
printf("%d\n",ans);
} // system("pause");
return 0;
}
 

poj1050 dp动态规划的更多相关文章

  1. Day 5 笔记 dp动态规划

    Day 5 笔记 dp动态规划 一.动态规划的基本思路 就是用一些子状态来算出全局状态. 特点: 无后效性--狗熊掰棒子,所以滚动什么的最好了 可以分解性--每个大的状态可以分解成较小的步骤完成 dp ...

  2. (转)dp动态规划分类详解

    dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间 ...

  3. 【模板整合计划】DP动态规划

    [模板整合计划]DP动态规划 一:[背包] 1.[01背包] 采药 \([P1048]\) #include<algorithm> #include<cstdio> int T ...

  4. DP动态规划学习笔记——高级篇上

    说了要肝的怎么能咕咕咕呢? 不了解DP或者想从基础开始学习DP的请移步上一篇博客:DP动态规划学习笔记 这一篇博客我们将分为上中下三篇(这样就不用咕咕咕了...),上篇是较难一些树形DP,中篇则是数位 ...

  5. 树形DP——动态规划与数据结构的结合,在树上做DP

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法与数据结构的第15篇,也是动态规划系列的第4篇. 之前的几篇文章当中一直在聊背包问题,不知道大家有没有觉得有些腻味了.虽然经典的文 ...

  6. [原]POJ1141 Brackets Sequence (dp动态规划,递归)

    本文出自:http://blog.csdn.net/svitter 原题:http://poj.org/problem?id=1141 题意:输出添加括号最少,并且使其匹配的串. 题解: dp [ i ...

  7. DP动态规划练习

    先来看一下经典的背包问题吧 http://www.cnblogs.com/Kalix/p/7617856.html  01背包问题 https://www.cnblogs.com/Kalix/p/76 ...

  8. 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  9. 摆花 (DP动态规划)

    2012_p3 摆花 (flower.cpp/c/pas) 时间限制: 1 Sec  内存限制: 128 MB提交: 17  解决: 10[提交][状态][讨论版][命题人:外部导入] 题目描述 3. ...

随机推荐

  1. March 20 2017 Week 12 Monday

    A goal is a dream with a deadline. 目标就是给梦想一个期限. Dream without dealine is just daydream, because you ...

  2. 欢迎来到“火龙族智者”的blog

    本blog里有每天更新的比赛感想,新技术体会以及日语学习相关事宜. 主要研究方向是算法,信息安全以及日语. 希望各位能常来看看.

  3. ImageNet Classification with Deep Convolutional Nerual Networks(AlexNet)

    Architecture: 整个网络8层,5个卷积层,3个全连接层 Relu Nonlinearity: 非饱和的relu比饱和的sigmoid或者tanh训练速度快很多,并有效解决梯度消失 Over ...

  4. Maven Dependencies missing jar 解决

    在导入SVN项目之后发现Maven里面的pom.xml报错. 发现是Maven Dependencies 里面的jar包不完整. 我试过手动加入jar,但是不能成功,然后就又试着添加dependecn ...

  5. Cesium.js隐藏logo等信息

    css: .cesium-widget-credits{ display:none!important;}js: var viewer = new Cesium.Viewer('cs', { anim ...

  6. 【luogu P3807 卢卡斯定理】 模板

    题目链接:https://www.luogu.org/problemnew/show/P3807 Lucas定理用来求大组合数对一个质数取模的值,不大于10^5用逆元,大于10^5就用Lucas转化成 ...

  7. Extjs 4 动态显示折线图 按秒显示

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  8. A Year in Computer Vision

    A Year in Computer Vision http://themtank.org/

  9. how to create a custom form for sharepoint list

    在VS中创建一个applicationPage映射到Layouts文件夹下,然后代码如下: SPList lstTest = web.Lists["Shared Documents" ...

  10. 石头剪刀布的JAVA小程序 供初学者参考

    package youxi; public class Player { private String name; private double score; public Player(String ...