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. selenium grid 测试资料

    像风一样自由的4篇博客: http://blog.csdn.net/five3/article/details/9671287 http://blog.csdn.net/five3/article/d ...

  2. kafka入门教程

    1.Kafka独特设计在什么地方?2.Kafka如何搭建及创建topic.发送消息.消费消息?3.如何书写Kafka程序?4.数据传输的事务定义有哪三种?5.Kafka判断一个节点是否活着有哪两个条件 ...

  3. 【bzoj3514】Codechef MARCH14 GERALD07加强版

    hzwer上少有的几道需要建一下模的 要不是有这么几道题 我都觉得lct只会考裸题了呢 题解看hzwer吧 http://hzwer.com/4358.html 唯一蛋疼的就是为了处理0这个呵呵的位置 ...

  4. cocos2d-x 关于tilemap滚动时黑线闪动的问题

    改动抗锯齿这个全然没用. 解决问题的方法是开启CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL=1.不是在自己的project中开启,而是改动libcocos2dx库来解决 wa ...

  5. Jinja2模版语言自定义filter的使用

    Jinja2模版语言,自带有一些filter,能够在前端的模版中控制数据按照相应的方式显示.比如以下两种filter,分别能在前端控制数字的近似精度显示和根据字符串长度补齐: round(value, ...

  6. July收集荷兰国旗问题之三路partition

    这道题目和分成两块的partition的扩展.比如有一堆0 1 2 数字组成的数组,要分成 00 00  11 1 1  222 2这样的顺序的. 利用lumoto版的partition能够非常好的解 ...

  7. Swift的闭包(一):闭包简介、闭包表达式的优化

    定义:Closures are self-contained blocks of functionality that can be passed around and used in your co ...

  8. HDU 4607 Park Visit(树的直径)

    题目大意:给定一棵树,让求出依次访问k个点的最小花费,每条边的权值都为1. 思路:如果能一直往下走不回来,那么这个路径肯定是最小的,这就取决于给定的k,但是怎么确定这个能一直走的长度呢,其实这个就是树 ...

  9. 面试题——分析从输入url到页面返回的过程(或者查询返回过程)

    1. You enter a URL into the browser(输入一个url地址) 2.The browser looks up the IP address for the domain ...

  10. Linq101-QueryExecution

    using System; using System.Linq; namespace Linq101 { class QueryExecution { /// <summary> /// ...