题目链接: 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. Ext grid中单元格编辑,editor为combobox时用法

    { header: 'TO_PTN_NM', dataIndex: "TO_PTN_NM", sortable: true, renderer: function (v, m, r ...

  2. Android中的动画总结

    文章主要内容来源<Android开发艺术探索>,部分内容来源网上的文章,文中会有链接. Android系统提供了两个动画框架:属性动画框架和View动画框架. 两个动画框架都是可行的选项, ...

  3. 用hashmap实现自己的缓存

    @SuppressWarnings({"unchecked", "rawtypes"})public class DefaultCache implements ...

  4. ios中表示private

    在.m中写成 如下形式既为私有的形式 @interface ViewController ()  这里只是声明类名和括号即可 /////方法等 @end

  5. n个平面把空间最多分成几个部分?

    题目: n个平面把空间最多分成几个部分? 解答: 1条直线可以把平面分成2部分,2条直线最多可以把平面分成4部分, 3条直线最多可以把平面分成几部分,4条直线呢?你能不能想出n条直线最多可以把平面分成 ...

  6. jquery的ajax用法

    api参见:http://api.jquery.com/jquery.ajax/

  7. [反汇编练习] 160个CrackMe之033

    [反汇编练习] 160个CrackMe之033. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  8. 编译php ./configure命令enable和with有什么区别

    原文: https://segmentfault.com/q/1010000009174725 ---------------------------------------------------- ...

  9. vue2.0 + vux 项目搭建

    1.快速搭建项目模板 因为项目使用vux,所以推荐使用vux官网的airyland/vux2 模板,vue-cli工具是vue项目的搭建脚手架 默认为 webpack2 模板,如果你需要使用webpa ...

  10. 笔记08 WPF导航

    如何在winform中做导航,如何重定向界面,就产生了争执. 是用window还是Page还是UserControl? 先不管用啥.我们先比较一下各自的优缺点. 在WPF中使用导航,内容被组织在Pag ...