http://codeforces.com/contest/373/problem/E

E. 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 ≤ im) launching is on time ti at section ai. If you are at section x (1 ≤ xn) 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 ≤ dn).

Each of the next m lines contains integers aibiti (1 ≤ ain; 1 ≤ bi ≤ 109; 1 ≤ ti ≤ 109). The i-th line contains description of the i-th launching.

It is guaranteed that the condition titi + 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, cout streams or the %I64dspecifier.

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

题目分析:核心在如何减小∑|ai-x|

依然是长度不等的柱子,一时间顺序排列(ai),需要确定所有的xi,使得∑|ai-x|最小,当然两个xi之间需要可达。

这个问题确实很难,但是由于m=300,貌似暴力点是可以过的。n=150000。 【dp】

dp[i][j] 前i个柱子(ai),以xi=j为决策所能达到的最小的 ∑|ai-x|

dp复杂度是n*m=150000*300,而方程转移复杂度是O(n)

因此,需要减小方程转移的复杂度。转移集合D={dp[i-1][r] |  r 和 j 可达},转移目标是求D的最小值。

可以发现转移集合D是连续的,r是连续的,因此问题变成了 求解区间最小值。

思路有二:

第一,线段树,每次将第i-1的数据放入一颗线段树,对于计算i 的时候,查询是log(n)【当然,此题修改和查询较简单,树状数组也可以实现最小值查询】

第二,单调队列,转移集合D只需要在j=1时求一次,以后j增加的时候,集合D最多只修改了两个元素。而建立一个递增的单调队列,维护待查询 区间的最小值。

思路1的复杂度较大,m*n*log(n),会T。但是叫一个小优化就过了。某些时候,最小值具有连续性,因此并不是每一次都需要去线段树里面查询。

思路2的解法比较常规,复杂度m*n。

下面是AC的代码,基于思路1+树状数组最小值查询+小优化。

#include<algorithm>
#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<ctime> using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FORU(i,a,b) for(Uint i=a;i<b;i++)
#define FORD(i,a,b) for(int i=a;i>b;i--)
#define MST(a,num) memset(a,num,sizeof(a))
#define MCP(d,s) memcpy(d,s,sizeof(s))
#define WH(n) while(scanf("%d", &n) != EOF)
#define WHZ(n) while(scanf("%d", &n) != EOF && n != 0)
#define SCF(a) scanf("%d",&a)
#define SCFS(a) scanf("%s",a)
#define PRF(a) printf("%d",a)
#define PRS(a) printf("%s",a)
#define PRFF(a) printf("%d\n",a)
#define PRSF(a) printf("%s\n",a)
#define PRFFU(a) printf("%I64d\n",a) #define PI acos(-1)
#define min2(a,b) (a<b)?a:b
#define max2(a,b) (a>b)?a:b
#define max3(a,b,c) max(max(a,b),c)
#define max4(a,b,c,d) max(max(a,b),max(c,d)) #define FORE(e,x) for(__typeof(x.begin()) e=x.begin(); e!=x.end(); e++) //foreach(it, ans ) cout<<*it<<" ";
#define all(a) (a).begin(),(a).end() //sort(all(v));
#define len(a) ((int)(a).size())
#define pb push_back
#define mk make_pair
#define V(etype) vector<etype> typedef __int64 Uint;
typedef vector<int> Vint;
typedef pair<int,int>mypair; #define INF 0x3f3f3f3f
#define eps 1e-9
const int N=300000+100; int val[N];
int sum[N];
struct Bitree{
int n;//1 -> n
Bitree(int k){
n=1<<k;
clear();
}
void clear(){
FOR(i,1,n+1)sum[i]=val[i]=INF;
}
void add(int i,int _val){
val[i]=_val;
for(;i<=n;i+=-i&i)sum[i]=min(sum[i],_val);
}
bool cover(int i,int j)//whether i cover j,j<i
{
return j+(-i&i)>i;
}
int query(int j,int i){//[j,i]
int ret=INF;
for(;i>=j;){
if(cover(i,j)){ret=min(ret,val[i]);i--;}
else {ret=min(ret,sum[i]);i-=-i&i;}
}
return ret;
}
};
int dp[N];
int pos[N];
int b[N];
int t[N];
int main(){
int n,m,d;
Uint ret;
Bitree tree(18);
while(cin>>n>>m>>d){
FOR(i,0,m)scanf("%d%d%d",&pos[i],&b[i],&t[i]);
ret=0;
int tmp;
Uint ad;
FOR(j,1,n+1)dp[j]=abs(pos[0]-j);
FOR(i,1,m){
ad=(t[i]-t[i-1]);
ad*=d;
FORU(j,1,n+1){
if(j==1){
tree.clear();
FOR(r,1,n+1)tree.add(r,dp[r]);
tmp=tree.query(max2(1,j-ad),min2(n,j+ad));
}
else
if(j+ad <= n && val[j+ad]<=tmp)tmp=val[j+ad];
else
if(j-ad<=1 || val[j-ad - 1]>tmp);
else tmp=tree.query(max2(1,j-ad),min2(n,j+ad));
dp[j]=tmp+abs(pos[i]-j);
}
}
ret=INF;
FOR(j,1,n+1)ret=min2(ret,dp[j]);
ret=-ret;
FOR(i,0,m)ret+=b[i];
PRFFU(ret);
}
return 0;
}

