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. 第二次scrum冲击

    1.小组第二次冲刺任务及其完成情况描述. 本次冲刺我们小组经过讨论,实现的使我们爱上长大系统中的失物招领功能,由于在实际的实现中,对于本功能的逐渐深入和了解,渐渐发现这个功能实现起来需要由很多部分组成 ...

  2. c++互斥锁的实现

    class IMyLock { public: virtual ~IMyLock(){} ; ; }; class Mutex : public IMyLock { public: Mutex(); ...

  3. 【洛谷5287】[HNOI2019] JOJO(主席树优化KMP)

    点此看题面 大致题意: 每次往一个字符串末尾加上\(x\)个字符\(c\),或者回到某一历史版本,求\(KMP\)的\(\sum Next_i\). 问题转化 考虑到可以离线. 于是,我们就可以用一个 ...

  4. SP348 EXPEDI - Expedition

    嘟嘟嘟 水贪心. 当经过一个加油站的时候,记下这个加油站能加的油,然后没油的时候从经过的加油站中选择加油最多的加. #include<cstdio> #include<iostrea ...

  5. C# .Net Framework4.5中配置和使用managedCUDA及常见问题解决办法

    主要参考英文帖子.我就不翻译了哈.很容易懂的. 先说明我的运行平台: 1.IDE:Visual Studio 2012 C# .Net Framework4.5,使用默认安装路径: 2.显卡类型:NV ...

  6. 关于layui问题

    编辑: $('#Teacher').find('option').each(function(){ $(this).attr('selected',$(this).val()==data.tid); ...

  7. Windows下同时安装Anaconda2与Anaconda3

    1. 安装一个作为主版本,比如先安装Anaconda2,安装时选择[添加path环境变量].我的安装地址为:E:\ProgramData\Anaconda3 2. 安装另一个版本python,安装时注 ...

  8. Android学习笔记_56_应用Tween动画 (渐变、缩放、位移、旋转)

    1.实现listview每个项先向右移动,再向左移动(回到原来位置) TranslateAnimation ta = new TranslateAnimation( Animation.RELATIV ...

  9. c++ 虚基类应用

    多重继承存在二义性,为了消除二义性在访问相同名称的属性时需要加上类名,加以区分.虽然这样可以解决二义性,但是相同的属性出现在多个基类中,为了解决数据冗余,c++引入了虚基类. 虚基类定义:class ...

  10. spring(一)-基本概念

    1.定义与特点 定义:一个分模块的一站式后台开发框架. 特征: (1)比起EJB,更轻量级别的容器框架,模块形式组织,只需要调用相应模块(jdbc.springmvc) (2)Spring IOC低耦 ...