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. [AS/400] Control Language

    下面是一个简单的 CL 例子,转换日期格式:从 Julian 到 MDY,或者反方向转换. 接受两个参数,日期值 IN,目标类型 TYP,将转换后的日期值存入 OUT 中. PGM (&IN ...

  2. DBCP,C3P0,Tomcat_JDBC 性能及稳定性测试

    1.测试环境: 硬件环境: 数据库服务器:2U*8核 8G内存 测试服务器:   2U*8核 6G内存 软件环境: jdk: 1.6.29 mysql: 5.0.77 mysql_driver: my ...

  3. 想挑战AlphaGO吗?先和PostgreSQL玩一玩?? PostgreSQL与人工智能(AI)

    1月4日晚,随着古力认输,Master对人类顶尖高手的战绩停留在60胜0负1和,而令人尴尬的是这唯一一场和棋还是因为棋手掉线系统自动判和,并不是棋盘上的局势真的势均力敌了.包括聂卫平.柯洁.朴廷桓.井 ...

  4. Craking the coding interview 面试题:完美随机洗牌

    给定一个序列,随机打乱这个序列,新产生的序列和任意一个序列产生的可能性是一样的,就是所谓的完美随机洗牌. 看下面的运行结果: 上面第一列是原数列,下面一行是新产生的打乱的数列. 基本思想:如果n-1个 ...

  5. Demo_塔防(自动生成怪物,导航,炮塔攻击,怪物掉血死忙)

    using UnityEngine; using System.Collections; public struct WaveMsg { //该波次生成的怪物 public GameObject mo ...

  6. Dota兄订餐——静态代理(java)

    理解并使用设计模式,能够培养我们良好的面向对象编程习惯,同时在实际应用中,可以如鱼得水,享受游刃有余的乐趣. 代理模式是比较有用途的一种模式,而且变种较多,应用场合覆盖从小结构到整个系统的大结构,Pr ...

  7. android strings.xml转义字符, 注意细节解决(转)

    XML转义字符 以下为XML标志符的数字和字符串转义符 "     (" 或 ") '     (' 或 &apos;) &     (& 或 & ...

  8. 【开源java游戏框架libgdx专题】-14-系统控件-Skin类

    Skin类主要用于存储用户界面的资源,该资源主要用于窗口部件.这些资源也包括纹理图片.位图画笔.颜色等内容.方便创建游戏组件,同时使用Skin也可以批量的粗略处理一些窗口部件. test.json { ...

  9. Django Errors Archive

    记录使用 Django 开发中遇到的问题,备用 1. 版本要选好,最好安装上 pip,可以省很多麻烦 2. 如果使用 Postgresql,选 8.1 之后的版本,免去 Retruning 之类的错误 ...

  10. GridView布局及适配器优化

    1.布局样式 <GridView android:id="@+id/gridView" android:layout_width="fill_parent" ...