Description

Give you a sequence of n numbers, and a number k you should find the max length of Good subsequence. Good subsequence is a continuous subsequence of the given sequence and its maximum value - minimum value<=k. For example n=5, k=2, the sequence ={5, 4, 2, 3, 1}. The answer is 3, the good subsequence are {4, 2, 3} or {2, 3, 1}.

Input

There are several test cases.
Each test case contains two line. the first line are two numbers
indicates n and k (1<=n<=10,000, 1<=k<=1,000,000,000). The
second line give the sequence of n numbers a[i] (1<=i<=n,
1<=a[i]<=1,000,000,000).
The input will finish with the end of file.

Output

For each the case, output one integer indicates the answer.

Sample Input

5 2
5 4 2 3 1
1 1
1

Sample Output

3
1
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
using namespace std;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=;
const int MS2=; int a[MS];
int n,k; int minv[MS][];
int maxv[MS][]; void RMQ_init()
{
for(int i=;i<n;i++)
{
minv[i][]=a[i];
maxv[i][]=a[i];
} for(int j=;(<<j)<=n;j++)
{
for(int i=;i+(<<j)-<n;i++)
{
minv[i][j]=min(minv[i][j-],minv[i+(<<(j-))][j-]);
maxv[i][j]=max(maxv[i][j-],maxv[i+(<<(j-))][j-]);
}
}
} int RMQ(int l,int r)
{
int k=;
while(<<(k+)<=r-l+)
k++;
return max(maxv[l][k],maxv[r-(<<k)+][k])-min(minv[l][k],minv[r-(<<k)+][k]);
} int main()
{ while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=;i<n;i++)
scanf("%d",&a[i]);
RMQ_init();
int ans=;
for(int i=;i<n;i++)
{
int l=i;
int r=n-;
while(l<=r)
{
int mid=(l+r)/;
int t=RMQ(i,mid);
if(t>k)
r=mid-;
else
l=mid+;
}
if(ans<l-i)
ans=l-i;
}
cout<<ans<<endl;
}
return ;
}

Good subsequence( RMQ+二分)的更多相关文章

  1. *HDU3486 RMQ+二分

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...

  3. hdu 3486 Interviewe (RMQ+二分)

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...

  5. HDU 5089 Assignment(rmq+二分 或 单调队列)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  6. 玲珑杯 Round 19 B Buildings (RMQ + 二分)

    DESCRIPTION There are nn buildings lined up, and the height of the ii-th house is hihi. An inteval [ ...

  7. codeforces 487B B. Strip(RMQ+二分+dp)

    题目链接: B. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. CodeForces 689D Friends and Subsequences (RMQ+二分)

    Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...

  9. HDU 5726 GCD (RMQ + 二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...

随机推荐

  1. 【转】jsp页面中jstl标签详解

    原文地址: JSLT标签库,是日常开发经常使用的,也是众多标签中性能最好的.把常用的内容,放在这里备份一份,随用随查.尽量做到不用查,就可以随手就可以写出来.这算是Java程序员的基本功吧,一定要扎实 ...

  2. Determining Equality of Objects

    [Determining Equality of Objects] If you need to determine whether one object is the same as another ...

  3. RingBuffer源代码分析

    看到一篇写的非常详细的帖子,为防止楼主删帖后找不到,果断转载过来 RingBuffer源代码分析 出处:http://bbs.ickey.cn/community/forum.php?mod=view ...

  4. svn如何回滚到之前版本

    第一种情况:改动没有被提交(commit). 这种情况下,使用svn revert就能取消之前的修改. svn revert用法如下: # svn revert [-R] something 其中so ...

  5. Spring JTA应用JOTM & Atomikos II JOTM

    上节建立了一个简单的Java Application以及所需要的数据库和数据表,本节将介绍JOTM在Spring中的配置. JOTM(Java Open Transaction Manager)是Ob ...

  6. CentOS 安装 gcc

    centos linux默认可以采用yum方式安装,则采用如下命令安装gcc编译器即可:#yum -y install gcc 系统会自动安装gcc及依赖组件 gcc                 ...

  7. Windows x86 x64使用SetThreadContext注入shellcode的方式加载DLL

    一.前言 注入DLL的方式有很多,在R3就有远程线程CreateRemoteThread.SetWindowsHookEx.QueueUserApc.SetThreadContext 在R0可以使用a ...

  8. HTML to DOM

    Although you can now natively parse HTML using DOMParser and XMLHttpRequest, this is a new feature t ...

  9. 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换 [转]

    本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下. #ifndef USE_H_ #define USE_H_ # ...

  10. 使用Visual Leak Detector检测内存泄漏[转]

      1.初识Visual Leak Detector 灵活自由是C/C++语言的一大特色,而这也为C/C++程序员出了一个难题.当程序越来越复杂时,内存的管理也会变得越加复杂,稍有不慎就会出现内存问题 ...