HDU 2490 Parade(DPの单调队列)(2008 Asia Regional Beijing)
Description
When seeing his Citizens, Panagola always waves his hands. He may get tired and need a break. So please never make Panagola travel in a same west-east road for more than k minutes. If it takes p minutes to pass a love-hate zone, we say the length of that love-hate zone is p. Of course you know every love-hate zone’s length.
The figure below illustrates the case in sample input. In this figure, a best route is marked by thicker lines.
Input
The first line contains three integers: n, m and k.(0<n<=100,0<m<=10000, 0<=k<=3000000)
The next n+1 lines stands for n + 1 west-east roads in north to south order. Each line contains m integers showing the welcome values of the road’s m love-hate zones, in west to east order.
The last n+1 lines also stands for n + 1 west-east roads in north to south order. Each line contains m integers showing the lengths (in minutes) of the road's m love-hate zones, in west to east order.
Output
题目大意:有一个n*m的矩阵,只能沿着边走,只能往左、往右或往上走,在同一行只能沿一个方向走(走了左边就不能返回走右边了)。打横的边都有一个权值(可能为负数)和一个长度,每行走过的长度不能超过k,打竖的边没有权值和长度。先要从最下面的任意一个点开始,走到最上面的任意一个点,问最大权值和为多少(答案不超过$2^{31}-1$,虽然题目不是这么说的)。
思路:一看就是动态规划,每一行只和上一行的状态有关。因为习惯从小到大循环我们从上往下走,反正都一样。设dp[i][j]为走到第 i 行第 j 个点的最大权值(已往左往右走完),那么dp[i][j] = max(dp[i-1][x] + sum(welcome[i][y])),distance(x, y) ≤ k,y in [x, i]。其中distance和sum(welcome[i][y])可以预处理出来(如sum[i]代表1~i的和,distance(i, j) = sum[j] - sum[i],i ≤ j),平均到处理每个dp[i][j]身上时间复杂度为O(1)。但是这样计算dp数组,时间复杂度高达$O(nm^2)$。
现假设我们从左到右走,那么dp[i][j] = max(dp[i - 1][x] - sum_welcome[x] + sum_welcome[y]) = dp[i][j] = max(dp[i - 1][x] - sum_welcome[x]) + sum_welcome[y],那么对每一个j,所用的dp[i - 1][x] - sum_welcome[x]都是一样的,这里很容易能想到单调队列优化(如果你知道单调队列的话)。每次把队列末尾小于dp[i - 1][j] - sum_welcome[j]弹出,把队头distance(i, x) > k的弹出,队头就是最佳的dp[i - 1][x] - sum_welcome[x]。优化完时间复杂度为$O(nm)$,已经是读入数据的复杂度了。(这里不介绍单调队列)
PS:可恶这题居然不让人在线非要我把整个矩阵一起读进来……
代码(1078MS,可恶啊C++又比G++快一倍):
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- const int MAXN = ;
- const int MAXM = ;
- int wel[MAXN][MAXM], len[MAXN][MAXM];
- int sum_w[MAXM], sum_l[MAXM];
- int a[MAXM], b[MAXM], head, tail;
- int dp[][MAXM];
- int n, m, k, cur;
- inline void insert(int x, int y) {
- while(head != tail && a[tail - ] < x) --tail;
- a[tail] = x; b[tail] = y; ++tail;
- }
- void solve() {
- memset(dp, , sizeof(dp));
- cur = ;
- for(int i = ; i < n; ++i) {
- cur ^= ;
- memset(dp[cur], , sizeof(dp[cur]));
- sum_w[] = sum_l[] = ;
- for(int j = ; j <= m; ++j) sum_w[j] = sum_w[j - ] + wel[i][j];
- for(int j = ; j <= m; ++j) sum_l[j] = sum_l[j - ] + len[i][j];
- head = tail = ;
- for(int j = ; j <= m; ++j) {
- insert(dp[cur ^ ][j] - sum_w[j], sum_l[j]);
- while(k < sum_l[j] - b[head]) ++head;
- dp[cur][j] = max(dp[cur][j], a[head] + sum_w[j]);
- }
- sum_w[m] = sum_l[m] = ;
- for(int j = m; j > ; --j) sum_w[j - ] = sum_w[j] + wel[i][j];
- for(int j = m; j > ; --j) sum_l[j - ] = sum_l[j] + len[i][j];
- head = tail = ;
- for(int j = m; j >= ; --j) {
- insert(dp[cur ^ ][j] - sum_w[j], sum_l[j]);
- while(k < sum_l[j] - b[head]) ++head;
- dp[cur][j] = max(dp[cur][j], a[head] + sum_w[j]);
- }
- }
- }
- int main() {
- while(scanf("%d%d%d", &n, &m, &k) != EOF) {
- if(n == && m == && k == ) break;
- ++n;
- for(int i = ; i < n; ++i)
- for(int j = ; j <= m; ++j) scanf("%d", &wel[i][j]);
- for(int i = ; i < n; ++i)
- for(int j = ; j <= m; ++j) scanf("%d", &len[i][j]);
- solve();
- int ans = ;
- for(int i = ; i <= m; ++i) ans = max(ans, dp[cur][i]);
- printf("%d\n", ans);
- }
- }
HDU 2490 Parade(DPの单调队列)(2008 Asia Regional Beijing)的更多相关文章
- HDU 3401 Trade dp+单调队列优化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)Mem ...
- HDU - 3415(DP + 单调队列)
链接:HDU - 3415 题意:给出一个包含 n 个数的环,求满足长度大于 0 小于等于 k 的最大区间和. 题解:将数组加倍,形成环.求一个前缀和sum.枚举每一个sum[i],以 i 结尾的最大 ...
- HDU 5945 题解(DP)(单调队列)
题面: Fxx and game Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) T ...
- hdu 4123 树形DP+单调队列
http://acm.hust.edu.cn/vjudge/problem/25790 这题基本同poj 3162 要注意mx,mx2,vx,vx2每次都要初始化 #include <iostr ...
- HDU 4749 Parade Show 2013 ACM/ICPC Asia Regional Nanjing Online
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题目大意:给一个原序列N,再给出一个序列M,问从N中一共可以找出多少个长度为m的序列,序列中的数 ...
- HDU 2494/POJ 3930 Elevator(模拟)(2008 Asia Regional Beijing)
Description Too worrying about the house price bubble, poor Mike sold his house and rent an apartmen ...
- HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)
Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street ...
- HDU 2491 Priest John's Busiest Day(贪心)(2008 Asia Regional Beijing)
Description John is the only priest in his town. October 26th is the John's busiest day in a year be ...
- HDU 2489 Minimal Ratio Tree(暴力+最小生成树)(2008 Asia Regional Beijing)
Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated accord ...
随机推荐
- python 输入三个整数,按照从小到大的顺序打印
# # 3 输入三个整数,按照从小到大的顺序打印 a = int(input('请输入第一个整数:')) b = int(input('请输入第二个整数:')) c = int(input('请输入第 ...
- 【TOJ 3600】Fibonacci II (对数+斐波那契通项式)
描述 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列(f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i> ...
- 06.升级git版本及命令学习
博客为日常工作学习积累总结: 1.升级git版本: 参考博客:https://blog.csdn.net/yuexiahunone/article/details/78647565由于新的版本可以使用 ...
- es6 Proxy对象详解
Proxy用于修改某些操作的默认行为,也可以理解为在目标对象之前架设一层拦截,外部所有的访问都必须先通过这层拦截,因此提供了一种机制,可以对外部的访问进行过滤和修改.这个词的原理为代理,在这里可以表示 ...
- angular-sanitize 插件的使用,获取带html标签的内容
1,安装 angular-sanitize bower install angular-sanitize --save 引入到 html <script src="/bower_com ...
- itertools.groupby()分组字典列表
## itertools.groupby()分组字典列表数据 from operator import itemgetter from itertools import groupby student ...
- C/C++中的malloc、calloc和realloc
1. malloc 原型:extern void *malloc(unsigned int num_bytes); 头文件:Visual C++6.0中可以用malloc.h或者stdlib.h 功能 ...
- python note 001
.tilte() .upper() .lower() --- \n \t --- "apple"+" "+"pen" --- .strip( ...
- 慎用静态类static class
偶然翻到一篇有趣的帖子: class - When to Use Static Classes in C# - Stack Overflowhttp://stackoverflow.com/quest ...
- Quartus II 项目文件分类及内容