Special Subsequence

Time Limit: 5000ms
Memory Limit: 32768KB

This problem will be judged on ZJU. Original ID: 3349
64-bit integer IO format: %lld      Java class name: Main

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
 

Source

Author

CHEN, Zhangyi
 
解题:动态规划+线段树优化
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int tree[maxn<<],Lisan[maxn],a[maxn],tot,n,d;
int query(int L,int R,int lt,int rt,int v){
if(lt <= L && rt >= R) return tree[v];
int mid = (L + R)>>,ret = ;
if(lt <= mid) ret = query(L,mid,lt,rt,v<<);
if(rt > mid) ret = max(ret,query(mid + ,R,lt,rt,v<<|));
return ret;
}
void update(int L,int R,int pos,int val,int v){
if(L == R){
tree[v] = max(tree[v],val);
return;
}
int mid = (L + R)>>;
if(pos <= mid) update(L,mid,pos,val,v<<);
if(pos > mid) update(mid + ,R,pos,val,v<<|);
tree[v] = max(tree[v<<],tree[v<<|]);
}
int main(){
while(~scanf("%d%d",&n,&d)){
memset(tree,,sizeof tree);
for(int i = ; i < n; ++i){
scanf("%d",a + i);
Lisan[i] = a[i];
}
sort(Lisan,Lisan + n);
tot = unique(Lisan,Lisan + n) - Lisan;
int ret = ;
for(int i = ; i < n; ++i){
int L = max(,(int)(lower_bound(Lisan,Lisan + tot,a[i] - d) - Lisan + ));
int R = min(tot,(int)(upper_bound(Lisan,Lisan + tot,a[i] + d) - Lisan));
int tmp = ,pos = lower_bound(Lisan,Lisan + tot,a[i]) - Lisan + ;
if(L <= R) tmp = query(,tot,L,R,) + ;
ret = max(ret,tmp);
update(,tot,pos,tmp,);
}
printf("%d\n",ret);
}
return ;
}

ZOJ 3349 Special Subsequence的更多相关文章

  1. ZOJ 3349 Special Subsequence 简单DP + 线段树

    同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...

  2. Special Subsequence(离散化线段树+dp)

    Special Subsequence Time Limit: 5 Seconds      Memory Limit: 32768 KB There a sequence S with n inte ...

  3. ZOJ-3349 Special Subsequence 线段树优化DP

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3349 题意:给定一个数列,序列A是一个满足|Ai-Ai-1| & ...

  4. ZOJ 1733 Common Subsequence(LCS)

    Common Subsequence Time Limit: 2 Seconds      Memory Limit: 65536 KB A subsequence of a given sequen ...

  5. ZOJ 2672 Fibonacci Subsequence(动态规划+hash)

    题意:在给定的数组里,寻找一个最长的序列,满足ai-2+ai-1=ai.并输出这个序列. 很容易想到一个DP方程 dp[i][j]=max(dp[k][i])+1. (a[k]+a[i]==a[j], ...

  6. zoj 3349 dp + 线段树优化

    题目:给出一个序列,找出一个最长的子序列,相邻的两个数的差在d以内. /* 线段树优化dp dp[i]表示前i个数的最长为多少,则dp[i]=max(dp[j]+1) abs(a[i]-a[j])&l ...

  7. ZOJ 1301 The New Villa (BFS + 状态压缩)

    题意:黑先生新买了一栋别墅,可是里面的电灯线路的连接是很混乱的(每个房间的开关可能控制其他房间,房间数<=10),有一天晚上他回家时发现所有的灯(除了他出发的房间)都是关闭的,而他想回卧室去休息 ...

  8. ZOJ Monthly, October 2010 ABEFI

    ZOJ 3406 Another Very Easy Task #include <cstdio> #include <cstring> const int N = 10000 ...

  9. Soj题目分类

    -----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...

随机推荐

  1. Linux查找和替换目录下所有文件中字符串(转载)

    转自:http://rubyer.me/blog/1613/ 单个文件中查找替换很简单,就不说了.文件夹下所有文件中字符串的查找替换就要记忆了,最近部署几十台linux服务器,记录下总结. 查找文件夹 ...

  2. Google C++编程规范 – 第十九条 -《前置声明》

    转自:http://roclinux.cn/?p=3285 本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc wu == [规范] ...

  3. 微信H5页面 - 调酒

    这是微信H5的一款小游戏(一款酒的推广活动),主要游戏页面如下: 游戏规则: 点击选择2个元素(圈圈图片),放入瓶中,使它们与瓶中已有的三个元素碰撞,调配佳酿. 只有选择正确的2个元素搭配,才可以调配 ...

  4. bzoj 1705: [Usaco2007 Nov]Telephone Wire 架设电话线【dp】

    i的初始化写成2了于是成功查错2h--怕不是个傻子 设f[i][j]为第i根高为j,转移是 \[ f[i][j]=min(f[i-1][k]+abs(k-j)*c+(j-h[i])^2)(j>= ...

  5. bzoj 1643: [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪【枚举】

    4维枚举平方小于10000的数,相加等于n则ans++ #include<iostream> #include<cstdio> using namespace std; con ...

  6. [Swift通天遁地]一、超级工具-(7)创建一个图文并茂的笔记本程序

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. Android项目模块化遇到的问题

    1.问题背景 gradle 4 MacOs 10.14.3 Android Studio 3 在android模块化的时候,例如,有两个模块,一个是usercenter,另一个是common. 其中u ...

  8. Snackbar:用它来替换Toast 显示短提示

    简介 Snackbar 它是Toast的子类.主要用来提示短暂的提示信息后,然后它自动消失. 它寄生在普通view上,具有一些基本功能. 它寄生在 CoordinatorLayout 时,有以下两个特 ...

  9. 410 Split Array Largest Sum 分割数组的最大值

    给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小.注意:数组长度 n 满足以下条件:    1 ≤ n ≤ 1000 ...

  10. Selenium Grid操作使用指南

    一.实现串行多浏览器执行脚本1.启动selenium-server-standalonejava -jar selenium-server-standalone-2.44.0.jar2.脚本代码 fr ...