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”三种中任意一种就输 ...
随机推荐
- hadoop(五): shell命令
hdfs dfs -cat URI : 查看文件内容 hdfs dfs -cat hdfs dfs -cat hdfs://mycluster/user/root/rcc1 hdfs dfs -cat ...
- Android App启动错误的问题(connection to the server was unsuccessful)
问题描述: PhoneGap+Sencha Touch开发的应用,打包后的APP或者调试期间,在启动的时候提示如下信息: Application Error - The connection to t ...
- Myeclipse SVN错误 443
转:Myeclipse SVN错误:Error validating server certificate for https// Error validating server certificat ...
- android的Looper例子
直接贴代码 MsgThread.java package bb.aa.looperdemo; import android.os.Handler; import android.os.Looper; ...
- 像学历史课本一样学习Perl
第一次接触Perl,还是2008年10月份的时候,当时因为项目重构,需要进行大量的文本操作,于是便拾起了以“文本操作为己任”的Perl语言.当然,带我入门的还是那本赫赫有名的The Llama Bo ...
- C#笔记一 .Net Framwork
参考Learning hard本人在博客园的主页: http://www.cnblogs.com/zhili/ 以及本书中的一些知识点: http://www.cnblogs.com/zhil ...
- [JavaWebService-axis]-环境搭建
一.准备 1.下载环境需要的zip包 JDK Eclipse axis(http://axis.apache.org/axis2/java/core/download.html)(axis2-1.7. ...
- python3-cookbook
http://python3-cookbook.readthedocs.io/zh_CN/latest/index.html 一般的类找方法,通过MRO找到第一个就停了对吧,可以描述器好像会顺着MRO ...
- apache/php 开启 gzip压缩
1.php方式开启 原理: header("Content-Encoding: gzip"); echo gzencode('songjiankang'); 示例1: functi ...
- 编写一个程序对Largest函数进行测试,找出一组数据的最大值
源程序代码: import java.util.Scanner; public class findmax { public static void main(String[] args) { // ...