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. 如何处理错误信息 Pricing procedure could not be determined

    当给一个SAP CRM Quotation文档的行项目维护一个产品时,遇到如下错误信息:Pricing procedure could not be determined 通过调试得知错误消息在fun ...

  2. C语言 Printf函数

    #include <stdio.h> int main(int argc, const char * argv[]) { // insert code here... printf(&qu ...

  3. 【[CTSC2018]混合果汁】

    题目 \(CTSC\)也有这么水的题啊 首先看到美味值来自于最小的美味值,我们就可以先考虑把所有的果汁按照美味值排序 接下来可以考虑二分,二分出一个\(mid\)我们只使用美味值大于等于\(mid\) ...

  4. H5与安卓、IOS的交互,判断微信、移动设备、安卓、ios

    一.通过用户代理可以判断网页当前所在的环境 var browser={ versions:function(){ var u = navigator.userAgent, app = navigato ...

  5. HashMap面试知识点

    HashMap的工作原理是近年来常见的Java面试题. 几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道Hashtable和HashMap之间的区别,那么为何这道面试题如 ...

  6. Android onMeasure 方法的测量规范MeasureSpec

    一个MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureSpec代表了一组宽度和高度的要求.一个MeasureSpec由大小和模式组成.它有三种模式:UNSPECIFIED(未 ...

  7. C#基础 一(方法详解)

    需要知道:类和方法的关系 方法和参数修饰符 自定义方法可以有或没有参数,也可以有或没有返回值.可以被各种关键字(static.virtual.public.new等)修饰以限制其行为. C#参数修饰符 ...

  8. oracle官网下载教程

    1.百度搜索oracle   也可以直接点击进入   oracle官网   或直接进入   下载页面 2.选择中文,看的更容易些 3.拉到最下面,选择所有下载和试用 4.选择数据库下载 5.点击下载对 ...

  9. UICollectionViewCell「居左显示」

    UICollectionViewCell「居左显示」 准备: 1.UICollectionView Left Aligned Layout 一款UICollectionView居左显示的约束点击下载_ ...

  10. html5语义化标签——回顾

    html5 头部结构   <!doctype html>    <meta charset=“utf-8”/> <header></header> 页眉 ...