A festival will be held in a town's main street. There are n sections in the main street. The sections are numbered1 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, coutstreams or the %I64d specifier.
50 3 1
49 1 1
26 1 4
6 1 10
-31
10 2 1
1 1000 4
9 1000 4
1992
分析:
大致思路就是,先把所有的b[i]加起来,因为计算式中b[i]跟决策没关系,然后反过来求|a[i]-x|的和的最小值即可。
dp[i][j] 表示第 i 个烟花在 j 位置时(前 i 个)获得的总|a[i]-x|的和的最小值
dp[i][j] = min(dp[i-1][k]) +fabs(a[i] - j), ( j-t*d<= k <= j+t*d )
要用单调队列进行优化:
第K个烟花,枚举每个观赏地点 i ,转移状态为:上一层[i - L , i + R] -〉 当前层i 。
因为是枚举观赏地点,所以可以发现移动的范围相当于一个滑动窗口,随着枚举向右移动,用单调队列维护即可。
如: 区间为4 。 滑动窗口为下大概所示。
**********
**** 位置1
**** 位置2
**** 位置3
**** 位置4
**** 位置5
**** 位置6
注意点:
(1)保持队列升序
(2)保持在当前扩展范围中
#include <iostream> #include <string> #include <string.h> #include <map> #include <stdio.h> #include <algorithm> #include <queue> #include <vector> #include <math.h> #include <set> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std; typedef long long LL ; const int Max_N = 150008 ; const int Max_M = 308 ; int a[Max_M] , b[Max_M] ,t[Max_M] ; LL dp[2][Max_N] , Queue[Max_N] ,Index[Max_N] ; int N ,M ; LL D ; LL DP(){ int head , rear , i , k ,now ,L , R ; LL step ; for(i = 1 ; i <= N ; i++) dp[0][i] = fabs(a[1] - i) ; now = 0 ; for(k = 2 ; k <= M ; k++){ step = t[k] - t[k-1] ; step *= D ; if(step > N) step = N ; head = rear = 0 ;// clear the queue /* init the queue , keep crease sort sequece */ for(i = 1 ; i <= step ; i++){ while(head < rear && dp[now][i] < Queue[rear-1]) rear-- ; Queue[rear] = dp[now][i] ; Index[rear] = i ; rear ++ ; } /*enum the curent position i */ for(i = 1 ; i <= N ; i++){ L = i - step ; if(L <= 0) L = 1 ; R = i + step ; while(head < rear && Index[head] < L) head++ ; if(R <= N){ while(head < rear && dp[now][R] < Queue[rear-1]) rear-- ; Queue[rear] = dp[now][R] ; Index[rear] = R ; rear ++ ; } dp[now^1][i] = Queue[head] + fabs(a[k] - i) ; } now ^= 1 ; } return *min_element(dp[now]+1,dp[now]+1+N) ; } int main(){ LL sum ; while(cin>>N>>M>>D){ sum = 0 ; for(int i = 1 ; i <= M ; i++){ scanf("%d%d%d",&a[i],&b[i],&t[i]) ; sum += b[i] ; } cout<<sum - DP()<<endl ; } return 0 ; }