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

  1. 5 2
  2. 5 4 2 3 1
  3. 1 1
  4. 1

Sample Output

  1. 3
  2. 1
  3.  
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <stack>
  12. #include <sstream>
  13. #include <iomanip>
  14. using namespace std;
  15. const int INF=0x4fffffff;
  16. const int EXP=1e-;
  17. const int MS=;
  18. const int MS2=;
  19.  
  20. int a[MS];
  21. int n,k;
  22.  
  23. int minv[MS][];
  24. int maxv[MS][];
  25.  
  26. void RMQ_init()
  27. {
  28. for(int i=;i<n;i++)
  29. {
  30. minv[i][]=a[i];
  31. maxv[i][]=a[i];
  32. }
  33.  
  34. for(int j=;(<<j)<=n;j++)
  35. {
  36. for(int i=;i+(<<j)-<n;i++)
  37. {
  38. minv[i][j]=min(minv[i][j-],minv[i+(<<(j-))][j-]);
  39. maxv[i][j]=max(maxv[i][j-],maxv[i+(<<(j-))][j-]);
  40. }
  41. }
  42. }
  43.  
  44. int RMQ(int l,int r)
  45. {
  46. int k=;
  47. while(<<(k+)<=r-l+)
  48. k++;
  49. return max(maxv[l][k],maxv[r-(<<k)+][k])-min(minv[l][k],minv[r-(<<k)+][k]);
  50. }
  51.  
  52. int main()
  53. {
  54.  
  55. while(scanf("%d%d",&n,&k)!=EOF)
  56. {
  57. for(int i=;i<n;i++)
  58. scanf("%d",&a[i]);
  59. RMQ_init();
  60. int ans=;
  61. for(int i=;i<n;i++)
  62. {
  63. int l=i;
  64. int r=n-;
  65. while(l<=r)
  66. {
  67. int mid=(l+r)/;
  68. int t=RMQ(i,mid);
  69. if(t>k)
  70. r=mid-;
  71. else
  72. l=mid+;
  73. }
  74. if(ans<l-i)
  75. ans=l-i;
  76. }
  77. cout<<ans<<endl;
  78. }
  79. return ;
  80. }
  1.  

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. 转】MyEclipse使用总结——在MyEclipse中设置jsp页面为默认utf-8编码

    原博文出自于:http://www.cnblogs.com/xdp-gacl/p/3496161.html 感谢! 在MyEclispe中创建Jsp页面,Jsp页面的默认编码是"ISO-88 ...

  2. Multimedia&Network

    [Multimedia&Network] 1.Unity3D共支持4种格式音乐文件: 2.AudioSource用于指明音频源,被绑定在一个GameObject身上.光有AudioSource ...

  3. 关于iTunes11.1 不能刷自制固件的解决方案

    由于iTunes升级到11.1后, 苹果在程序里限制了不允许刷自制固件, 所以想刷自制固件暂时只能降级iTunes到11.1版本之前, 这里提供iTunes 11.0.5 的下载地址: Windows ...

  4. Telnet连接Win7系统被拒绝的原因及解决方法

    有时要与计算机进行远程连接,会用到telnet.win7中telnet默认是没有开启的,所以这时连接会连接失败,其失败提示如下: 正在连接192.168.100.103...无法打开到主机的连接. 在 ...

  5. 关于JPA封装数据库数据到实体不调用属性的get和set的方法解决办法

    今天发现JPA封装数据库数据到实体并不调用属性的get和set的,郁闷,本来想在set方法做改字段的值处理的谁知道遇到这个情况: @Column(name = acode) @Access(value ...

  6. Jmeter_初步认识随笔

    1. 简介 Apache JMeter是100%纯java桌面应用程序,被设计用来测试客户端/服务器结构的软件(例如web应用程序).它可以用来测试包括基于静态和动态资源程序的性能,例如静态文件,Ja ...

  7. UserControl和CustomControl基础【PluraSight】

    UserControl UserControl实际上就是ContentControl,xaml里<UserControl></UserControl>tag之间包含的实际就是后 ...

  8. 记录一点自己写的Php代码(1)取得任意种类,无限级下线

    //获取所有下线 function get_all_heeler($user_id,$user_rank = 0){ $user_id_array = $user_id; while (true) { ...

  9. C# 绘制统计图(柱状图, 折线图, 扇形图)

    统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的:这里我们用 ...

  10. 静默安装oracle11G

    1.操作系统及Oracle版本 Linux版本:CentOS release 5.5 (Final) Oracle版本:Oracle Database 11g Release 2 (11.2.0.1. ...