Codeforces Round #219 (Div. 1) C. Watching Fireworks is Fun
4 seconds
256 megabytes
standard input
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.
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 = ;
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的更多相关文章
- 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 ...
- 数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun
题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...
- Codeforces Round #219 (Div. 1)(完全)
戳我看题目 A:给你n个数,要求尽可能多的找出匹配,如果两个数匹配,则ai*2 <= aj 排序,从中间切断,分成相等的两半后,对于较大的那一半,从大到小遍历,对于每个数在左边那组找到最大的满足 ...
- 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 ...
- 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 ...
- Codeforces Round #219 (Div. 2) D题
D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
随机推荐
- ubuntu 创建快捷方式
sudo ln -s /opt/eclipse/eclipse /usr/bin/eclipse 这样就可以在命令行中敲:eclipse,来打开eclipse了. 应该还有一种方式,就像快捷方式一样. ...
- Struts2 - 传值
Struts2传值有几种方式: 1)URL parameter 例如配置代码: <action name="login_*" class="com.my.actio ...
- 工作中简单又实用的vim配置
set number syntax enable set tabstop=4 set shiftwidth=4 set noexpandtab set cindent set autoindent s ...
- 【linux】man和--help
man和--help都是获取命令帮助的指令. man 格式: man [命令] [root@andon tmp]# man pwd --help 命令 --help [root@andon tmp]# ...
- Redis报错:WRONGTYPE Operation against a key holding the wrong kind of value 解决处理
首先应该明白报这个错误说明了你用的jedis方法与redis服务器中存储数据的类型存在冲突. 例如:数据库中有一个key的数据存储的是Hash类型的,但是你使用jedis执行数据操作的时候却使用了非H ...
- Python列表,元组,字典,序列,引用
1.列表 # Filename: using_list.py # This is my shopping list shoplist=["apple", "mango&q ...
- postgresql数据库文件目录
不同的发行版位置不同 查看进程 ps auxw | grep postgres | grep -- -D 找到默认的目录 /usr/lib/postgresql/9.4/bin/postgres -D ...
- 再看erlang的socket部分基础
socket的选项里面的{packet,0}和{packet,raw}的区别 {packet,} erlang处理2字节大端包头 {packet,} erlang处理4字节大端包头 {packet,} ...
- 黄聪:利用Aspose.Word控件实现Word文档的操作(转)
撰写人:伍华聪 http://www.iqidi.com Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及 ...
- 黄聪:《跟黄聪学WordPress主题开发》
又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...