题目链接: 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. POJ 2253 Frogger Floyd

    原题链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  2. 期望DP初步

    感觉期望DP这种东西像是玄学- 主要总结说一点基础性的东西, 或许对于理解题目的做法会有一点帮助. 首先是关于独立事件, 互斥事件的概念. 通俗地说, 就是对于两个事件A, B, 假如满足发生了其中一 ...

  3. Microsoft SQL Server 2005技术内幕:存储引擎笔记

    http://www.cnblogs.com/lyhabc/articles/3942053.html

  4. openfire Android学习---android客户端聊天开发之登录 和 注销登录

    一切就绪,新建一个android测试工程: 上网权限配置,界面绘制啥的,这里就不说了. 首先 导入一个smark包.这个是用来维护长连接的,也可以是asmark.我用的是asmark 先普及一些基本知 ...

  5. android -- 存储byte

    public static String byteArrayToHexStr(byte[] byteArray) { if (byteArray == null){ return null; } ch ...

  6. es6 对象浅拷贝的2种方法

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  7. Java数据库访问技术

    1.Java集合: Collection Map List: 位于 java.util包中. Arraylist 顺序结构.Linkedlist 链表结构 // List<NewsClassif ...

  8. 4.php整合Memcached

    用法: nginx响应请求时,直接请求memcached, 如果没有相应的内容,再回调PHP页面,去查询database,并写入memcached. 分析: memcached是k/v存储, key- ...

  9. python正则方法

    通过正则替换字符串 res=re.sub(正则,newString,srcString)//返回替换后的字符串 res,m=res.subn(正则,newString,srcString)//返回替换 ...

  10. adb命令具体解释(二)——手机缺失sqlite3时操作数据库的多种解决方式

    在android应用开发无处不在SQLite数据库的身影.那么在开发中怎么使用adb命令操作数据库的功能呢? 以下我们将完整的介绍与数据库操作相关的命令集及当手机缺少sqlite3的时候的多种解决方式 ...