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]) ...
随机推荐
- CentOS6.5(带图形安装)在使用过程中遇到的一些网络问题迷惑
比如,经常会遇到这样的问题************************** [root@SourceCompiler local]# pwd/usr/local[root@SourceCompil ...
- <<海闻电子发票接口 ESB 封装 代码指示 文档>>
<<海闻电子发票接口 ESB 封装 代码指示 文档>> isValid 是否有效标志 代码 中文 说明 true 成功 false 失败 code 海闻错误说明 代码 中文 ...
- mysql编码详解
在开发程序的时候,我们使用mysql数据库开发的时候,有时会碰到自己明明输入的是中文,为什么数据库中存储的就是???? 1.在配置Connection URL时,加上?useUnicode=true& ...
- POJ1014Dividing(DP)
http://poj.org/problem?id=1014 最简单之多重背包 #include <map> #include <set> #include <stack ...
- web.xml配置详解之listener与context-param
1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context- ...
- ASP.NET中使用 Response.Write("<script>alert('****');</script>");后CSS界面发生变化的解决方法 (经验证)
在后台使用Response.Write("<script>alert('Hello World');</script>);弹出alert窗口后发现网页的界面和原来CS ...
- Libvirt 虚拟化库剖析
讲到向外扩展计算(比如云计算),libvirt 可能是您从未听说过的最重要的库之一.libvirt 提供一种虚拟机监控程序不可知的 API 来安全管理运行于主机上的来宾操作系统.libvirt 本身不 ...
- 不需要JAVAScript完成分页查询功能
分页查询之前已经说过,现在用另一种方法实现,换汤不换药.但是更简单. view层代码: 控制层代码: 业务逻辑层,主要看一下方法count1()的代码: count1()方法的功能就是控制翻页,如果传 ...
- jQuery日历和日期选取插件
参考网站:http://developer.51cto.com/art/201103/248670.htm http://www.open-open.com/ajax/3_Calendar.htm 推 ...
- NAT类型与穿透 及 STUN TURN 协议
STUN : Simple Traversal of User Datagram Protocol [UDP] Through Network Address Translators [NATs] S ...