Good subsequence( RMQ+二分)
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+二分)的更多相关文章
- *HDU3486 RMQ+二分
Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...
- hdu 3486 Interviewe (RMQ+二分)
Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分
原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...
- HDU 5089 Assignment(rmq+二分 或 单调队列)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- 玲珑杯 Round 19 B Buildings (RMQ + 二分)
DESCRIPTION There are nn buildings lined up, and the height of the ii-th house is hihi. An inteval [ ...
- codeforces 487B B. Strip(RMQ+二分+dp)
题目链接: B. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- CodeForces 689D Friends and Subsequences (RMQ+二分)
Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...
- HDU 5726 GCD (RMQ + 二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...
随机推荐
- 转】MyEclipse使用总结——在MyEclipse中设置jsp页面为默认utf-8编码
原博文出自于:http://www.cnblogs.com/xdp-gacl/p/3496161.html 感谢! 在MyEclispe中创建Jsp页面,Jsp页面的默认编码是"ISO-88 ...
- Multimedia&Network
[Multimedia&Network] 1.Unity3D共支持4种格式音乐文件: 2.AudioSource用于指明音频源,被绑定在一个GameObject身上.光有AudioSource ...
- 关于iTunes11.1 不能刷自制固件的解决方案
由于iTunes升级到11.1后, 苹果在程序里限制了不允许刷自制固件, 所以想刷自制固件暂时只能降级iTunes到11.1版本之前, 这里提供iTunes 11.0.5 的下载地址: Windows ...
- Telnet连接Win7系统被拒绝的原因及解决方法
有时要与计算机进行远程连接,会用到telnet.win7中telnet默认是没有开启的,所以这时连接会连接失败,其失败提示如下: 正在连接192.168.100.103...无法打开到主机的连接. 在 ...
- 关于JPA封装数据库数据到实体不调用属性的get和set的方法解决办法
今天发现JPA封装数据库数据到实体并不调用属性的get和set的,郁闷,本来想在set方法做改字段的值处理的谁知道遇到这个情况: @Column(name = acode) @Access(value ...
- Jmeter_初步认识随笔
1. 简介 Apache JMeter是100%纯java桌面应用程序,被设计用来测试客户端/服务器结构的软件(例如web应用程序).它可以用来测试包括基于静态和动态资源程序的性能,例如静态文件,Ja ...
- UserControl和CustomControl基础【PluraSight】
UserControl UserControl实际上就是ContentControl,xaml里<UserControl></UserControl>tag之间包含的实际就是后 ...
- 记录一点自己写的Php代码(1)取得任意种类,无限级下线
//获取所有下线 function get_all_heeler($user_id,$user_rank = 0){ $user_id_array = $user_id; while (true) { ...
- C# 绘制统计图(柱状图, 折线图, 扇形图)
统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的:这里我们用 ...
- 静默安装oracle11G
1.操作系统及Oracle版本 Linux版本:CentOS release 5.5 (Final) Oracle版本:Oracle Database 11g Release 2 (11.2.0.1. ...