题目链接: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1553

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

题解:

之前学了RMQ,线段树, 树状数组,但是一直不知道他们在哪里能派上用场。通过这题,终于找到他们的用武之地了:区间查询最大最小值。

解决了查询区间最大最小值的问题,剩下的就是二分了。这里是二分长度。


RMQ:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
#define LNF (1<<60)
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = 1e9+; LL st_max[maxn][], st_min[maxn][];
LL a[maxn]; void RMQ_init(int n)
{
for(int i = ; i<n; i++)
{
st_min[i][] = a[i];
st_max[i][] = a[i];
} for(int j = ; (<<j)<=n; j++)//枚举长度
for(int i = ; i+(<<j)-<n; i++)//枚举起点
{
st_min[i][j] = min(st_min[i][j-],st_min[i+(<<(j-))][j-]);
st_max[i][j] = max(st_max[i][j-],st_max[i+(<<(j-))][j-]);
}
} LL RMQ(int l, int r)//查询
{
int k = ;
while((<<(k+))<=r-l+)
k++;
return max(st_max[l][k],st_max[r-(<<k)+][k]) - min(st_min[l][k],st_min[r-(<<k)+][k]);
} int test(int len, int n, int k)
{
for(int i = len-; i<n; i++)
if(RMQ(i-len+, i)<=1LL*k)
return ; return ;
} int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL int n, k;
while(~scanf("%d%d", &n, &k))
{
for(int i=;i<n;i++)
scanf("%lld", &a[i]); ms(st_max, );
ms(st_min, );
RMQ_init(n); int l = , r = n;
while(l<=r)
{
int mid = (l+r)/;
if(test(mid, n, k))
l = mid+;
else
r = mid-;
}
printf("%d\n", r);
}
return ;
}

线段树:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
#define LNF 1000000000000
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = 1e9+; LL st_max[*maxn], st_min[*maxn];
LL a[maxn]; void build(int rt, int l, int r)
{
if(l==r)
{
st_max[rt] = a[r];
st_min[rt] = a[r];
return;
} int mid = (l+r)>>;
build(rt*,l,mid);
build(rt*+,mid+,r);
st_max[rt] = max(st_max[rt*], st_max[rt*+]);
st_min[rt] = min(st_min[rt*], st_min[rt*+]);
} //由于最大最小值都要查询,而return只能返回一个,所以用ma和mi记录最小值
LL query(int rt, int l, int r, int x, int y, LL &ma, LL &mi)
{
if(x<=l && y>=r)
{
ma = max(ma,st_max[rt]);
mi = min(mi,st_min[rt]);
return ma - mi;
} int mid = (l+r)>>;
if(y<=mid) query(rt*,l,mid,x,y,ma,mi);
else if(x>=mid+) query(rt*+,mid+,r,x,y,ma,mi);
else query(rt*,l,mid,x,mid,ma,mi),query(rt*+,mid+,r,mid+,y,ma,mi); return ma - mi;
} int test(int len, int n, int k)
{
for(int i = len-; i<n; i++)
{
LL ma = -LNF, mi = LNF;
if(query(,,n-, i-len+, i, ma,mi)<=1LL*k)
return ;
}
return ;
} int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL int n, k;
while(scanf("%d%d", &n, &k)!=EOF)
{
for(int i=;i<n;i++)
scanf("%lld", &a[i]); build(,,n-); int l = , r = n;
while(l<=r)
{
int mid = (l+r)/;
if(test(mid, n,k))
l = mid+;
else
r = mid-;
}
printf("%d\n", r);
}
return ;
}

