关于单调队列优化DP的讲解:
单调队列优化DP(超详细!!!) - endl\n - 博客园
https://blog.csdn.net/qq_32460819/article/details/113749350
Watching Fireworks is Fun
A festival will be held in a town's main street. There are n sections in the main street. The sections are numbered 1 through n from left to right. The distance between each adjacent sections is 1.
In the festival m fireworks will be launched. The i-th (1 ≤ i ≤ m) launching is on time ti at section ai. If you are at section x (1 ≤ x ≤ n) at the time of i-th launching, you'll gain happiness value bi - |ai - x| (note that the happiness value might be a negative value).
You can move up to d length units in a unit time interval, but it's prohibited to go out of the main street. Also you can be in an arbitrary section at initial time moment (time equals to 1), and want to maximize the sum of happiness that can be gained from watching fireworks. Find the maximum total happiness.
Note that two or more fireworks can be launched at the same time.
Input
The first line contains three integers n, m, d (1 ≤ n ≤ 150000; 1 ≤ m ≤ 300; 1 ≤ d ≤ n).
Each of the next m lines contains integers ai, bi, ti (1 ≤ ai ≤ n; 1 ≤ bi ≤ 1e9; 1 ≤ ti ≤ 1e9). The i-th line contains description of the i-th launching.
It is guaranteed that the condition ti ≤ ti + 1 (1 ≤ i < m) will be satisfied.
Output
Print a single integer — the maximum sum of happiness that you can gain from watching all the fireworks.
Please, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
50 3 1 49 1 1 26 1 4 6 1 10
Output
-31
Input
10 2 1 1 1000 4 9 1000 4
Output
1992
题目大意:
-
一个节日将在一个镇的主街道上举行。 街道被分为nn段,从左到右编号为11到nn,每个相邻部分之间的距离为11。
-
节日里在主街道有 mm 个烟花要放,第i(1≤i≤m)i(1≤i≤m)次烟花将在 titi 时在 aiai 段燃放。 如果您在第 ii 次发射时处于 x(1≤x≤n)x(1≤x≤n) ,您将获得幸福值 bi−|ai−x|bi−|ai−x| (请注意,幸福价值可能是负值)
-
您可以以单位时间间隔移动长度为 dd 个长度单位,但禁止离开主要街道。 此外,在初始时刻(时间等于 11 )你可以在街道的任意部分,并希望最大化从观看烟花获得的幸福总和。 找到最大的总幸福。
-
请注意,两个或多个烟花可以同时发射
题目出处:CF372C
如果 i 时位置确定,在 xi ,则到下一个燃放时刻时最远能一定distance=(−)∗d,则位置可能在[xi−distance,xi+distance]。
所以该次燃放后可能获得的幸福总和由上一次位于[j−distance,j+distance]处的2h+1种情形得到。
假设:dp[i][j],表示第 i 次烟花燃放时位于 j 时所能获得的最大的幸福总和。//这里的dp含义和题解代码中dp的含义不同,不要弄混了
动态转移方程:dp[i][j]=max(dp[i−1][k])+b[i]−|a[i]−j|,k∈[j−h,j+h]。
第 i 次燃放时处于 j(1≤j≤n),那第 i−1 次燃放时必须能到达 j ,所以 j−distance≤k≤j+distance。当前状态至于上一行的某个区间的最值有关,我们很快就能联想起用单调队列进行优化。当前行状态至于上一行的状态相关,果断滚动数组压维。时间效率:O(m∗n)。
滚动数组,就是用来节省储存空间的,因为要求的是所有烟花放完后的最大happiness所以中间的过程结果没必要保存,算第i个烟花的结果的时候只需要第i-1个烟花的结果,而再之前的结果无需保存,那么只需要一个两行maxn列的数组就行,代码中的k是用来记录当前行的计数值,只会在0和1之间变化
这里也有一些贪心在里面,在求每次当前烟花的i区域快乐值的时候,选择[i-distance,i+distance]中的最大值。
这个题队列是一个单进双出的,从front到rear单减队列,用来维护max(dp[i−1][k])
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+10;
ll dp[2][maxn],n,m,d,a,b,nt,pt=1,dis,front,rear,ans,que[maxn],i,j;
int k;
int main(){
scanf("%lld%lld%lld",&n,&m,&d);
while(m--){
scanf("%lld%lld%lld",&a,&b,&nt);
dis=min(n,(nt-pt)*d);//从上一个时刻到这个烟花燃放时刻最远能走多远注意不能超过n
pt=nt;//为下一个时刻做准备
k=!k;//pre_t 记录上一个烟花的时刻,k滚动数组标记,k当前行,!k上一行
front=rear=1;
for(i=1,j=1;i<=n;i++){//i枚举当前燃放时所处的位置,j表示上一个烟花时可能的位置
for(;j<=i+dis&&j<=n;j++)//注意j在上面定义的,即当i改变位置时,j没有重新赋值,所以这两个for嵌套不是n^2复杂度,而是n复杂度
{
while(front<rear&&dp[!k][j]>=dp[!k][que[rear-1]])当上一行j位置的dp值比队尾大时显然,队尾就没用了,这里写>或者>=都可以
rear--;
que[rear++]=j;
}
while(front<rear&&que[front]<i-dis)///保证左边范围,这里 front<rear的判断去掉也可以
front++;
dp[k][i]=dp[!k][que[front]]+b-llabs(a-i);
}
}
ans=dp[k][1];//注意这里要先赋初始值,否则ans=0导致负数结果的样例WA
for(ll i=2;i<=n;i++)
ans=max(ans,dp[k][i]);
printf("%lld\n",ans);
return 0;
}
2021.7.31