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.
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.
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.
50 3 1 49 1 1 26 1 4 6 1 10
-31
10 2 1 1 1000 4 9 1000 4
1992
题意(转):有一个街道放烟花,街道分成 N 块(1 <= N <= 150000),相邻块距离为1, 你移动速度是 d 。烟花放 M 次 (1 <= M <= 300), 给出每次放烟花的时间地点,Happy 值得计算公式 b[i] - abs(a[i] - x) ,(b[i] 第i次放烟花的基准happy, a[i] 是放烟花的地点,x 是你所在的地点) 。求 Happy 值总和最大是多少。
http://codeforces.com/problemset/problem/372/C
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) 。不过这样会爆数组和时间。怎么办?
发现大神是用单调队列作辅助的。dp[ i ] [ j ] =?????+ b[ i ] - | a[ i ] - j | ,只要?????最大那么dp[i][j]就是最优解。由于相邻的j之间能取到的上一个状态k的范围[j-d,j+d]会有重叠部分,因此可以用双端队列进行优化。
(以上转自娜娜神)#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long
int n,m,d,a[400],b[400],t[400],now,last,L,R,l,Q[150010];
long long dp[2][150010];
void f1(ll x)
{
while(L<=R&&Q[L]<x)L++; //L<=R控制队列左端小于右端。如果L的上一次计算的最大值在范围之外,L++,直到L是符合的。
}
void f3(int l)
{
while(L<=R&&dp[last][Q[R]]<dp[last][l])R--;//如果队列保存的dp值比l要小,R--,继续往前比,将小的从队列里拿走。吧l插入。
Q[++R] = l;
}
void f2(ll y)
{
while(l<=n&&l<=y)f3(l),l++;//从上一次比较完的l开始.继续比较,
}
int main()
{
scanf("%d%d%d",&n,&m,&d);
for(int i = 1;i<=m;i++)scanf("%d%d%d",&a[i],&b[i],&t[i]);
for(int i = 1;i<=m;i++)//遍历每盏灯
{
now ^= 1;last =now^1;
L=l=1,R=0;
for(int j = 1;j<=n;j++)//位置
{
ll k1 = j-1ll*(t[i]-t[i-1])*d;
ll k2 = j+1ll*(t[i]-t[i-1])*d;
f1(k1);
f2(k2);
dp[now][j] = dp[last][Q[L]]+b[i]-abs(a[i]-j);
}
}
long long ans = -10101010;
for(ll i = 1;i<=n;i++)
ans = max(ans,dp[now][i]);
cout<<ans<<endl;
//printf("%d\n",ans);
return 0;
}