CSU1553 Good subsequence —— 二分 + RMQ/线段树的更多相关文章

  1. HDU 5558 Alice's Classified Message(后缀数组+二分+rmq(+线段树?))

    题意 大概就是给你一个串,对于每个\(i\),在\([1,i-1]\)中找到一个\(j\),使得\(lcp(i,j)\)最长,若有多个最大\(j\)选最小,求\(j\)和这个\(lcp\)长度 思路 ...

  2. NBU 2475 Survivors(RMQ线段树)

    NBU 2475Survivors 题目链接:http://acm.nbu.edu.cn/v1.0/Problems/Problem.php?pid=2475 题意:给定n个人,每个人有strengt ...

  3. [BZOJ4552][TJOI2016&&HEOI2016]排序(二分答案+线段树/线段树分裂与合并)

    解法一:二分答案+线段树 首先我们知道,对于一个01序列排序,用线段树维护的话可以做到单次排序复杂度仅为log级别. 这道题只有一个询问,所以离线没有意义,而一个询问让我们很自然的想到二分答案.先二分 ...

  4. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  5. YbtOJ#463-序列划分【二分答案,线段树,dp】

    正题 题目链接:https://www.ybtoj.com.cn/problem/463 题目大意 给出长度为\(n\)的序列\(A,B\).要求划分成若干段满足 对于任何\(i<j\),若\( ...

  6. UESTC 764 失落的圣诞节 --RMQ/线段树

    题意:n种物品,每种物品对不同的人都有不同的价值,有三个人选,第一个为普通学生,第二个是集,第三个是祈,集和祈可以选一样的,并且还会获得加分,集和祈选的普通学生都不能选,问三个人怎样选才能使总分最高. ...

  7. [RMQ] [线段树] POJ 3368 Frequent Values

    一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...

  8. Special Subsequence(离散化线段树+dp)

    Special Subsequence Time Limit: 5 Seconds      Memory Limit: 32768 KB There a sequence S with n inte ...

  9. VJ16216/RMQ/线段树单点更新

    题目链接 /* 单点更新,用RMQ维护最大值,add对c[i]修改,或加,或减. 求[l,r]的和,用sum(r)-sum(l-1).即可. */ #include<cmath> #inc ...

随机推荐

  1. 【mybatis】mybatis中insert操作,返回自增id

    需求是这样的: mybatis中insert操作,返回自增id,因为这个自增id需要给后续业务用到. 原本是这样的: 将insert语句传入,正常执行insert操作,返回int永远是 0[失败] 或 ...

  2. Eclipse 安装(Oxygen版本)

    Eclipse 安装(Oxygen版本) Eclipse 最新版本 Eclipse Neon,这个首次鼓励用户使用 Eclipse Installer 来做安装,这是一种由Eclipse Oomph提 ...

  3. android:使用gallery和imageSwitch制作可左右循环滑动的图片浏览器

    为了使图片浏览器左右无限循环滑动 我们要自己定义gallery的adapter 假设要想自己定义adapter首先要了解这几个方法 @Override public int getCount() { ...

  4. python(31)- 模块练习

    1. 小程序:根据用户输入选择可以完成以下功能:     创意文件,如果路径不存在,创建文件夹后再创建文件     能够查看当前路径     在当前目录及其所有子目录下查找文件名包含指定字符串的文件 ...

  5. mysql: expire_logs_days设置后无效问题

    Sina blog - MySQL的 expire_logs_days 和 PURGE MASTER LOGS 无效问题

  6. linux 下weblogic启动和停止

    启动weblogic 本例中weblogic 安装路径为:/data/weblogic/wls/wlserver_10.3/ 1. 启动nodeManager cd /data/weblogic/wl ...

  7. VS2010配置QT5.5.0开发环境

    一.官网下载QT和qtvsaddin插件 网址:http://www.qt.io/download-open-source/ 1. 2. 3. 得到下载的安装包,点击安装就能够了 watermark/ ...

  8. cocos2dx3.2 学习笔记(2)--ActionManagerTest

    前面看完了 CppTests的基本框架及流程.如今准备看看详细的每一个Test了 从哪里開始看呢. 额,就第一个吧(ActionManagerTest) 首先看看效果吧,执行了下.发现有几种效果.看不 ...

  9. freescale-sdk linux移植一搭建编译环境脚本host-prepare.sh分析

    接下来使用自己的课外歇息时间,对基于PowerPC架构freescale-sdk,进行linux移植和分析.主要參考官方文档freescale linux sdk START_HERE.html,首先 ...

  10. teradata培训文档 相关索引

    teradata培训文档 http://wenku.baidu.com/view/ec44c201cc175527072208ba.html Teradata 和Greenplum 的讨论 http: ...