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 ...
随机推荐
- nodejs发起HTTPS请求并获取数据
摘要:在网站中有时候需要跨域请求数据,直接用Ajax无法实现跨域,采用其他方式需要根据不同的浏览器做相应的处理.用Nodejs可以很好的解决这些问题,后台引用HTTPS模块,发送和返回的数据均为JSO ...
- redhat vim编辑器永久添加行号
cd ~ vim .vimrc 第一行加入: set nu :wq 保存退出,即可 如果想取消设置,同理删除set nu即可
- spring AutowireCapableBeanFactory 自动注入
文档:http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/config/Auto ...
- 使用Navicat for Oracle新建表空间、用户及权限赋予---来自烂泥
Navicat for Oracle是有关Oracle数据库的客户端工具.通过这个客户端,我们可以图形方式对Oracle数据库进行操作. 说 明我们此次试验的Oracle数据库版本是Oracle 10 ...
- 某个点到其他点的曼哈顿距离之和最小(HDU4311)
Meeting point-1 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- FAQ: c++ 函数名后添加 const void function1(int &id) const
说明这个函数不能修改这个类的成员变量!只能操作自己的参数和内部的范围变量! 括号内的&id,&表示这个变量和C# in和out是一样的,算是一个reference,可以更改值,要想不更 ...
- [原创]java WEB学习笔记68:Struts2 学习之路-- 类型转换与复杂属性配合使用
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 关于GridView只显示一样的问题
如果GridView不管怎么改都只能显示一行的话,就重写GridView,自定义GridView: public class MyGridView extends GridView { public ...
- URAL 1146 Maximum Sum(DP)
Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the large ...
- Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别
Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别 时间 2012-12-19 20:30:12 CSDN博客原文 http://blog.cs ...