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 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.

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<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<deque>
7 #include<stack>
8 #include<math.h>
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.t<q.t;
20 }
21 int quq[150005*10];
22 int main(void)
23 {
24 int n,m,d;
25 int i,j;
26 while(scanf("%d %d %d",&n,&m,&d)!=EOF)
27 {
28 for(i = 1; i <= m; i++)
29 {
30 scanf("%lld %lld %lld",&ans[i].a,&ans[i].b,&ans[i].t);
31 }
32 sort(ans+1,ans+1+m,cmp);
33 for(i = 0; i <= 150000; i++)
34 {
35 dp[0][i] = 0;
36 }
37 ans[0].t = 0;
38 LL maxa = -1e16;
39 for(i = 1; i <= m; i++)
40 {
41 for(j = 0; j <=n; j++)dp[i%2][j] = -1e16;
42 int head = 150001;
43 int rail = 150000;
44 LL minn = 1;
45 LL maxx = min((d*(ans[i].t-ans[i-1].t)),(LL)n);
46 for(j = minn; j <= maxx; j++)
47 {
48 if(head>rail)
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(rail<head)
76 {
77 quq[++rail] = yy;
78 }
79 else
80 {
81 while(true)
82 {
83 int id = quq[rail];
84 if(dp[(i+1)%2][id] <= dp[(i+1)%2][yy])
85 {
86 rail--;
87 }
88 else break;
89 if(rail < head)
90 {
91 break;
92 }
93 }
94 quq[++rail] = yy;
95 }
96 while(quq[head] < xx)
97 {
98 head++;
99 if(head>rail)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;}

最新文章

  1. WPF数据编辑的提交与撤销
  2. Linux 环境变量PS1设置
  3. WireShark系列: 使用WireShark过滤条件抓取特定数据流(zz)
  4. 一段功能齐全的PHP常用重定向代码html+js+header
  5. GemFire 入门篇1:GemFire 是什么?
  6. 改变DEV控件的字体 z
  7. 细雨学习笔记:Jmeter测试计划最基本的元素
  8. USACO 2013 November Contest Gold 简要题解
  9. 【Linux常用工具】02. 创建启动定时任务工具cron
  10. 一步步学算法(算法分析)---6(Floyd算法)
  11. python爬虫requests 下载图片
  12. [IR] Advanced XML Compression - ISX
  13. WCF开发实战系列二:使用IIS发布WCF服务 转
  14. 118/119. Pascal&#39;s Triangle/II
  15. python3.4学习笔记(二十五) Python 调用mysql redis实例代码
  16. Loitor_产品(二)校准立体摄像机
  17. 【洛谷】4917:天守阁的地板【欧拉函数的应用】【lcm与gcd】【同除根号优化】
  18. spring_restful_json_jdbc
  19. ACM java写法入门
  20. 无法链接到windows服务

热门文章

  1. EXCEL如何用公式提取一列中的唯一值和不重复值
  2. DOM给表格添加新一行和删除整个行的内容
  3. Vue相关,Vue JSX
  4. Kotlin 学习(1)
  5. Android获取通知栏的高度
  6. 3.0 rust 项目路径
  7. 剖析虚幻渲染体系(13)- RHI补充篇:现代图形API之奥义与指南
  8. LVS配置记录
  9. Boss直聘App上“天使投资、VC、PE” 与“A轮、B轮、C轮融资”的关系
  10. set env export区别