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 nmd (1 ≤ n ≤ 150000; 1 ≤ m ≤ 300; 1 ≤ d ≤ n).

Each of the next m lines contains integers aibiti (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

题解:

dp[i][j]表示当前放到了第I支烟花并且放这支烟花的时候他站在j点看。推出状态转移方程:

dp[ i ] [ j ] =max(dp[ i - 1] [ k ]) + b[ i ] - | a[ i ] - j | ,其中  max(1,j-t*d)<=k<=min(n,j+t*d)

不过每当我选取到 i 个烟花的时候,会先把所有能到他的点都放进单调队列中,那么最优解其实被我们存储了 只要那个最优解和第 i 个烟花的位置满足要求就选他,如果不满足 head++ 直到满足要求

#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pil pair<int,ll>
const ll maxn=1e5+5e4;
using namespace std;
ll n,m,d;
struct node{
ll a,b,x;
}p[];
ll dp[][maxn],q[maxn];
ll cmp(node n1,node n2){return n1.x<n2.x;}
int main()
{
ll time;
scanf("%lld%lld%lld",&n,&m,&d);
for(ll i=;i<=m;i++)
scanf("%lld%lld%lld",&p[i].a,&p[i].b,&p[i].x);
sort(p+,p++m,cmp);
memset(dp,,sizeof(dp));
time=p[].x;
ll mm=;
for(ll i=;i<=m;i++)
{
ll l=,r=,k=;
if(time==p[i].x)
{
for(ll j=;j<=n;j++)
dp[mm][j]=dp[-mm][j]+p[i].b-abs(p[i].a-j);
}
else
{
ll t=p[i].x-time;
time=p[i].x;
for(ll j=;j<=n;j++)
{
while(k<=n&&k<=j+d*t)
{
while(l<r&&dp[-mm][k]>=dp[-mm][q[r-]]) r--;
q[r++]=k++;
}
while(l<r&&j-t*d>q[l]) l++;
ll temp=p[i].b-abs(p[i].a-j);
dp[mm][j]=dp[-mm][q[l]]+temp;
}
}
mm=-mm;
}
ll ans=-1e17;
for(ll i=;i<=n;i++) ans=max(ans,dp[-mm][i]);
printf("%lld\n",ans);
return ;
}

CF372C Watching Fireworks is Fun(单调队列优化DP)的更多相关文章

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

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

  2. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  3. bzoj1855: [Scoi2010]股票交易--单调队列优化DP

    单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w ...

  4. hdu3401:单调队列优化dp

    第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...

  5. Parade(单调队列优化dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others)    ...

  6. BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP

    BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP Description 有一排n棵树,第i棵树的高度是Di. MHY要从第一棵树到第n棵树去找他的妹子玩. 如果MHY在 ...

  7. 【单调队列优化dp】 分组

    [单调队列优化dp] 分组 >>>>题目 [题目] 给定一行n个非负整数,现在你可以选择其中若干个数,但不能有连续k个数被选择.你的任务是使得选出的数字的和最大 [输入格式] ...

  8. [小明打联盟][斜率/单调队列 优化dp][背包]

    链接:https://ac.nowcoder.com/acm/problem/14553来源:牛客网 题目描述 小明很喜欢打游戏,现在已知一个新英雄即将推出,他同样拥有四个技能,其中三个小技能的释放时 ...

  9. 单调队列以及单调队列优化DP

    单调队列定义: 其实单调队列就是一种队列内的元素有单调性的队列,因为其单调性所以经常会被用来维护区间最值或者降低DP的维数已达到降维来减少空间及时间的目的. 单调队列的一般应用: 1.维护区间最值 2 ...

随机推荐

  1. PHP根据ip获取地理位置(通过高德地图接口)

    PHP根据ip获取地理位置(通过高德地图接口)<pre>//restapi.amap.com/v3/ip?key=2004f145cf3a39a72e9ca70ca4b2a1dc& ...

  2. JavaScrip 基础

    JavaScript 基础 前段的三剑客之一JS,来来来,看看它是什么鬼!到底如何让网页动起来的呢,今天就搞他一下. 一.JavaScript的简单介绍 javascript是一门动态弱类型的解释型编 ...

  3. git Lab ssh方式拉取代码失败

    gitLab在linux上已经安装好了, 在配置项目的时候报如下异常 使用http方式没问题, 但是用ssh方式设置repository URL 提示资源库不存在. returned status c ...

  4. pat 1008 Elevator(20 分)

    1008 Elevator(20 分) The highest building in our city has only one elevator. A request list is made u ...

  5. nyoj 56-阶乘因式分解(一)(数学)

    56-阶乘因式分解(一) 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:15 submit:16 题目描述: 给定两个数m,n,其中m是一个素数. ...

  6. C# Json之序列化与反序列化

    前言:在实际开发过程中经常都要和Json打交道,序列化与反序列化就成了开发中必不可缺的技能.本篇博客就教大家如何进行Json序列化与反序列化. 首先要添加引用NuGet包,Newtonsoft.Jso ...

  7. PostGIS 使用Mysql_fdw同步ArcGIS填坑记录

    ##实现Mysql_fdw数据同步过程中,出现过很多坑,开此贴记录一下 1.触发器记录 这里insert的时候,采用过insert into f_pressureline select new.*,出 ...

  8. 20191031-9 beta week 1/2 Scrum立会报告+燃尽图 07

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9917 一.小组情况 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 韩昊 ...

  9. python3 之 变量作用域详解

    作用域: 指命名空间可直接访问的python程序的文本区域,这里的 ‘可直接访问’ 意味着:对名称的引用(非限定),会尝试在命名空间中查找名称: L:local,局部作用域,即函数中定义的变量: E: ...

  10. Deep attention tracking via Reciprocative Learning

    文章:Deep attention tracking via Reciprocative Learning 出自NIPS2018 文章链接:https://arxiv.org/pdf/1810.038 ...