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. 正在调用的 ServicedComponent 配置不正确(请使用 regsvcs 重新注册)

    问题: 正在调用的 ServicedComponent 配置不正确(请使用 regsvcs 重新注册)   方法1: 我用的是64位操作系统.IIS中,启用32位应用程序设置为false.这样就可以了 ...

  2. [java]序列化框架性能对比(kryo、hessian、java、protostuff)

    序列化框架性能对比(kryo.hessian.java.protostuff) 简介:   优点 缺点 Kryo 速度快,序列化后体积小 跨语言支持较复杂 Hessian 默认支持跨语言 较慢 Pro ...

  3. jQuery validate基本原则

    Markup recommendations Each input has a label associated with it: The for-attribute of the label ref ...

  4. windows 系统中打开一个数字证书所经历的过程

         今天在使用Outlook express调试CSP程序时,发现数字证书总是加载不上(提示该数字证书已经被破坏),使用断点进去跟踪一下,发现在CSP程序中调用CPVerifySignature ...

  5. 异步等待时,在异步前弹出窗口的TIMER,不会TICK

    窗体中用FORMS.TIMER,TICK不会走.改用TIMERS.TIMER 则正常.

  6. Android官方提供的支持不同屏幕大小的全部方法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8830286 原文地址为:http://developer.android.com/ ...

  7. 【Hadoop测试程序】编写MapReduce测试Hadoop环境

    我们使用之前搭建好的Hadoop环境,可参见: <[Hadoop环境搭建]Centos6.8搭建hadoop伪分布模式>http://www.cnblogs.com/ssslinppp/p ...

  8. 【设计模式】工厂方法模式(Factory Method)

    工厂方法模式 定义了一个创建对象的接口,但由子类决定要实现的类是哪一个.工厂方法让类把实例化推迟到子类.所有的工厂模式都用来封装对象的创建.工厂方法模式通过让子类决定改创建的对象是什么,来达到将对象创 ...

  9. 网页颜色RGB记法和16进制记法转化方法

    A=>10,B=>11,C=>12,D=>13,E=>14,F=>15 看一个例子: 254,112,85 255/16 等于 15 余 14 那么它对应的应该是F ...

  10. 异常处理:Sys.WebForms.PageRequestManagerParserErrorException:The message……

    如果你为了使页面可以达到局部刷新的效果,并且用了UpdatePanel控件,这是如果你在后台页面用到Response对象时肯呢过会抛出一下异常: 解决方法:$(document).ready(func ...