您的位置:首页 > 其它

Codeforces 373E Watching Fireworks is Fun【思维+单调队列优化Dp+滚动数组】

2017-10-19 23:06 519 查看
E. 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 %I64dspecifier.

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


题目大意:

给出N个位子,排成一列,现在有M个烟花,每秒人的速度d,每个烟花有三个元素:a,b,t,表示在时刻t会绽放,如果我们在位子x观看到了的话,会获得b-abs(a-x)的欢乐值(可能为负而且所有烟花都必须观看);初始的时候可以在任何位子,问最高获得多少欢乐值。

思路:

①观察到m并不大,所以t的数量也就是有限制的,考虑求最优方案,那么不难想到Dp去做,设定Dp【i】【j】表示在位子i,时刻j的时候获得的最优的欢乐值。

那么我们不难想到其状态转移方程:

Dp【i】【j】=Dp【x】【j-1】(x和i之间的差距要在时间内走的到才能转移)+当前时刻能够获得的价值summ;

那么我们不难想到用线段树加速优化,但是这道题卡了Log.........................

观察到转移的时候,Dp【i】【j】转移新获得的价值只和当前位子j有关,所以我们能够用单调队列优化。

②对于单调队列能够确定决策单调性,但是两个方向不能同时进行,所以我们分两个方向两次去做就行了。

数组LL太大,爆内存,要用滚动数组优化= =

Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll __int64
vector< pair<ll,ll> >mp[305];
ll n,m,d;
ll dp[3][150060];
ll que[150060];
int main()
{
while(~scanf("%I64d%I64d%I64d",&n,&m,&d))
{
ll cnt=0;
map<ll,ll>s;s.clear();
map<ll,ll>re;re.clear();
for(ll i=1;i<=m;i++)mp[i].clear();
for(ll i=1;i<=m;i++)
{
ll a,b,t;
scanf("%I64d%I64d%I64d",&a,&b,&t);
if(s[t]==0)s[t]=++cnt;
re[s[t]]=t;
mp[s[t]].push_back(make_pair(a,b));
}
memset(dp,0,sizeof(dp));
for(ll i=1;i<=cnt;i++)
{
ll ttt=re[i]-re[i-1];
ll move=ttt*d;
ll tot,head;
head=tot=0;
que[tot]=1;
for(ll j=1;j<=n;j++)
{
ll summ=0;
for(ll k=0;k<mp[i].size();k++)
{
pair<ll,ll> temp=mp[i][k];
summ+=temp.second-abs(temp.first-j);
}
while(head<=tot&&abs(que[head]-j)>move)head++;
dp[1][j]=dp[0][que[head]]+summ;
while(j+1<=n&&head<=tot&&dp[0][que[head]]<=dp[0][j+1])tot--;
if(j+1<=n)que[++tot]=j+1;
}
head=tot=0;
que[tot]=n;
for(ll j=n;j>=1;j--)
{
ll summ=0;
for(ll k=0;k<mp[i].size();k++)
{
pair<ll,ll> temp=mp[i][k];
summ+=temp.second-abs(temp.first-j);
}
while(head<=tot&&abs(que[head]-j)>move)head++;
dp[1][j]=max(dp[1][j],dp[0][que[head]]+summ);
while(j-1>=1&&head<=tot&&dp[0][que[head]]<dp[0][j-1])tot--;
if(j-1>=1)que[++tot]=j-1;
}
for(ll j=1;j<=n;j++)dp[0][j]=dp[1][j];
}
ll output=-10000000000000000ll;
for(ll j=1;j<=n;j++)output=max(output,dp[0][j]);
printf("%I64d\n",output);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 373E