「题解」Watching Fireworks is Fun

单调队列优化DP转移……

一 题目

Source

Descriptions

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 ≤ 109; 1 ≤ ti ≤ 109). 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.


二 题解

单调队列优化DP。从O(n^2m)变成O(nm)。

因为转移方程里面有个固定长度区间的$\max$或$\min$,这个是可以滑动窗口的。emm细节懒得讲了。

贴代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;

#define LL long long

const int CN = 2e5+5;
const int CM = 310;

const LL INF = 1e18;

LL read(){
LL s=0,ne=1; char c = getchar();
for(;c<'0'||c>'9';c=getchar()) if(c=='-') ne=-1;
for(;c>='0'&&c<='9';c=getchar()) s=(s<<1)+(s<<3)+c-'0';
return s*ne;
}

int n,m,d;
int a[CM],t[CM]; LL b[CM];

LL f[2][CN]; int p = 0;

LL abs(LL x){return x>0 ? x:-x;}

int main()
{
//memset(f,-0x7f,sizeof(f));

n = read(); m = read(); d = read();
for(int i=1;i<=m;i++)
a[i] = read(),b[i] = read(),t[i] = read();

for(int i=1;i<=n;i++) f[p][i] = b[1]-abs(a[1]-i); //初始化
for(int i=2;i<=m;i++){ //枚举烟花
p ^= 1;
LL MMD = (LL)(t[i]-t[i-1])*d; //MaxMoveDist

//单调队列
int l = 1,r = 0,Q[CN];
for(int j=1;j<=n;j++){ //正着扫一遍 j-k<=MMD
while(l<=r && j-Q[l]>MMD) l++; //过时的出队
while(l<=r && f[p^1][j]>f[p^1][Q[r]]) r--; //保持单减
Q[++r] = j; //入队

f[p][j] = f[p^1][Q[l]]+b[i]-abs(a[i]-j); //第一遍要强制更新状态
}
l = 1; r = 0;
for(int j=n;j;j--){ //倒着扫一遍 k-j<=MMD
while(l<=r && Q[l]-j>MMD) l++;
while(l<=r && f[p^1][j]>f[p^1][Q[r]]) r--;
Q[++r] = j;

f[p][j] = max(f[p][j], f[p^1][Q[l]]+b[i]-abs(a[i]-j));
}
}

LL ans = -INF;
for(int i=1;i<=n;i++){
ans = max(ans, f[p][i]);
//if(ans == 1) cout<<i<<endl;
}

printf("%lld",ans);

return 0;
}

另一个例子

LG-P2627

可以结合着一下理解MQ优化DP到底应该怎么写。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;

#define LL long long

const int CN = 1e5+5;

int n,k;
LL e,f[CN],sum[CN];

int Q[CN],l = 0,r = 0; //存下标
/*
r = l = 0的原因是默认队列里面有一个初始元素 0
*/
LL d[CN]; //存值
/*
d[i] = f[i-1]-sum[i]
源于方程 f[i] = max(f[j-1]+sum[i]-sum[j]) ,位置 j 不选
变形 f[i] = max(f[j-1]-sum[j]) + sum[i] ,位置 j 不选
max()里面的用MQ维护 当扫描到 i 时,在队列中添加 d[i] = f[i-1]-sum[i] ,位置 i 不选
在队列中查询出位置 j 转移时,位置 j 不选
*/
void PushBack(int i){
d[i] = f[i-1]-sum[i];
while(l<=r && d[Q[r]]<d[i]) r--;
Q[++r] = i;
}
LL QueryMax(int i){
while(l<r && Q[l]<i-k) l++;
return d[Q[l]];
}

int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%lld",&e),sum[i] = sum[i-1]+e;
for(int i=1;i<=n;i++){
PushBack(i);
f[i] = QueryMax(i)+sum[i];
}
printf("%lld",f[n]);
return 0;
}

作者

ce-amtic

发布于

2019-08-04

更新于

2021-01-04

许可协议

CC BY-NC-SA 4.0

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×