ZOJ 3349 Special Subsequence
Special Subsequence
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
#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的更多相关文章
- ZOJ 3349 Special Subsequence 简单DP + 线段树
同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...
- Special Subsequence(离散化线段树+dp)
Special Subsequence Time Limit: 5 Seconds Memory Limit: 32768 KB There a sequence S with n inte ...
- ZOJ-3349 Special Subsequence 线段树优化DP
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3349 题意:给定一个数列,序列A是一个满足|Ai-Ai-1| & ...
- ZOJ 1733 Common Subsequence(LCS)
Common Subsequence Time Limit: 2 Seconds Memory Limit: 65536 KB A subsequence of a given sequen ...
- 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], ...
- zoj 3349 dp + 线段树优化
题目:给出一个序列,找出一个最长的子序列,相邻的两个数的差在d以内. /* 线段树优化dp dp[i]表示前i个数的最长为多少,则dp[i]=max(dp[j]+1) abs(a[i]-a[j])&l ...
- ZOJ 1301 The New Villa (BFS + 状态压缩)
题意:黑先生新买了一栋别墅,可是里面的电灯线路的连接是很混乱的(每个房间的开关可能控制其他房间,房间数<=10),有一天晚上他回家时发现所有的灯(除了他出发的房间)都是关闭的,而他想回卧室去休息 ...
- ZOJ Monthly, October 2010 ABEFI
ZOJ 3406 Another Very Easy Task #include <cstdio> #include <cstring> const int N = 10000 ...
- Soj题目分类
-----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...
随机推荐
- PCB 圆形板切边算法 实现
在工程CAM处理圆形拼板是个头疼的问题,需人工程师自行设计切边 满足可以拼板并且拼板后锣板板边没有内角,不然会影响装配 1.原始单 PCS圆形板 此外形如果不采用邮票孔连接的话,采V-CUT连接须采用 ...
- (DP)51NOD 1085 背包问题
在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数).求背包能够容纳的最大价值. Input 第1行,2个整数 ...
- Linux C编程 GCC的使用
本篇文章面向一些会linux文件操作与vim基本命令,编程大佬请移步勿喷. gcc是默认安装的,但是其还缺少常用的头文件和库文件,所以还需要build-essential这个包,可以在联网状态下使用如 ...
- 51nod1344 走格子
1344 走格子 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 有编号1-n的n个格子,机器人从1号格子顺序向后走,一直走到n号格子,并需要从n号格 ...
- VC++常见错误原因解析--error LNK2019: 无法解析的外部符号 "public: void __thiscall
根据个人遇到这个错误时的记录,原因可以分为一下几种: 原因一: 只是在.h里面声明了某个方法, 没有在cpp里面实现 . 具体讲,有时候在头文件中声明了需要的方法,确实忘记了在源文件中实现: 有时候在 ...
- 内存溢出及Jvm监控工具
内存泄露与内存溢出 内存溢出 out of memory,是指程序在申请内存时,没有足够的内存空间供其使用,出现out of memory. 内存泄露 memory leak,是指程序在申请内存后,无 ...
- re.S解析
转自:https://www.cnblogs.com/xieqiankun/p/re-sinpython.html 在Python的正则表达式中,有一个参数为re.S.它表示“.”(不包含外侧双引号, ...
- Mantis 配置与使用学习
转载自:http://blog.csdn.net/xifeijian/article/category/1429687
- 在中间层 .NET 应用程序中通过授权管理器使用基于角色的安全
基于角色的安全是从 Windows NT 的第一个版本开始在 Windows 平台上发展而来的.使用角色,操作系统可以通过检查称为 BUILTIN\Administrators 的组的安全上下文做出一 ...
- Git——基本操作
Shell 基本概念 shell俗称壳,为使用者提供使用界面,例如DOS下command以及后来的cmd.exe shell分类 图形界面shell,提供友好的可视化界面,例如windows操作界面, ...