Parade(单调队列优化dp)

时间:2021-07-26 07:59:13

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490

Parade

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 902    Accepted Submission(s): 396

Problem Description
Panagola,
The Lord of city F likes to parade very much. He always inspects his
city in his car and enjoys the welcome of his citizens. City F has a
regular road system. It looks like a matrix with n+1 west-east roads and
m+1 north-south roads. Of course, there are (n+1)×(m+1) road crosses in
that system. The parade can start at any cross in the southernmost road
and end at any cross in the northernmost road. Panagola will never
travel from north to south or pass a cross more than once. Citizens will
see Panagola along the sides of every west-east road. People who love
Panagola will give him a warm welcome and those who hate him will throw
eggs and tomatoes instead. We call a road segment connecting two
adjacent crosses in a west-east road a “love-hate zone”. Obviously
there are m love-hate zones in every west-east road. When passing a
love-hate zone, Panagola may get happier or less happy, depending on how
many people love him or hate him in that zone. So we can give every
love-hate zone a “welcome value” which may be negative, zero or
positive. As his secretary, you must make Panagola as happy as possible.
So you have to find out the best route ----- of which the sum of the
welcome values is maximal. You decide where to start the parade and
where to end it.

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.

Parade(单调队列优化dp)

 
Input
There are multiple test cases. Input ends with a line containing three zeros.
Each test case consists of 2×n + 3 lines.

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
For each test case, output the sum of welcome values of the best route. The answer can be fit in a 32 bits integer.
 
Sample Input
2 3 2
7 8 1
4 5 6
1 2 3
1 1 1
1 1 1
1 1 1
0 0 0
 
Sample Output
27
 
Source
 题意: F城是由n+1条横向路和m+1条竖向路组成。你的任务是从最南边的路走到最北边的路,使得走过的路上的高兴值和最大(注意,一段路上的高兴值可能是负 数)。同一段路不能经过两次,且不能从北往南走,另外,在每条横向路上所花的时间不能超过k。求从南到北走完可以获得的最大高兴值。
题解: 学习dp 任何一个状态可以是由下一行的-k<t<k 的状态转移过来,所以是一个dp问题,现在先预处理出每一行到当前位置的前缀和用sum[i][j]保存下来,
dp[i][j]表示到第i行第j列的时候最大的高兴值,这个时候下一行的dp[i+1][j]已经处理出来了
假设当前位置是由下一行的第k个格子转移来的,当k<=j 的时候有 dp[i][j] = dp[i+1][k] + sum[i+1][j] - sum[i+1][k] ——(1式)
                       当k>  j 的时候有 dp[i][j] = dp[i+1][k] + sum[i+1][k] - sum[i+1][j] ——(2式)
所以有dp[i][j] = max(dp[i][j] , (1式) ,(2式) ) ;
考虑每次枚举k的话,复杂度是n*m*m 肯定会超时,所以像一个不用枚举k 的方法——单调队列优化dp
根据转移方程1 dp[i+1][k] - sum[i+1][k] 和k 有关,则设F(K) = dp[i+1][k] - sum[i+1][k]  这样只要在向上转移的时候,只要在转移范围之内,并且维护下一行的F(k) 是一个单调队列,及永远是最大值在最后面,
现在的问题就是要预处理出每个点的转移范围,即对每一行的第i个位置的 t 相加,从0加到t,如果大于k则从1开始逐个递减,这样从前向后扫描一边,因为t肯定是正数所以扫描一遍即可预处理所有的左边界,
再从后向前扫描一边即可预处理出所有的右边界,然后用一个优先队列储存使得f(k)在转移范围内的从小到大的k值,则最后肯定是从这个k值转移到此状态是大的高兴值
 
这里注意一个小细节,就是一共有n+1行
 
 代码:
 #include<cstdio>
#include<algorithm>
using namespace std;
#define N 104
#define M 10004 int L[N][M],R[N][M];
int v[N][M],t[N][M];
int n , m , k ;
int Q[M];
int f[M],sum[N][M],dp[N][M];
int main()
{
while(~scanf("%d %d %d",&n , &m , &k),n||m||k)
{
for(int i = ; i <= n+ ;i++)
for(int j = ; j < m ;j++)
scanf("%d",&v[i][j]);
for(int i = ; i <= n+ ; i++)
for(int j = ; j < m ;j++)
scanf("%d",&t[i][j]);
for(int i = ; i <= n+ ; i++)
{
sum[i][] = ;
for(int j = ; j <= m ;j++)
sum[i][j] = sum[i][j-] + v[i][j-];
}
for(int i = ; i <= n+ ; i++)
{
L[i][] = ;R[i][m] = m;
int cur = , id = ;
for(int j = ; j <= m ;j++){
cur+=t[i][j-];
while(cur>k) cur-=t[i][id++];
L[i][j] = id;
}
cur = ; id = m-;
for(int j = m- ; j>= ; j--){
cur+=t[i][j];
while(cur>k) cur -= t[i][id--];
R[i][j] = id+;
}
}
for(int i = ; i < m+ ; i++) dp[n+][i] = ;
for(int i = n ; i >= ; i--)
{
int head = , rear = ;
for(int j = ; j < m+ ; j++)
{
f[j] = dp[i+][j] - sum[i+][j];
while(rear < head && Q[rear] < L[i+][j]) rear++;
while(head > rear && f[j] >=f[Q[head-]]) head--;
Q[head++] = j;
dp[i][j] = max(dp[i+][j],sum[i+][j]+f[Q[rear]]);
}
head = , rear = ;
for(int j = m ; j>= ; j--)
{
f[j] = dp[i+][j] + sum[i+][j];
while(rear<head&&Q[rear]>R[i+][j]) rear++;
while(head>rear&&f[j]>=f[Q[head-]]) head--;
Q[head++] = j;
dp[i][j] = max(dp[i][j],f[Q[rear]]-sum[i+][j]);
}
}
int ans = ;
for(int i = ; i < m+ ;i++) ans = max(ans,dp[][i]);
printf("%d\n",ans);
}
return ;
}