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. A full JDK must be specified

    当你开发就了,你就发现,你遇到了各种奇葩的问题,结果,自己奇葩了. 背景:由于项目需要做安全恢复测试,然后,就搭一个新的库环境去测试: 配置jboss的运行jdk时,结果,蹦出个:A full JDK ...

  2. February 23 2017 Week 8 Thursday

    In order to be irreplaceable, one must always be different. 想要无可取代,必须与众不同. In recent days, a news ab ...

  3. oozie调用java实例------shell action

    Oozie提供了一个方便的方式来运行任何命令.这可能是Unix命令,Perl或Python脚本,甚至java程序都可以通过Unix shell调用.shell命令运行在任意的Hadoop集群节点上,并 ...

  4. NTU Long-Term Positioning Dataset

    NTU Long-Term Positioning Dataset 地址:http://www.clarenceliang.com/dataset/ 場景:NTU 博理館外廣場 描述:超過一個月連續拍 ...

  5. webapi是如何绑定参数的(How WebAPI does Parameter Binding)

    原文地址 由于工作原因,要使用ASP.NET WEBAPI(非mvc webapi),前几天时间一直很紧张,所以webapi一直将就用,今天下午好不容易有时间终于看了下,解决了自己一直疑惑的问题,在此 ...

  6. Entity Framework 四

    实体框架支持三种类型的查询:1)LINQ to Entities,2)Entity SQL,3)Native SQL LINQ方法语法: LINQ查询语法: 实体SQL: 这种可以简单的了解,不必深入 ...

  7. viewpager中 pagerAdapter使用详解

    必须覆盖以下方法instantiateItem(ViewGroup, int) 这个方法,return一个对象,这个对象表明了PagerAdapter适配器选择哪个对象*放在当前的ViewPager中 ...

  8. android 学习笔记 杂记1

    getIntent().getExtras().get("intent"); 这个intent是数据包装的参数. 比如: Intent intent = new Intent(th ...

  9. Angular将填入表单的数据渲染到表格

    一.项目简介 我们将采用Angular框架来做一个demo,这个demo将要实现的功能如下: 在X坐标和Y坐标文本框输入信息,然后点击添加,就会在下面表格 中出现一项相应的数据,点击每一项旁边的删除按 ...

  10. 还在占用存储的进程lsof grep deleted;

    查看僵尸进程 lsof grep deleted; 用于查看已经停止但还在占用存储的进程