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

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, coutstreams or the %I64d specifier.

Sample test(s)
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

分析:

大致思路就是,先把所有的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 = ;
const int Max_M = ;
int a[Max_M] , b[Max_M] ,t[Max_M] ;
LL dp[][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 = ; i <= N ; i++)
dp[][i] = fabs(a[] - i) ;
now = ;
for(k = ; k <= M ; k++){
step = t[k] - t[k-] ;
step *= D ;
if(step > N)
step = N ;
head = rear = ;// clear the queue
/* init the queue , keep crease sort sequece */
for(i = ; i <= step ; i++){
while(head < rear && dp[now][i] < Queue[rear-])
rear-- ;
Queue[rear] = dp[now][i] ;
Index[rear] = i ;
rear ++ ;
}
/*enum the curent position i */
for(i = ; i <= N ; i++){
L = i - step ;
if(L <= )
L = ;
R = i + step ;
while(head < rear && Index[head] < L)
head++ ;
if(R <= N){
while(head < rear && dp[now][R] < Queue[rear-])
rear-- ;
Queue[rear] = dp[now][R] ;
Index[rear] = R ;
rear ++ ;
}
dp[now^][i] = Queue[head] + fabs(a[k] - i) ;
}
now ^= ;
}
return *min_element(dp[now]+,dp[now]++N) ;
} int main(){
LL sum ;
while(cin>>N>>M>>D){
sum = ;
for(int i = ; i <= M ; i++){
scanf("%d%d%d",&a[i],&b[i],&t[i]) ;
sum += b[i] ;
}
cout<<sum - DP()<<endl ;
}
return ;
}

Codeforces Round #219 (Div. 1) C. Watching Fireworks is Fun的更多相关文章

  1. Codeforces Round #219 (Div. 2) E. Watching Fireworks is Fun

    http://codeforces.com/contest/373/problem/E E. Watching Fireworks is Fun time limit per test 4 secon ...

  2. 数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun

    题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...

  3. Codeforces Round #219 (Div. 1)(完全)

    戳我看题目 A:给你n个数,要求尽可能多的找出匹配,如果两个数匹配,则ai*2 <= aj 排序,从中间切断,分成相等的两半后,对于较大的那一半,从大到小遍历,对于每个数在左边那组找到最大的满足 ...

  4. Codeforces Round #219 (Div. 2) B. Making Sequences is Fun

    B. Making Sequences is Fun time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  5. Codeforces Round #219 (Div. 2) D. Counting Rectangles is Fun 四维前缀和

    D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...

  6. Codeforces Round #219 (Div. 2) D题

    D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...

  7. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  8. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  9. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

随机推荐

  1. OC-字典

    1.所有的key都是一个字符串,键 值是成对出现的.且都不能为空,非要为空要使用NSnull.字典是通过key来存取值的,key valu必须成对出场 2.字典是有键-值的数据组合,通过key查找对于 ...

  2. Some thoughts on a progress

    I can feel that I am making great progress now.. if inspected closely, it is obvious that what I'm g ...

  3. MySQL时间戳和时间格式转换函数

    MySQL时间戳和时间格式转换函数:unix_timestamp and from_unixtime unix_timestamp将时间转化成时间戳格式.from_unixtime将时间戳转化成时间格 ...

  4. thinkphp禁止模版标签解析

    场景: 页面中某些样式或者js中含有tp定义的模版标签,如果被tp当成模版标签解析,就会解析异常. tp中提供了<literal></literal>标签用于禁止标签内部的代码 ...

  5. Linux命令之WC

    $ wc story.txt39 237 1901 story.txt● Use -l for only line count● Use -w for only word count● Use -c ...

  6. TX Textcontrol 使用总结六——常用属性设置

    1.字体设置 Tx textcontrol字体设置以版本22为例,直接设置FontSize =int,字体大小将小于正常其他控件字体设置.应做如下处理(仅供参考) this.textControl1. ...

  7. C语言每日一题之No.2

    题目:已知三个整型数8,12,6,按公式s=a+b*c计算,并显示结果 思路:定义三个整型变量a,b,c 定义一个变量s用来保存运算结果 输出 程序: #include <stdio.h> ...

  8. 文件压缩与挤压ZIP

    /// <summary> /// Zip压缩与解压缩 /// </summary> public class ZipHelper { /// <summary> ...

  9. 【MySQL】技巧 之 count(*)、count(1)、count(col)

    只看结果的话,Select Count(*) 和 Select Count(1) 两着返回结果是一样的. 假如表沒有主键(Primary key), 那么count(1)比count(*)快,如果有主 ...

  10. 在html中添加缩放meta

    见代码(html) <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://w ...