Codeforces Round #219 (Div. 2) E. Watching Fireworks is Fun的更多相关文章

  1. Codeforces Round #219 (Div. 1) C. Watching Fireworks is Fun

    C. Watching Fireworks is Fun time limit per test 4 seconds memory limit per test 256 megabytes input ...

  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. (转载)与OpenDialog相关的一个问题

    OpenDialog的一个问题 有一个功能要求就是[每次打开文件的对话框的默认路径是上一次保存文件的路径],本来这个就是设置OpenDialog控件的InitialDir属性就行了,但是第一次打开的时 ...

  2. 面试题:m个长度为n的ordered array,求top k 个 数字

    package com.sinaWeibo.interview; import java.util.Comparator; import java.util.Iterator; import java ...

  3. C#数据类型-string

    string是各种编程语言中最基础的数据类型,长期以来受尽其它类的压迫,经常被肢解(Substring.Split).蹂躏(Join)... 而现在C#数据类型string要“翻身闹革命”了,它几乎无 ...

  4. Js Carousel

    http://getbootstrap.com/javascript/#carousel http://owlgraphic.com/owlcarousel/#demo https://www.mob ...

  5. xmpp 配置数据库 服务器

    一.了解XMPP 协议(标准) XMPP 即时通讯协议 SGIP 短信网关协议 这手机发短信 移动支付和网页支付 0x23232[0,1] 0x23232 0x23232 0x23232 只有协议,必 ...

  6. Socket实现简单的聊天通信

    最近学习了Socket后,感觉Socket挺好玩的,在博客中看到socket在实时聊天功能的很强大,于是乎就做了一个简单的聊天功能,今天贴出来,能够与大家一起共享,有不对之处,能够给予指出,谢谢! 服 ...

  7. hdu 2460

    这是一道双联通分量的题,要用到LCA算法: 听说这个算法有两种实现方式:一个是dfs+线段树或着RMQ;一个是用tarjin: 我用的是tarjin: 题目比较简单,就是每次加了一条边之后剩下的桥的个 ...

  8. [译]GotW #2: Temporary Objects

        不必要的和(或)临时的变量经常是罪魁祸首,它让你在程序性能方面的努力功亏一篑.如何才能识别出它们然后避免它们呢? Problem JG Question: 1. 什么是临时变量? Guru Q ...

  9. “net.tcp://localhost:9000/ObtainData”处带有协定“"IObtainData"”的 ChannelDispatcher 无法打开其 IchannelListener。

    http://stackoverflow.com/questions/1252791/how-to-solve-the-channeldispatcher-is-unable-to-open-its- ...

  10. 《Gulp 入门指南》 : 使用 gulp 压缩 JS

    <Gulp 入门指南> : 使用 gulp 压缩 JS 请务必理解如下章节后阅读此章节: 安装 Node 和 gulp 访问论坛获取帮助 压缩 js 代码可降低 js 文件大小,提高页面打 ...