To the Max


Time Limit:1 Second     Memory Limit:32768
KB


Problem

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 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.

The input consists of an N x 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.

Example

Input

4

0 -2 -7 0 9 2 -6 2

-4 1 -4 1 -1

8 0 -2


Output

15





解题心得:

1、是一个很明显的动态规划,但是一开始的思路有点混乱,所以在处理的时候可以参照一下的方法:    
          在输入的时候就将矩阵中的每一个数改写为同一行中前面I个数的和(这样才能处理子矩阵)。
          在处理列的时候数字就已经是行的和,这样就可以得到矩阵的和,所以处理子矩阵行的时候只能使用循环得出答案,记录最大的那个子矩阵。

2、在做动态规划的题的时候首先要看出这个题是否可以用动态规划的思想来进行处理,在感觉普通思路行不通的时候可以试着用动态规划的思想来试试。

3、在确定使用动态规划的时候就可以开始推动态转移(注意记录状态,先想最简单的转移,再逐步优化)的方程式,在数据不是很方便的时候可以改写一下数据。例如改写成 几 个数据的和,差。






#include<bits/stdc++.h>
using namespace std;
const int maxn = 150;
int maps[maxn][maxn];
int main()
{
int n,sum,Max;
while(~scanf("%d",&n))
{
memset(maps,0,sizeof(maps));
Max = -1000000;
for(int i=1;i<=n;i++)//考虑为什么要从1开始输入
for(int j=1;j<=n;j++)
{
int t;
scanf("%d",&t);
maps[i][j] = maps[i-1][j] + t;//将每一个数改为每一列的和
}
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
sum = 0;
for(int k=1;k<=n;k++)
{
int K;
K = maps[j][k]-maps[i-1][k];
sum += K;//处理列的方法
sum = max(sum,0);//当sum小于0的时候可以直接舍去,参考最长子序列
Max = max(Max,sum);//记录最大的那个子矩阵的和
}
}
}
printf("%d\n",Max);
}
return 0;
}

动态规划:ZOJ1074-最大和子矩阵 DP(最长子序列的升级版)的更多相关文章

  1. ZOJ1074 (最大和子矩阵 DP)

    F - 最大子矩阵和 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u   Descri ...

  2. HDU 1160 DP最长子序列

    G - FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  3. nyoj 17-单调递增最长子序列 && poj 2533(动态规划,演算法)

    17-单调递增最长子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:21 submit:49 题目描述: 求一个字符串的最长递增子序列的长度 如 ...

  4. HDU 4123 (2011 Asia FZU contest)(树形DP + 维护最长子序列)(bfs + 尺取法)

    题意:告诉一张带权图,不存在环,存下每个点能够到的最大的距离,就是一个长度为n的序列,然后求出最大值-最小值不大于Q的最长子序列的长度. 做法1:两步,第一步是根据图计算出这个序列,大姐头用了树形DP ...

  5. 最长子序列dp poj2479 题解

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44476   Accepted: 13796 Des ...

  6. nyoj17-单调递增最长子序列-(dp)

    17-单调递增最长子序列 内存限制:64MB 时间限制:3000ms 特判: No通过数:125 提交数:259 难度:4 题目描述: 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列 ...

  7. 动态规划(1)——最长子序列(LCS)问题

    最长子序列问题:从中找出最长的字符序列,比如: cnblogs和belong.这两个字符串的最长子序列就是blog. 动态规划:通过分解大问题,不断的将大问题变成小问题,最终整合所有解,得出最优解(和 ...

  8. 算法:Common Subsequence(动态规划 Java 最长子序列)

    Description A subsequence of a given sequence is the given sequence with some elements (possible non ...

  9. HDU 1513 最长子序列

    Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. SpringBoot | 第九章:Mybatis-plus的集成和使用

    前言 本章节开始介绍数据访问方面的相关知识点.对于后端开发者而言,和数据库打交道是每天都在进行的,所以一个好用的ORM框架是很有必要的.目前,绝大部分公司都选择MyBatis框架作为底层数据库持久化框 ...

  2. hibernate丢失更新

    如果多个线程操作基于同一个查询结构对表中的记录进行修改,那么后修改的记录将会覆盖前面修改的记录,前面的修改就丢失掉了,这就叫做更新丢失.Serializable可以防止更新丢失问题的发生.其他的三个隔 ...

  3. CSS单词换行and断词,你真的完全了解吗

    背景 某天老板在群里反馈,英文单词为什么被截断了? 很显然,这是我们前端的锅,自行背锅.这个问题太简单了,css里加两行属性,分分钟搞定. 开心的提交代码,刷新页面.我擦,怎么还是没有断词?不可能啊! ...

  4. Tomcat Stack-8.0.35 (OpenLogic CentOS7.2)

       平台: CentOS 类型: 虚拟机镜像 软件包: apache2.4.20 mysql5.6.30 tomcat8.0.35 apache application server basic s ...

  5. mysqlbinlog 查看执行的sql (row模式)

    记录一下:当bin-log的模式设置为 row时 不仅日志长得快 并且查看执行的sql时 也稍微麻烦一点:1.干扰语句多:2生成sql的编码需要解码. binlog_format=row 直接mysq ...

  6. GitLab一个非标准的端口远程仓库导致clone失败

    首先看下报错信息 当gitlab服务器ssh端口不是默认的22时,使用ssh连接gitlab会出现上面的错误 解决方法: 修改/etc/gitlab/gitlab.rd gitlab_rails['g ...

  7. UVA Mega Man's Mission(状压dp)

    把消灭了那些机器人作为状态S,预处理出状态S下可以消灭的机器人,转移统计方案.方案数最多16!,要用64bit保存方案. #include<bits/stdc++.h> using nam ...

  8. hdu-1874 畅通工程续---模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目大意: 求起点到终点的最短距离 解题思路: 注意重边,其他的就是模板 #include&l ...

  9. 2017.12.10 Java写一个杨辉三角(二维数组的应用)

    杨辉三角的定律 第n行m列元素通项公式为: C(n-1,m-1)=(n-1)!/[(m-1)!(n-m)!] 需要用到创建二维数组 package com.glut.demo; /** * 杨辉三角 ...

  10. springboot集成shiro的session污染问题

    问题起因是这样的,有两套系统,系统a和系统b.两套系统均使用shiro做的权限管理,之前部署在两台机器上.使用浏览器打开a系统后另开页签打开b系统,互不干扰都能正常使用,后因业务迁移,两套系统部署到了 ...