Special Subsequence


Time Limit: 5 Seconds      Memory Limit: 32768 KB

There a sequence S with n integers , and A is a special subsequence that satisfies |Ai-Ai-1| <= d ( 0 <i<=|A|))

Now your task is to find the longest special subsequence of a certain sequence S

Input

There are no more than 15 cases , process till the end-of-file

The first line of each case contains two integer n and d ( 1<=n<=100000 , 0<=d<=100000000) as in the description.

The second line contains exact n integers , which consist the sequnece S .Each integer is in the range [0,100000000] .There is blank between each integer.

There is a blank line between two cases

Output

For each case , print the maximum length of special subsequence you can get.

Sample Input

5 2
1 4 3 6 5 5 0
1 2 3 4 5

Sample Output

3
1

题解:让求|Ai-Ai-1| <= d 的最长子序列;
很简单就想到DP;
dp[i]表示前i个数的最长为多少,
则dp[i]=max(dp[j]+1) abs(a[i]-a[j])<=d
不出意外,肯定超时了;
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define P_ printf(" ")
#define T_T while(T--)
const int MAXN=100010;
int dp[MAXN];
int m[MAXN];
int main(){
int n,d;
while(~scanf("%d%d",&n,&d)){
for(int i=0;i<n;i++)SI(m[i]);
mem(dp,0);
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(abs(m[i]-m[j])<=d){
dp[i]=max(dp[i],dp[j]+1);
ans=max(ans,dp[i]);
}
}
}
printf("%d\n",ans+1);
}
return 0;
}

 

接下来就是如何高效地找到满足差值在d以内的最大值。

将数字进行离散化,对于一个新的数,就可以确定一个范围,然后在这个范围进行查找dp的最值+1即可。

线段树每一个结点保存的是这个区间的最值,叶子结点的值便是以这个数结尾的最长数量;

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define P_ printf(" ")
#define T_T while(T--)
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
#define V tree[root]
const int MAXN=100010;
int tree[MAXN<<2];
int a[MAXN],b[MAXN];
int dp[MAXN];
int nm;
int pushup(int root){
tree[root]=max(tree[ll],tree[rr]);
}
void update(int root,int l,int r,int nt,int v){
int mid=(l+r)>>1;
if(l==r){
V=v;return;
}
if(mid>=nt)update(lson,nt,v);
else update(rson,nt,v);
pushup(root);
}
void query(int root,int l,int r,int L,int R){
if(l>=L&&r<=R){
nm=max(nm,V);
return;
}
int mid=(l+r)>>1;
if(mid>=L)query(lson,L,R);
if(mid<R)query(rson,L,R);
}
int main(){
int n,d;
while(~scanf("%d%d",&n,&d)){
for(int i=0;i<n;i++)SI(a[i]),b[i]=a[i];
sort(b,b+n);
mem(tree,0);mem(dp,0);
for(int i=0;i<n;i++){
int l,r;
l=lower_bound(b,b+n,a[i]-d)-b+1;
r=upper_bound(b,b+n,a[i]+d)-b+1-1;
nm=0;
query(1,1,n,l,r);
dp[i]=nm+1;
update(1,1,n,lower_bound(b,b+n,a[i])-b+1,dp[i]);
}
int ans=0;
for(int i=0;i<n;i++)ans=max(ans,dp[i]);
printf("%d\n",ans);
}
return 0;
}

  

Special Subsequence(离散化线段树+dp)的更多相关文章

  1. 干物妹小埋 (离散化 + 线段树 + DP)

    链接:https://ac.nowcoder.com/acm/contest/992/B来源:牛客网 题目描述 在之前很火的一个动漫<干物妹小埋>中,大家对小埋打游戏喝可乐的印象十分的深刻 ...

  2. 南阳理工 题目9:posters(离散化+线段树)

    posters 时间限制:1000 ms  |  内存限制:65535 KB 难度:6   描述 The citizens of Bytetown, AB, could not stand that ...

  3. SGU 180 Inversions(离散化 + 线段树求逆序对)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...

  4. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  5. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  6. 【POJ】2528 Mayor's posters ——离散化+线段树

    Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K   Description The citizens of Bytetown, A ...

  7. hpu校赛--雪人的高度(离散化线段树)

    1721: 感恩节KK专场——雪人的高度 时间限制: 1 Sec  内存限制: 128 MB 提交: 81  解决: 35 [提交][状态][讨论版] 题目描述 大雪过后,KK决定在春秋大道的某些区间 ...

  8. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

  9. 【bzoj4636】蒟蒻的数列 离散化+线段树

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801379.html 题目描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个 ...

随机推荐

  1. C++中虚函数功能的实现机制

    要理解C++中虚函数是如何工作的,需要回答四个问题. 1.  什么是虚函数. 虚函数由于必须是在类中声明的函数,因此又称为虚方法.所有以virtual修饰符开始的成员函数都成为虚方法.此时注意是vir ...

  2. xhprof failed to execute cmd: " dot -Tpng". stderr: `sh: dot: command not found '

    wget http://www.graphviz.org/pub/graphviz/ARCHIVE/graphviz-2.28.0.tar.gz tar xzvf graphviz-2.28.0.ta ...

  3. mina学习资料整合

    最好的资料当然是官方文档:https://mina.apache.org/mina-project/userguide/user-guide-toc.html 官方文档,配合源码中的example例子 ...

  4. Naive Bayes Theorem and Application - Theorem

    Naive Bayes Theorm And Application - Theorem Naive Bayes model: 1. Naive Bayes model 2. model: discr ...

  5. Use API to retrieve data from internet

    Reference: Working with APIs Many big companies and organizations provide API for us to retrieve dat ...

  6. 【转载】Android Studio jar、so、library项目依赖,原文链接http://zhengxiaopeng.com/2014/12/13/Android-Studio-jar、so、library项目依赖/

    前言 Android Studio(以下简称AS)在13年I/O大会后放出预览版到现在放出的正式版1.0(PS.今天又更新到1.0.1了)历时一年多了,虽然Google官方推出的Android开发者的 ...

  7. NYOJ 527 AC_mm玩dota

    AC_mm玩dota 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 大家都知道AC_mm比较喜欢玩游戏,特别是擅长war3这款经典游戏.某天AC_mm来到了VS平台上 ...

  8. 315M无线发射模块天线的长度计算

    波长=光速/频率=300/315=0.952米 1/4波长须要的天线长度=波长*1/4=0.952/4=0.238米 考虑导线传播高频信号的缩短率在0.98左右,因此天线长度=0.238*0.98=0 ...

  9. c/c++测试程序运行时间

    算法分析中需要对各种算法进行性能测试,下面介绍两种通用的测试方法,由于只用到标准c语言函数,所以在各种平台和编译器下都能使用. 方法1: clock()函数 开始计时:start = clock() ...

  10. DropDownList为啥总是获取第一项的值???

    小菜: DropDownList控件绑定的数据,在获取数据时总是获取到第一项,很是郁闷,怎么回事,于是就各种想,都没有找到问题的原因. 请看下面的代码 前台代码: <asp:DropDownLi ...