HDU 4374 One hundred layer DP的单调队列优化
Posted Meek
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 4374 One hundred layer DP的单调队列优化相关的知识,希望对你有一定的参考价值。
One hundred layer
Problem Description
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.
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.
Input
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)
Followed N lines, each line has M integers indicating the score. (-500<=score<=500)
Output
Output the highest score you can get.
Sample Input
3 3 2 1
7 8 1
4 5 6
1 2 3
Sample Output
29
题意:
给你n*m的图,起始位置在第一行的第x个位置,现在你可以选择一个方向至多走T个位置然后走向下一行,直到第n行
路过的格子里的值总和最大是多少
题解:
首先想到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[N][M],sum[N][M],dp[N][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[n][i]); printf("%d\n",ans); } return 0; }
以上是关于HDU 4374 One hundred layer DP的单调队列优化的主要内容,如果未能解决你的问题,请参考以下文章
I am (one) hundred percent sure.
cf1562D Two Hundred Twenty One
将进酒丨Three hundred cups but one gulp
CodeForces - 1562D2 Two Hundred Twenty One (hard version)(二分)
CodeForces - 1562D2 Two Hundred Twenty One (hard version)(二分)