C. Watching Fireworks is Fun---dp

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

題目鏈接:http://codeforces.com/contest/372/problem/C


不得不說,我dp真的是太渣了,做一些簡單的還行,上點難度我就gg。

http://blog.csdn.net/u011151784/article/details/48181317

代碼:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#define ll long long
using namespace std;
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++;
}
void f3(int l){
	while(L<=R&&dp[last][Q[R]]<dp[last][l])
        R--;
	Q[++R]=l;
}
void f2(ll y){
	while(l<=n&&l<=y){
        f3(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;
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章