博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #219 (Div. 1) C. Watching Fireworks is Fun
阅读量:6405 次
发布时间:2019-06-23

本文共 3649 字,大约阅读时间需要 12 分钟。

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 cincoutstreams 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 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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 = 150008 ;const int Max_M = 308 ;int a[Max_M] , b[Max_M] ,t[Max_M] ;LL dp[2][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 = 1 ; i <= N ; i++) dp[0][i] = fabs(a[1] - i) ; now = 0 ; for(k = 2 ; k <= M ; k++){ step = t[k] - t[k-1] ; step *= D ; if(step > N) step = N ; head = rear = 0 ;// clear the queue /* init the queue , keep crease sort sequece */ for(i = 1 ; i <= step ; i++){ while(head < rear && dp[now][i] < Queue[rear-1]) rear-- ; Queue[rear] = dp[now][i] ; Index[rear] = i ; rear ++ ; } /*enum the curent position i */ for(i = 1 ; i <= N ; i++){ L = i - step ; if(L <= 0) L = 1 ; R = i + step ; while(head < rear && Index[head] < L) head++ ; if(R <= N){ while(head < rear && dp[now][R] < Queue[rear-1]) rear-- ; Queue[rear] = dp[now][R] ; Index[rear] = R ; rear ++ ; } dp[now^1][i] = Queue[head] + fabs(a[k] - i) ; } now ^= 1 ; } return *min_element(dp[now]+1,dp[now]+1+N) ;}int main(){ LL sum ; while(cin>>N>>M>>D){ sum = 0 ; for(int i = 1 ; i <= M ; i++){ scanf("%d%d%d",&a[i],&b[i],&t[i]) ; sum += b[i] ; } cout<

 

转载于:https://www.cnblogs.com/liyangtianmen/p/3476833.html

你可能感兴趣的文章
关于Segmentation fault (core dumped)几个简单问题
查看>>
经典SQL语句大全(基础篇)
查看>>
HTML5 Canvas眨眼睛动画
查看>>
C-C和指针作业题(第一章)
查看>>
[推荐]网店代销的卖家,你的宝贝名称修改了吗?
查看>>
Android NDK JNI C++ <7> eg
查看>>
jQuery打造智能提示插件二(可编辑下拉框)
查看>>
[Python] Python 之 function, unbound method 和 bound method
查看>>
希尔排序
查看>>
改变随机数中一些值的概率
查看>>
Spark分析之SparkContext启动过程分析
查看>>
2014电子商务安全技术峰会(含全议题下载)
查看>>
东大OJ-5到100000000之间的回文质数
查看>>
linux C 快速排序法
查看>>
模仿与创新
查看>>
Python用subprocess的Popen来调用系统命令
查看>>
Java NIO与IO的差别和比較
查看>>
.NET源代码的内部排序实现
查看>>
解决Strict Standards: Only variables should be passed by reference
查看>>
解决JBoss只能通过localhost(127.0.0.1)而不能通过IP访问
查看>>