直接暴力枚举所有子矩形至少需要O(n^4)的复杂度,显然这不是一个合理的解决方法。

上述方案忽略了矩形之间的联系,进行了过多不必要的计算。

实际上如果固定矩形的左右边界,则底边在i行的矩形内数值之和与底边在i-1行的矩形的关系为 f[i] = s[i] + max(0, f[i - 1]), s[i]表示当前行对应的数值之和。

本题枚举是切入口,通过枚举把所有矩形分成左右边界固定的矩形族,而后再进行动态规划,可降低复杂度至O(n^3)。

acm.hdu.edu.cn/showproblem.php?pid=1081
 
 #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
typedef __int64 LL; const int inf = 0x3f3f3f3f;
const int maxn = + ; int n;
int s[maxn][maxn]; int main(){
while(~scanf("%d", &n)){
memset(s, , sizeof s);
for(int i = ; i <= n; i++) for(int j = ; j <= n; j++) scanf("%d", &s[i][j]);
for(int i = ; i <= n; i++) for(int j = ; j <= n; j++) s[i][j] += s[i][j - ];
int ans = -inf;
for(int i = ; i <= n; i++){
//enumerate left boundary
for(int j = i; j <= n; j++){
//enumerate right boundary
int pre = -inf;
for(int k = ; k <= n; k++){
//dynamic programming
pre = s[k][j] - s[k][i - ] + max(, pre);
ans = max(ans, pre);
}
}
}
printf("%d\n", ans);
}
return ;
}

hdu1081 To the Max的更多相关文章

  1. hdu1081 To The Max 2016-09-11 10:06 29人阅读 评论(0) 收藏

    To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  2. 【 HDU1081 】 To The Max (最大子矩阵和)

    题目链接 Problem - 1081 题意 Given a two-dimensional array of positive and negative integers, a sub-rectan ...

  3. HDU1081:To The Max(最大子矩阵,线性DP)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1081 自己真够垃圾的,明明做过一维的这种题,但遇到二维的这种题目,竟然不会了,我也是服了(ps:猪啊). ...

  4. 区间Dp 暴力枚举+动态规划 Hdu1081

    F - 最大子矩形 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status Des ...

  5. HDU1081 最大字段和 压缩数组

    最大字段和题型,推荐做题顺序: HDU1003    HDU1024     HDU1081  zoj2975  zoj2067 #include<cstdio> #include< ...

  6. HDU1081 最大字段和 压缩数组(单调队列优化)

    最大字段和题型,推荐做题顺序: HDU1003 HDU1024 HDU1081  ZOJ2975 ZOJ2067 #include<cstdio> #include<cstdlib& ...

  7. Kafka副本管理—— 为何去掉replica.lag.max.messages参数

    今天查看Kafka 0.10.0的官方文档,发现了这样一句话:Configuration parameter replica.lag.max.messages was removed. Partiti ...

  8. 排序算法----基数排序(RadixSort(L,max))单链表版本

    转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...

  9. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

随机推荐

  1. js 多选题选项内容显示在标题下

    <body><div class="page-container"> <div class="view-container"> ...

  2. Java基础之一组有用的类——使用正则表达式搜索子字符串(TryRegex)

    控制台程序. 正则表达式只是一个字符串,描述了在其他字符串中搜索匹配的模式.但这不是被动地进行字符序列匹配,正则表达式其实是一个微型程序,用于一种特殊的计算机——状态机.状态机并不是真正的机器,而是软 ...

  3. pg_stat_statements

    Functions pg_stat_statements_reset() returns void pg_stat_statements_reset discards all statistics g ...

  4. Leetcode: Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  5. 树形DP 2013多校8(Terrorist’s destroy HDU4679)

    题意: There is a city which is built like a tree.A terrorist wants to destroy the city's roads. But no ...

  6. 如何为 Eclipse 中的 Java 源文件设置为 UTF-8 默认编码(转)

    要让一个 Java 源文件打开时编码格式为 UTF-8,需要做2件事情: 1)设置Java 源文件的默认编码格式为UTF-8: 2)设置workspace的编码格式为UTF-8. 相应设置如下: 设置 ...

  7. Linux内核之旅 List_entry()

    #include "iostream" #define List_entry(type,member)\ (type *)(()->data)) ) using namesp ...

  8. linux进程自动关闭与dmesg的使用

    一些应用程序,后台服务被关掉.例如内存不足等,可能是操作系统关掉的.这些日志记录在dmesg中. 存储目录:/var/log/dmesg dmesg -T 可以将时间戳转化为可以识别的时间. | he ...

  9. java 虚拟机工具

    jps 命令 可以查询开启了rmi服务的远程虚拟机进程状态. -v jvm参数. jstat -gcutil命令 [cangyue@/System/Library/Frameworks/JavaVM. ...

  10. 有趣的linux命令

    安装工具 debian => apt-get (In Debian like OS) red hat=> yum -y (In Red Hat like OS) mac => bre ...