CSU1553 Good subsequence —— 二分 + RMQ/线段树
题目链接: 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/线段树的更多相关文章
- HDU 5558 Alice's Classified Message(后缀数组+二分+rmq(+线段树?))
题意 大概就是给你一个串,对于每个\(i\),在\([1,i-1]\)中找到一个\(j\),使得\(lcp(i,j)\)最长,若有多个最大\(j\)选最小,求\(j\)和这个\(lcp\)长度 思路 ...
- NBU 2475 Survivors(RMQ线段树)
NBU 2475Survivors 题目链接:http://acm.nbu.edu.cn/v1.0/Problems/Problem.php?pid=2475 题意:给定n个人,每个人有strengt ...
- [BZOJ4552][TJOI2016&&HEOI2016]排序(二分答案+线段树/线段树分裂与合并)
解法一:二分答案+线段树 首先我们知道,对于一个01序列排序,用线段树维护的话可以做到单次排序复杂度仅为log级别. 这道题只有一个询问,所以离线没有意义,而一个询问让我们很自然的想到二分答案.先二分 ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- YbtOJ#463-序列划分【二分答案,线段树,dp】
正题 题目链接:https://www.ybtoj.com.cn/problem/463 题目大意 给出长度为\(n\)的序列\(A,B\).要求划分成若干段满足 对于任何\(i<j\),若\( ...
- UESTC 764 失落的圣诞节 --RMQ/线段树
题意:n种物品,每种物品对不同的人都有不同的价值,有三个人选,第一个为普通学生,第二个是集,第三个是祈,集和祈可以选一样的,并且还会获得加分,集和祈选的普通学生都不能选,问三个人怎样选才能使总分最高. ...
- [RMQ] [线段树] POJ 3368 Frequent Values
一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...
- Special Subsequence(离散化线段树+dp)
Special Subsequence Time Limit: 5 Seconds Memory Limit: 32768 KB There a sequence S with n inte ...
- VJ16216/RMQ/线段树单点更新
题目链接 /* 单点更新,用RMQ维护最大值,add对c[i]修改,或加,或减. 求[l,r]的和,用sum(r)-sum(l-1).即可. */ #include<cmath> #inc ...
随机推荐
- 证书锁定Certificate Pinning技术
证书锁定Certificate Pinning技术 在中间人攻击中,攻击主机通常截断客户端和服务器的加密通信.攻击机以自己的证书替代服务器发给客户端的证书.通常,客户端不会验证该证书,直接接受该证 ...
- POJ 3041 Asteroids 二分图
原题连接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- man
Description n间房子高度不同,Man 要从最矮的房子按照高度顺序跳到最高的房子,你知道房子的顺序,以及Man一次最远可以跳多远,相邻的房子至少有1的距离,房子的宽不计,现在由你安排相邻房子 ...
- Android CrashHandler
package jason.android.utils; import android.content.Context; import android.content.pm.PackageInfo; ...
- 【GitHub】删除GitHub上的文件
想要删除已经提交上GitHub上的文件, 删除之后,如果这个文件夹下没有文件了,这个文件夹也会被删除! 并且在它的上层文件夹后面 有提示删除了这个文件的信息!!
- Nutch学习笔记二——抓取过程简析
在上篇学习笔记中http://www.cnblogs.com/huligong1234/p/3464371.html 主要记录Nutch安装及简单运行的过程. 笔记中 通过配置抓取地址http://b ...
- ffmpeg一些filter使用方法、以及一些功能命令
1.加字幕 命令:ffmpeg -i <input> -filter_complex subtitles=filename=<SubtitleName>-y <outpu ...
- Path SumI、II——给出一个数,从根到子的和等于它
I.Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up a ...
- UVA 11578 - Situp Benches(dp)
题目链接:11578 - Situp Benches 题意:健♂身♂房有两个仰卧起坐坐垫,每次调整角度要花费10元/10度,每次使用要花费15,如今给定n个人的时间顺序,和所希望的角度,求最少花费 思 ...
- 利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model
利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model 使用场景:网站配置项目,为了便于管理,网站有几个Model类来管理配置文件, 比如ConfigWebsiteMo ...