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
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
定义D序列:序列里相邻元素差的绝对值不超过d。
题意:给定n个元素组成的序列a[],求最长的D序列。
思路:考虑升序排列的序列,那么则有dp[i] = max(dp[j]) (l <= j <= r)。其中a[l]为大于或者等于a[i]-d的第一个数,a[r]为小于或者等于a[i]+d的最后一个数。维护升序序列,并不丢失序列元素的下标,可以先将坐标离散化。考虑对当前元素a[i]的处理,我们只需要处理出a[l] >= a[i] - d,a[r] <= a[i] + d以及a[i]在离散化序列中的下标L, R, P,用线段树维护[L, R]区间的最大值,更新到P对应的区间上。只要在这个过程中维护序列长度即可。
偷懒用了C++lower_boundWA了两次。还是自己手写好。。。
AC代码:
#include
#include
#include
#include
#include
#include
#include
#include