HDU 4374 One hundred layer DP的单调队列优化
2016-05-24 23:06
405 查看
One hundred layer
[align=left][b]Problem Description[/b][/align][align=left] [/align]
Now there is a game called the new man down 100th floor. The rules of this game is:
1. At first you are at the 1st floor. And the floor moves up. Of course you can choose which part you will stay in the first time.
2. Every floor is divided into M parts. You can only walk in a direction (left or right). And you can jump to the next floor in any part, however if you are now in part “y”, you can only jump to part “y” in the next floor! (1<=y<=M)
3. There are jags on the ceils, so you can only move at most T parts each floor.
4. Each part has a score. And the score is the sum of the parts’ score sum you passed by.
Now we want to know after you get the 100th floor, what’s the highest score you can get.
[align=left][b]Input[/b][/align]
[align=left] [/align]
The first line of each case has four integer N, M, X, T(1<=N<=100, 1<=M<=10000, 1<=X, T<=M). N indicates the number of layers; M indicates the number of parts. At first you are in the X-th part. You can move at most T parts in every floor in only one direction.
Followed N lines, each line has M integers indicating the score. (-500<=score<=500)
[align=left][b]Output[/b][/align]
[align=left] [/align]
Output the highest score you can get.
[align=left][b]Sample Input[/b][/align]
[align=left] [/align]
3 3 2 1
7 8 1
4 5 6
1 2 3
[align=left][b]Sample Output[/b][/align]
[align=left] [/align]
29
[b]题意:[/b]
给你n*m的图,起始位置在第一行的第x个位置,现在你可以选择一个方向至多走T个位置然后走向下一行,直到第n行
路过的格子里的值总和最大是多少
[b]题解:[/b]
首先想到dp[i][j]表示到达当前(i,j)格子的最大答案,那么最后答案显然了
思考如何得到(i,j)的最优答案
他可以是从左边上一层走下来再向右走到j位置,且走过不超过T,也可以是右边
对于左边:dp[i][j] = dp[i-1][k] - sum[i][k-1] + sum[i][j];
dp[i-1][k] - sum[i][k-1]这一块是上一层,和当前层没有任何关系,我们可以预处理啊
那么我们维护一个大小T的单调队列,左边右边扫一波就好了
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #include<queue> using namespace std; const int N = 120, M = 1e4+20, mod = 1000000007,inf = 2e9; typedef long long ll; int n,m,x,t,a [M],sum [M],dp [M]; int main() { while(scanf("%d%d%d%d",&n,&m,&x,&t)!=EOF) { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&a[i][j]); for(int i=1;i<=n;i++) { sum[i][0] = 0; for(int j=1;j<=m;j++) sum[i][j] = sum[i][j-1] + a[i][j]; } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) dp[i][j] = -inf; for(int i=x;i>=1&&i>=x-t;i--) dp[1][i] = sum[1][x] - sum[1][i-1]; for(int i=x;i<=m&&i<=x+t;i++) dp[1][i] = sum[1][i] - sum[1][x-1]; deque<int >q; for(int i=2;i<=n;i++) { //从左向右 while(!q.empty()) q.pop_back(); dp[i][1] = dp[i-1][1] + a[i][1]; q.push_back(1); for(int j=2;j<=m;j++) { while(!q.empty()&&j-q.front()>t) q.pop_front(); int now = dp[i-1][j] - sum[i][j-1]; while(!q.empty()&&dp[i-1][q.back()]-sum[i][q.back()-1]<=now) q.pop_back(); q.push_back(j); int pos = q.front(); dp[i][j] = max(dp[i][j],dp[i-1][pos]-sum[i][pos-1]+sum[i][j]); } while(!q.empty()) q.pop_back(); q.push_back(m); dp[i][m] = max(dp[i][m],dp[i-1][m]+a[i][m]); for(int j=m-1;j>=1;j--) { while(!q.empty()&&q.front()-j>t) q.pop_front(); int now = dp[i-1][j] + sum[i][j]; while(!q.empty()&&dp[i-1][q.back()]+sum[i][q.back()]<=now) q.pop_back(); q.push_back(j); int pos = q.front(); dp[i][j] = max(dp[i][j],dp[i-1][pos]+sum[i][pos]-sum[i][j-1]); } } int ans = -inf; for(int i=1;i<=m;i++) ans = max(ans,dp [i]); printf("%d\n",ans); } return 0; }
相关文章推荐
- [转]G++与GCC的区别
- 【Squid中文权威指南】磁盘缓存基础
- python数据持久存储:pickle模块的使用
- 中国天气网城市代码
- Mac下配置Nodejs开发环境
- 数据库原理课后习题答案
- 开始
- 《辅助技能》程序命名规范
- 【48】java抽象类和接口的定义和区别
- 【48】java抽象类和接口的定义和区别
- Keepalived+Nginx实现双主高可用负载均衡
- 【48】java抽象类和接口的定义和区别
- 排序算法之直接插入排序
- java37java中的线程(二)
- 学习日记4
- 设置thunderbird用">"引用原邮件
- javaweb项目的包的命名
- Class.forName的作用以及为什么要用它
- 第八周项目:用对象数组操作长方柱类
- code force 之 Money transfer