C. Watching Fireworks is Fun(Codeforces 372C)
阅读原文时间:2023年07月09日阅读:2

C. Watching Fireworks is Fun

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 t__i at section a__i. If you are at section x (1 ≤ x ≤ n) at the time of i-th launching, you'll gain happiness value b__i - |a__i - 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 a__i, b__i, t__i (1 ≤ a__i ≤ n; 1 ≤ b__i ≤ 109; 1 ≤ t__i ≤ 109). The i-th line contains description of the i-th launching.

It is guaranteed that the condition t__i ≤ t__i + 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
思路:dp+单调队列;
dp[i][j]为到放第i个烟花的时候站在j的位置可以获得的最大happiness。转移方程:dp[ i ] [ j ] =max(dp[ i - 1] [ k ]) + b[ i ]  - | a[ i ] - j | ,其中  max(1,j-t*d)<=k<=min(n,j+t*d)
那么烟花燃放时间要先按照顺序排,我们可以看到k的区间是个恒定的长度,那么很容一就想到用单调队列去维护一个递减的队列,然后每次更新dp;
要开个滚动数组。复杂度O(n*m);

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 using namespace std;
10 typedef long long LL;
11 LL dp[2][150005];
12 typedef struct node
13 {
14 LL a,b,t;
15 } ss;
16 ss ans[400];
17 bool cmp(node p, node q)
18 {
19 return p.trail)
49 {
50 quq[++rail] = j;
51 }
52 else
53 {
54 while(true)
55 {
56 int id = quq[rail];
57 if(dp[(i+1)%2][id] <= dp[(i+1)%2][j]) 58 { 59 rail--; 60 } 61 else break; 62 if(rail < head) 63 { 64 break; 65 } 66 } 67 quq[++rail] = j; 68 } 69 } 70 for(j = 1; j <= n; j++) 71 { 72 LL xx = max((LL)1,(j-d*(ans[i].t-ans[i-1].t))); 73 LL yy = min((LL)n,(maxx+j)); 74 { 75 if(railrail)break;
100 }
101 dp[i%2][j] = max(dp[i%2][j],dp[(i+1)%2][quq[head]]+ans[i].b-abs(ans[i].a-j));
102 }
103 }
104 }
105 for(i = 1; i <= n; i++)
106 {
107 maxa = max(maxa,dp[m%2][i]);
108 }
109 printf("%lld\n",maxa);
110 }
111 return 0;}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章