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. Subversion与TortoiseSVN的安装

    首先介绍一下Subversion与TortoiseSVN两者之间的关系: Subversion是一种集中分享信息的系统,它的核心是版本库,储存所有的数据.版本库按照文件树形式储存数据-包括文件和目录. ...

  2. select case when

    SELECT CASE WHEN dc.defect_code_name IS NOT NULL THEN dc.defect_code_name WHEN sf.second_defect_leve ...

  3. MS CRM 2011的自定义和开发(11)——插件(plugin)开发(一)

    http://www.cnblogs.com/StoneGarden/archive/2012/02/02/2336147.html MS CRM 2011的自定义和开发(11)——插件(plugin ...

  4. 【shell】nmap工具的使用

    NMap,也就是Network Mapper,是Linux下的网络扫描和嗅探工 具包,其基本功能有三个,一是探测一组主机是否在线:其次是扫描主机端口,嗅探所提供的网络服务:还可以推断主机所用的操作系统 ...

  5. github 添加 C# IGNORE

    在创建仓库时选择 VisualStudio 即可.

  6. Saltstack系列6:Saltstack之state

    state功能 state是Saltstack最核心的功能,通过预先定制好的sls(salt state file)文件对被控制主机进行状态管理,支持包括程序包(pkg).文件(file).网络配置( ...

  7. 一个 IT 青年北漂四年的感悟

    转载自:http://www.codeceo.com/article/it-man-beijing-4-years.html 工作这几年,每年都会有朋友离开北京,每次朋友跟我告别的时候总是让我有很多感 ...

  8. 【转】 远程到服务器安装visualSVN server,出现Service 'VisualSVN Server' failed to start的解决方法

    在帮助远程到服务器上安装visualSVN server的时候,出现Service 'VisualSVN Server' failed to start. 解决方法(先不要关闭安装弹出的错误窗口): ...

  9. wordpress主题结构_源码

    WordPress博客主题的工作机制 WordPress主题由一系列模板文件组成,每个文件分别控制主题的特定区域.无论你处于哪个页面都能看到的网站的静态部分,由header文件.sidebar和foo ...

  10. (C# Debug)A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll

    Debug 模式下运行程序的时候,Output 窗口出来个错误“A first chance exception of type 'System.ArgumentException' occurred ...