Parade(单调队列优化dp)

题目连接: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.

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

2008 Asia Regional Beijing

题意: 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行

代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 #define N 104
 5 #define M 10004
 6
 7 int L[N][M],R[N][M];
 8 int v[N][M],t[N][M];
 9 int n , m , k ;
10 int Q[M];
11 int f[M],sum[N][M],dp[N][M];
12 int main()
13 {
14     while(~scanf("%d %d %d",&n , &m , &k),n||m||k)
15     {
16         for(int i = 1 ; i <= n+1 ;i++)
17             for(int j = 0 ; j < m ;j++)
18                 scanf("%d",&v[i][j]);
19         for(int i = 1; i <= n+1 ; i++)
20             for(int j = 0 ; j < m ;j++)
21                 scanf("%d",&t[i][j]);
22         for(int i = 1 ; i <= n+1 ; i++)
23         {
24             sum[i][0] = 0;
25             for(int j = 1 ; j <= m ;j++)
26                 sum[i][j] = sum[i][j-1] + v[i][j-1];
27         }
28         for(int i = 1 ; i <= n+1 ; i++)
29         {
30             L[i][0] = 0;R[i][m] = m;
31             int cur = 0 , id = 0;
32             for(int j = 1 ; j <= m ;j++){
33                 cur+=t[i][j-1];
34                 while(cur>k) cur-=t[i][id++];
35                 L[i][j] = id;
36             }
37             cur = 0 ; id = m-1;
38             for(int j = m-1 ; j>=0 ; j--){
39                 cur+=t[i][j];
40                 while(cur>k) cur -= t[i][id--];
41                 R[i][j] = id+1;
42             }
43         }
44         for(int i = 0 ; i < m+1 ; i++) dp[n+1][i] = 0;
45         for(int i = n ; i >=0 ; i--)
46         {
47             int head = 0, rear = 0;
48             for(int j = 0 ; j < m+1 ; j++)
49             {
50                 f[j] = dp[i+1][j] - sum[i+1][j];
51                 while(rear < head && Q[rear] < L[i+1][j]) rear++;
52                 while(head > rear && f[j] >=f[Q[head-1]]) head--;
53                 Q[head++] = j;
54                 dp[i][j] = max(dp[i+1][j],sum[i+1][j]+f[Q[rear]]);
55             }
56             head = 0 , rear = 0;
57             for(int j = m ; j>= 0 ; j--)
58             {
59                 f[j] = dp[i+1][j] + sum[i+1][j];
60                 while(rear<head&&Q[rear]>R[i+1][j]) rear++;
61                 while(head>rear&&f[j]>=f[Q[head-1]]) head--;
62                 Q[head++] = j;
63                 dp[i][j] = max(dp[i][j],f[Q[rear]]-sum[i+1][j]);
64             }
65         }
66         int ans = 0;
67         for(int i = 0 ; i < m+1 ;i++) ans = max(ans,dp[0][i]);
68         printf("%d\n",ans);
69     }
70     return 0;
71 }
时间: 2024-12-06 11:05:17

Parade(单调队列优化dp)的相关文章

POJ 3926 Parade 单调队列优化DP

来源:http://poj.org/problem?id=3926 题意:行n <= 100, 列m <= 10000,类似于数字三角形,一个人要从底下往上走,每层中可以左右走,但选定方向不能回头(向左不能再向右),每经过一段获得该段的一个值,并走了该段的距离,在同一层走的距离不能超过k.问走到最顶头,获得的总值最大是多少. 分析:dp[i][j]表示走到第i行第j列,获得的值最大为多少.则dp[i][j] = max(dp[i+1][p] + sum(p to j)),sum(p to j)

HDU 4122 Alice&#39;s mooncake shop 单调队列优化dp

Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4122 Description The Mid-Autumn Festival, also known as the Moon Festival or Zhongqiu Festival is a popular harvest festival celebrated by Ch

Tyvj1305最大子序和(单调队列优化dp)

描述 输入一个长度为n的整数序列,从中找出一段不超过M的连续子序列,使得整个序列的和最大. 例如 1,-3,5,1,-2,3 当m=4时,S=5+1-2+3=7当m=2或m=3时,S=5+1=6 输入格式 第一行两个数n,m第二行有n个数,要求在n个数找到最大子序和 输出格式 一个数,数出他们的最大子序和 测试样例1 输入 6 4 1 -3 5 1 -2 3 输出 7 备注 数据范围:100%满足n,m<=300000 是不超过m,不是选m个!!!!! /* 单调队列优化dp 单调队列维护的是前

bzoj1855: [Scoi2010]股票交易--单调队列优化DP

单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w-1][k]+k*Ap[i]的单调性即可 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 const int maxn = 2010; 6 int

1855: [Scoi2010]股票交易[单调队列优化DP]

1855: [Scoi2010]股票交易 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1083  Solved: 519[Submit][Status][Discuss] Description 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股票的走势,第i天的股票买入价为每股APi,第i天的股票卖出价为每股BPi(数据保证对于每个i,都有APi>=

BZOJ 1855 股票交易(单调队列优化DP)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1855 题意:最近lxhgww又迷上了投资股票, 通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股票的走势,第i天的股票买入价为每股APi,第i天的股票卖出价为每股BPi(数据保证对于每 个i,都有APi>=BPi),但是每天不能无限制地交易,于是股票交易所规定第i天的一次买入至多只能购买ASi股,一次卖出至多只能卖出BS

【单调队列优化dp】uestc 594 我要长高

http://acm.uestc.edu.cn/#/problem/show/594 [AC] 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=5e4+2; 5 const int inf=0x3f3f3f3f; 6 int n,c; 7 int cur; 8 int dp[2][maxn]; 9 int q[maxn]; 10 int main() 11 { 1

洛谷P1725 琪露诺 单调队列优化 DP

洛谷P1725 琪露诺 单调队列优化 DP 题意:1--n 每个点都有一个权值,从当前点i可以到达i+l--i+r 之间的点, 动态规划 方程 为 f[ i ] = max(f[ i ],f[ k ] ) +a[ i ] i-r<=k<=i-l 然而这样复杂度 就为 n^2 因为相当于 dp 是在求 一段区间的最大值,而这个最大值就可以用O(n) 来维护 注意 这个O(n) 是均摊O(n) 即将所有固定区间长度的 最大值求出来 是 O(n)的这样就把复杂度降到 O(n) 级别了 1 #incl

bzoj1233: [Usaco2009Open]干草堆tower 单调队列优化dp

又是一道单调队列优化dp的题目 这道题呢 先要了解一个结论,在多种可行的堆叠方案中,至少有一种能使层数最高的方案同时使得底边最短.即底边最短的,层数一定最高. 这个是zkw大神得出的 我也不会证明来着 反正这样之后我们就可以得出正确的方法了 递推式 F[i]=min(sum[j-1]-sum[i-1])  j>i 且 sum[j-1]-sum[i-1]>=F[j] 易得在满足的条件下j当然是越小越好了嘛 而这样得出的方程又满足一定的单调性 这样调整之后队首就是我们要的答案啦 又对于转移条件 f