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;}

C. Watching Fireworks is Fun(Codeforces 372C)的更多相关文章

  1. Codeforces Round #219 (Div. 2) E. Watching Fireworks is Fun

    http://codeforces.com/contest/373/problem/E E. Watching Fireworks is Fun time limit per test 4 secon ...

  2. Codeforces Round #219 (Div. 1) C. Watching Fireworks is Fun

    C. Watching Fireworks is Fun time limit per test 4 seconds memory limit per test 256 megabytes input ...

  3. CF372C Watching Fireworks is Fun(单调队列优化DP)

    A festival will be held in a town's main street. There are n sections in the main street. The sectio ...

  4. F - Watching Fireworks is Fun

    C. Watching Fireworks is Fun 题目大意: 一个城镇有n个区域,从左到右1-n,每个区域之间距离1个单位距离.节日中有m个烟火要放,给定放的地点a[ i ].时间t[ i ] ...

  5. 单调队列+线性dp题Watching Fireworks is Fun (CF372C)

    一.Watching Fireworks is Fun(紫题) 题目:一个城镇有n个区域,从左到右1编号为n,每个区域之间距离1个单位距离节日中有m个烟火要放,给定放的地点ai,时间ti当时你在x,那 ...

  6. 【简洁易懂】CF372C Watching Fireworks is Fun dp + 单调队列优化 dp优化 ACM codeforces

    题目大意 一条街道有$n$个区域. 从左到右编号为$1$到$n$. 相邻区域之间的距离为$1$. 在节日期间,有$m$次烟花要燃放. 第$i$次烟花燃放区域为$a_i$ ,幸福属性为$b_i$,时间为 ...

  7. 【单调队列优化】[CF372C] Watching Fireworks is Fun

    突然发现我可能单调队列都打不来了...我太菜了... 这道题显然有$$f[i][j]=min\{f[i-1][k]+\vert j-a[i] \vert\}$$ 则$ans=\sum_{i=1}^{m ...

  8. 题解-------CF372C Watching Fireworks is Fun

    传送门 一道有趣的DP 题目大意 城镇中有$n$个位置,有$m$个烟花要放.第$i$个烟花放出的时间记为$t_{i}$,放出的位置记为$a_{i}$.如果烟花放出的时候,你处在位置$x$,那么你将收获 ...

  9. DP刷题记录

    目录 dp刷题记录 codeforces 706C codeforces 940E BZOJ3997 POJ2279 GYM102082B GYM102082D codeforces132C L3-0 ...

随机推荐

  1. Identity Server 4 从入门到落地(四)—— 创建Web Api

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  2. Applescript快速入门及OmniFocus每日md报告开发

    本篇主要记录 Applescript 基础语法,以及利用 applescript 生成 omnifocus 每日报告 从 windows 转换到 macos,最近一直在不断折腾,这两天浏览 githu ...

  3. Hadoop【MR开发规范、序列化】

    Hadoop[MR开发规范.序列化] 目录 Hadoop[MR开发规范.序列化] 一.MapReduce编程规范 1.Mapper阶段 2.Reducer阶段 3.Driver阶段 二.WordCou ...

  4. jvm的优化

    a) 设置参数,设置jvm的最大内存数 b) 垃圾回收器的选择

  5. ubuntu基础

    下载地址: http://cdimage.ubuntu.com/releases/ #:配置多网卡静态IP地址和路由 root@ubuntu:~# vim /etc/netplan/01-netcfg ...

  6. VueAPI 2 (生命周期钩子函数)

    所有的生命周期钩子自动绑定 this 上下文到实例中,因此你可以访问数据,对属性和方法进行运算.这意味着你不能使用箭头函数来定义一个生命周期方法. beforeCreate 在实例初始化之后,此时还不 ...

  7. binlog浅析

    binlog浅析 一.基础知识 什么是binlog? (图一) 全称:Binary Log (二进制日志),包含描述数据库更改的" 事件 ",例如表创建操作或对表数据的更改.二进制 ...

  8. Linkerd Service Mesh 授权策略(Server & ServerAuthorization)

    简介 Server 和 ServerAuthorization 是 Linkerd 中的两种策略资源, 用于控制对 mesh 应用程序的入站访问. 在 linkerd 安装期间,policyContr ...

  9. Linux目录终章,单用户模式修改密码、环境变量、第三方软件安装

    目录 今日内容概要 内容详细 解析映射文件 磁盘挂载文件 开机加载脚本 系统启动级别 使用单用户模式修改密码 变量加载文件 登录提示信息 第三方软件安装目录(编译安装目录) 系统日志目录 保存系统运行 ...

  10. 降低制作门槛,人人都是3D“模”术师

    12月14日,HDD(Huawei Developer Day)深圳站圆满举办.国内3D扫描类开发团队看书击水为大家分享了与HMS Core 3D建模服务的合作之旅,讲述了如何通过3D物体建模能力为其 ...