F - 最大子矩阵和

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

 

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

题解:怎么说呢。。就是第一行的一个序列加到第二行,找到他们的和序列的最大子序列,然后他们的和序列再加第三行,再找,每加一次找一次,加到第n行。
然后从第二行开始,按照上面的进行,再从第三行开始..... 直到第n行....
每次都要更新他们的子矩阵的的最大值,用一个变量更新
不说了,上代码,最下面有输出截图
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,sum,f,Max;
int b[];
int dp[][];
int main()
{
f=-;
cin>>n;
for(int i=; i<n; i++)
for(int j=; j<n; j++)
{
cin>>dp[i][j];
}
for(int k=; k<n; k++)
{
memset(b,,sizeof(b)); // 一定每次记得清零
for(int i=k; i<n; i++)
{
for(int j=; j<n; j++)
{
b[j]+=dp[i][j];
}
sum = Max =-; //赋很小的值
for(int i=; i<n; i++)
{
if (sum<)
sum = b[i];
else
sum += b[i];
if (Max < sum)
Max = sum;
}
if(f<Max) // 每次都要比较更新最大子矩阵和
f=Max;
}
}
cout<<f<<endl;
}


ZOJ1074 (最大和子矩阵 DP)的更多相关文章

  1. 动态规划:ZOJ1074-最大和子矩阵 DP(最长子序列的升级版)

    To the Max Time Limit:1 Second     Memory Limit:32768 KB Problem Given a two-dimensional array of po ...

  2. bzoj1084: [SCOI2005]最大子矩阵 dp

    这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. 题解:m很小分类讨论,m==1时怎么搞都可以,m==2时,dp[i][j][k]表 ...

  3. BZOJ 1084: [SCOI2005]最大子矩阵 DP

    1084: [SCOI2005]最大子矩阵 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1084 Description 这里有一个n* ...

  4. HUD-1559 最大子矩阵,dp模拟

    最大子矩阵                                                                                               ...

  5. P2258 子矩阵(dp)

    P2258 子矩阵 题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵. 例如,下面左图中选取第2.4行和第2.4 ...

  6. BZOJ 1057: [ZJOI2007]棋盘制作 悬线法求最大子矩阵+dp

    1057: [ZJOI2007]棋盘制作 Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑 ...

  7. 洛谷P2331 [SCOI2005]最大子矩阵 DP

    P2331 [SCOI2005]最大子矩阵 题意 : 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. 第一行为n,m,k(1≤n≤ ...

  8. URAL_1146/uva_108 最大子矩阵 DP 降维

    题意很简单,给定一个N*N的大矩阵,求其中数值和最大的子矩阵. 一开始找不到怎么DP,没有最优子结构啊,后来聪哥给了我思路,化成一维,变成最大连续和即可.为了转化成一维,必须枚举子矩阵的宽度,通过预处 ...

  9. UVA-10074 最大子矩阵 DP

    求出大矩阵里面全为0的最大子矩阵 我自己用的个挫DP写的,感觉写的不是很好,其实可以再优化,DP想法就是以 0 0 到当前 i j 为整体矩阵考虑,当前 i j就是从 i-1 j或者 i,j-1那里最 ...

随机推荐

  1. How to Implement the IContextMenu Interface

    http://msdn.microsoft.com/en-us/library/windows/desktop/hh127443(v=vs.85).aspx IContextMenu is the m ...

  2. redis合库

    玩家数据全部保存在redis,对合服来绝对是个坑.因为一直都是做开发,合库这事还是第一次操作. 首先,合服要做哪些事情,当然不同的游戏肯定不一样.合服的目的是为了增加同个服务器上活跃玩家的数量.合服有 ...

  3. Apache-Tika解析Excell文档

    通常在使用爬虫时,爬取到网上的文章都是各式各样的格式处理起来比较麻烦,这里我们使用Apache-Tika来处理Excell格式的文章,如下: package com.mengyao.tika.app; ...

  4. oracle 零散知识汇集

    1. Select '登陆' + 2 From dual会报错: ora- 01722 无效数字,原理是oracle把'登陆'当成数字来和2进行加法运算. Select '登陆'|| 2 From d ...

  5. 根据新浪天气API获取各地天气状况(Java实现)

    原文出自 参考网址(重要) http://blog.csdn.net/cyxlzzs/article/details/7602469  新浪 http://blog.csdn.net/l_ch_g/a ...

  6. Spark GraphX的函数源码分析及应用实例

    1. outerJoinVertices函数 首先给出源代码 override def outerJoinVertices[U: ClassTag, VD2: ClassTag] (other: RD ...

  7. 一起学android之怎样获取手机程序列表以及程序相关信息并启动指定程序 (26)

    效果图: 程序列表: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fonts ...

  8. select poll epoll三者之间的比较

    一.概述 说到Linux下的IO复用,系统提供了三个系统调用,分别是select poll epoll.那么这三者之间有什么不同呢,什么时候使用三个之间的其中一个呢? 下面,我将从系统调用原型来分析其 ...

  9. Python之路,Day21 - 常用算法学习

    Python之路,Day21 - 常用算法学习   本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...

  10. 提升升级 强制更新 Download

    Activity和广播 /** 下载APK细节 1.点击升级后对话框不消失,再次点击时不能重复下载 2.下载过程中退出APP,下次进入应用后要重新下载(因为可能不完整) 3.下载过程中退出APP(或下 ...