hdu1081 To the Max
直接暴力枚举所有子矩形至少需要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的更多相关文章
- 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 ...
- 【 HDU1081 】 To The Max (最大子矩阵和)
题目链接 Problem - 1081 题意 Given a two-dimensional array of positive and negative integers, a sub-rectan ...
- HDU1081:To The Max(最大子矩阵,线性DP)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1081 自己真够垃圾的,明明做过一维的这种题,但遇到二维的这种题目,竟然不会了,我也是服了(ps:猪啊). ...
- 区间Dp 暴力枚举+动态规划 Hdu1081
F - 最大子矩形 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status Des ...
- HDU1081 最大字段和 压缩数组
最大字段和题型,推荐做题顺序: HDU1003 HDU1024 HDU1081 zoj2975 zoj2067 #include<cstdio> #include< ...
- HDU1081 最大字段和 压缩数组(单调队列优化)
最大字段和题型,推荐做题顺序: HDU1003 HDU1024 HDU1081 ZOJ2975 ZOJ2067 #include<cstdio> #include<cstdlib& ...
- Kafka副本管理—— 为何去掉replica.lag.max.messages参数
今天查看Kafka 0.10.0的官方文档,发现了这样一句话:Configuration parameter replica.lag.max.messages was removed. Partiti ...
- 排序算法----基数排序(RadixSort(L,max))单链表版本
转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...
- [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 ...
随机推荐
- Azure billing 分析
昨天把西欧的2012的VM删掉,在北美新建一个2008的VM,装了sql2005 express 在C盘,这样存储就变成2个位置了,西欧和美国,然后放在那里不操作一天,发现billing多了很多, S ...
- Git_Commands
- spring AutowireCapableBeanFactory 自动注入
文档:http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/config/Auto ...
- For循环输出九九乘法表
题:使用For循环输出九九乘法表 解析: 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 .... 1*9=9 ........ .....9*9=81 可以看做j*i ...
- 在Visual Studio 2013/2015上使用C#开发Android/IOS安装包和操作步骤
Xamarin 配置手册和离线包下载 http://pan.baidu.com/s/1eQ3qw8a 具体操作: 安装前提条件 1. 安装Visual Studio 2013,安装过程省略,我这里安装 ...
- 夺命雷公狗---DEDECMS----15dedecms首页栏目列表页导航部分完成
我们在点击导航页面的连接时候我们需要我们的连接跳到指定的模版页面,而不是随便跳到一个指定的A连接标签: 所以我们首先要将前端给我们的栏目列表模版拷贝到目录下,然后就可以创建栏目列表页面了,但是名字我们 ...
- java - Annotation元数据
Annotation元数据(一) 一.Annotation究竟是什么? 是java5.0中的新特征 数据的数据(元数据) Annotation和访问修饰符一样,应用于包.类型.构造方法.方法.成员变量 ...
- 5 Best Automation Tools for Testing Android Applications
Posted In | Automation Testing, Mobile Testing, Software Testing Tools Nowadays automated tests ar ...
- 项目中的一个JQuery ajax实现案例
/** * brief 这些代码用于在线制图中 attention author <list of authors> <date> begin modify by * nu ...
- selenium启动PhantomJS错误
from selenium import webdriverbrowser = webdriver.PhantomJS(executable_path="D:\PhantomJS\phant ...