题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3349

  题意:给定一个数列,序列A是一个满足|Ai-Ai-1| <= d的一个序列,其中Ai为给定数列中的元素,要保持相对顺序,求最长的序列A。

  显然用DP来解决,f[i]表示以第i个数结尾时的最长长度,则f[i]=Max{ f[j]+1 | j<i && |num[j]-num[i]|<=d }。直接转移复杂度O(n^2),超时。显然对于每个数num只要保存最大的f值就可以了,然后就是查找num[j]在区间[num[i]-d, num[i]+d]的最大的f[j],用颗线段树维护就可以了。。

 //STATUS:C++_AC_480MS_3008KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=1e+,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int num[N],order[N],hig[N<<],f[N];
int n,d; void update(int l,int r,int rt,int w,int val)
{
if(l==r){
hig[rt]=max(hig[rt],val);
return ;
}
int mid=(l+r)>>;
if(w<=mid)update(lson,w,val);
else update(rson,w,val);
hig[rt]=max(hig[rt<<],hig[rt<<|]);
} int query(int l,int r,int rt,int L,int R)
{
if(L<=l && r<=R){
return hig[rt];
}
int mid=(l+r)>>,ret=;
if(L<=mid)ret=max(ret,query(lson,L,R));
if(R>mid)ret=max(ret,query(rson,L,R));
return ret;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,m,L,R,lnum,rnum,ans,w;
while(~scanf("%d%d",&n,&d))
{
for(i=;i<n;i++){
scanf("%d",&num[i]);
order[i]=num[i];
}
sort(order,order+n);
m=unique(order,order+n)-order; mem(hig,);ans=;
for(i=;i<n;i++){
lnum=num[i]-d;rnum=num[i]+d;
L=lower_bound(order,order+m,lnum)-order;
R=upper_bound(order,order+m,rnum)-order-;
f[i]=query(,m-,,L,R)+;
w=lower_bound(order,order+m,num[i])-order;
update(,m-,,w,f[i]);
ans=max(ans,f[i]);
} printf("%d\n",ans);
}
return ;
}

ZOJ-3349 Special Subsequence 线段树优化DP的更多相关文章

  1. Codeforces Round #426 (Div. 2) D 线段树优化dp

    D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  2. BZOJ2090: [Poi2010]Monotonicity 2【线段树优化DP】

    BZOJ2090: [Poi2010]Monotonicity 2[线段树优化DP] Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. ...

  3. [AGC011F] Train Service Planning [线段树优化dp+思维]

    思路 模意义 这题真tm有意思 我上下楼梯了半天做出来的qwq 首先,考虑到每K分钟有一辆车,那么可以把所有的操作都放到模$K$意义下进行 这时,我们只需要考虑两边的两辆车就好了. 定义一些称呼: 上 ...

  4. 【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp

    题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a varian ...

  5. POJ 2376 Cleaning Shifts (线段树优化DP)

    题目大意:给你很多条线段,开头结尾是$[l,r]$,让你覆盖整个区间$[1,T]$,求最少的线段数 题目传送门 线段树优化$DP$裸题.. 先去掉所有能被其他线段包含的线段,这种线段一定不在最优解里 ...

  6. 洛谷$P2605\ [ZJOI2010]$基站选址 线段树优化$dp$

    正解:线段树优化$dp$ 解题报告: 传送门$QwQ$ 难受阿,,,本来想做考试题的,我还造了个精妙无比的题面,然后今天讲$dp$的时候被讲到了$kk$ 先考虑暴力$dp$?就设$f_{i,j}$表示 ...

  7. D - The Bakery CodeForces - 834D 线段树优化dp···

    D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...

  8. 4.11 省选模拟赛 序列 二分 线段树优化dp set优化dp 缩点

    容易想到二分. 看到第一个条件容易想到缩点. 第二个条件自然是分段 然后让总和最小 容易想到dp. 缩点为先:我是采用了取了一个前缀最小值数组 二分+并查集缩点 当然也是可以直接采用 其他的奇奇怪怪的 ...

  9. Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...

随机推荐

  1. appium安装

    appium 这个移动端的自动化测试框架.是神器啊.selenium系列的工具.webdirver是一个使用很广泛的自动化测试框架. 至于API 测试,等,使用代码做单元测试就好了,各种框架很多,只要 ...

  2. R语言算术运算和逻辑运算

    Arithmetic Operators Operator Description + addition - subtraction * multiplication / division ^ or ...

  3. Python风格规范

    Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...

  4. .NET 实现异步处理的集中方式

    对于异步,相信大家都不十分陌生.准确点来说就是方法执行后立即返回,待到执行完毕会进行通知.就是当一个任务在执行的时候,尤其是需要耗费很长的时间进行处理的任务,如果利用单线程进行操作的话,势必造成界面的 ...

  5. CSS样式的特点与优先选择权

    CSS样式的特点:(子元素会继承父元素的某些样式,子元素有自己的样式就用自己的样式,没有的就用父元素的)      1.继承:              网页中子元素,将继承父元素的样式(比如要控制p ...

  6. echarts地图点定位的问题

    1,生成地图 2,如果需要产生地图上的点位,需要在配置中传入geoCoord具体数据为一下: { "海门": [121.15, 31.89], "鄂尔多斯": ...

  7. HDU 1513 Palindrome

    题目就是给一个字符串问最少插入多少个字符能让原字符串变为回文字符串. 算法: 用原串的长度减去原串与翻转后的串的最大公共字串的长度,就是所求答案. //#define LOCAL #include & ...

  8. 了解Objective-C中NSAutoreleasePool使用方法

    本文的目的是来了解Objective-C中NSAutoreleasePool使用方法,Objective-C的Foundation库实际上是种运行级对象系统,与一般的对象语言,例如C++,Java不一 ...

  9. [Swift系列]001-入门准备

    [引子] 最新的苹果发布会上公布了新的苹果编程语言Swift,并且演示了Xcode 6 Beta的一些新功能. 据苹果公司称,这个新语言开放的API更多,实用起来更方便,总之是值得学习.使用,比C/o ...

  10. PHP学习笔记04——数组

    <?php // 1.数组的声明,可以直接为数组元素赋值,也可以使用array函数声明数组 /* 索引数组:下标从0开始,依次递增 * 关联数组:字符串为下标 * */ //直接赋值声明数组,不 ...