CF 372C Watching Fireworks is Fun

来源:互联网 发布:人民币大小写转换软件 编辑:程序博客网 时间:2024/05/17 23:32
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 numbered 1 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 cincout streams or the %I64dspecifier.

Sample test(s)
input
50 3 149 1 126 1 46 1 10
output
-31
input
10 2 11 1000 49 1000 4
output
1992

题意(转):有一个街道放烟花,街道分成 N 块(1 <= N <= 150000),相邻块距离为1, 你移动速度是 d 。烟花放 M 次 (1 <= M <= 300), 给出每次放烟花的时间地点,Happy 值得计算公式 b[i] - abs(a[i] - x) ,(b[i] 第i次放烟花的基准happy, a[i] 是放烟花的地点,x 是你所在的地点) 。求 Happy 值总和最大是多少。

http://codeforces.com/problemset/problem/372/C

dp[i][j]表示当前放到了第I支烟花并且放这支烟花的时候他站在j点看。推出状态转移方程:

dp[ i ] [ j ] =max(dp[ i - 1] [ k ]) + b[ i ] - | a[ i ] - j | ,其中  max(1,j-t*d)<=k<=min(n,j+t*d) 。不过这样会爆数组和时间。怎么办?

发现大神是用单调队列作辅助的。dp[ i ] [ j ] =?????+ b[ i ] - | a[ i ] - j | ,只要?????最大那么dp[i][j]就是最优解。由于相邻的j之间能取到的上一个状态k的范围[j-d,j+d]会有重叠部分,因此可以用双端队列进行优化。

(以上转自娜娜神)
关键是代码里面的双端队列的写法。
构造一个从大到小的队列,其中不需要的元素就马上丢掉。
Q[L]:Q就是用数组表示队列,L就是队列的首位置。Q[L]表示记录的当前区间内,最大的DP值得位置(范围1到n)。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespace std;#define ll long longint n,m,d,a[400],b[400],t[400],now,last,L,R,l,Q[150010];long long dp[2][150010];void f1(ll x){while(L<=R&&Q[L]<x)L++; //L<=R控制队列左端小于右端。如果L的上一次计算的最大值在范围之外,L++,直到L是符合的。 } void f3(int l){while(L<=R&&dp[last][Q[R]]<dp[last][l])R--;//如果队列保存的dp值比l要小,R--,继续往前比,将小的从队列里拿走。吧l插入。 Q[++R] = l;}void f2(ll y){while(l<=n&&l<=y)f3(l),l++;//从上一次比较完的l开始.继续比较,}int main(){scanf("%d%d%d",&n,&m,&d);for(int i = 1;i<=m;i++)scanf("%d%d%d",&a[i],&b[i],&t[i]);for(int i = 1;i<=m;i++)//遍历每盏灯 {now ^= 1;last =now^1;L=l=1,R=0;for(int j = 1;j<=n;j++)//位置 {ll k1 = j-1ll*(t[i]-t[i-1])*d;ll k2 = j+1ll*(t[i]-t[i-1])*d;f1(k1);f2(k2);dp[now][j] = dp[last][Q[L]]+b[i]-abs(a[i]-j);}}long long ans = -10101010;for(ll i = 1;i<=n;i++)ans = max(ans,dp[now][i]);cout<<ans<<endl;//printf("%d\n",ans);return 0;}


1 0
原创粉丝点击