Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5809    Accepted Submission(s): 1911

Problem Description
There is a sequence of integers. Your task is to find the longest subsequence that satisfies the following condition: the difference between the maximum element and the minimum element of the subsequence is no smaller than m and no larger than k.
 
Input
There are multiple test cases.
For each test case, the first line has three integers, n, m and k. n is the length of the sequence and is in the range [1, 100000]. m and k are in the range [0, 1000000]. The second line has n integers, which are all in the range [0, 1000000].
Proceed to the end of file.
 
Output
For each test case, print the length of the subsequence on a single line.
 
Sample Input

5 0 0 1 1 1 1 1 5 0 3 1 2 3 4 5
 
Sample Output

5 4
 
Source
 
 
题意:
求n个数里面最长的一段,其最大值减最小值的差>=m && <= k。
思路:
维护2个单调队列。一个存最大值,一个存最小值。
 
/*
* Author: sweat123
* Created Time: 2016/7/12 9:09:45
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
int a[MAXN];
deque<int>q1,q2;
int n,m,k;
int main(){
while(~scanf("%d%d%d",&n,&m,&k)){
q1.clear();
q2.clear();
for(int i = ; i <= n; i++){
scanf("%d",&a[i]);
}
int ans = ;
int bf = ;
for(int i = ; i <= n; i++){
while(!q1.empty() && a[q1.back()] < a[i]){
q1.pop_back();
}
while(!q2.empty() && a[q2.back()] > a[i]){
q2.pop_back();
}
q1.push_back(i);
q2.push_back(i);
while(!q1.empty() && !q2.empty() && a[q1.front()] - a[q2.front()] > k){
if(q1.front() < q2.front()){
bf = q1.front();
q1.pop_front();
} else if(q1.front() > q2.front()){
bf = q2.front();
q2.pop_front();
} else {
bf = q1.front();
q1.pop_front();
q2.pop_front();
}
}
if(!q1.empty() && !q2.empty() && a[q1.front()] - a[q2.front()] >= m){
ans = max(ans,i - bf);
}
}
printf("%d\n",ans);
}
return ;
}

hdu3530 单调队列的更多相关文章

  1. Subsequence(HDU3530+单调队列)

    题目链接 传送门 题面 题意 找到最长的一个区间,使得这个区间内的最大值减最小值在\([m,k]\)中. 思路 我们用两个单调队列分别维护最大值和最小值,我们记作\(q1\)和\(q2\). 如果\( ...

  2. hdu3530 双单调队列的维护

    单调队列有部分堆的功能,但其只能维护给定区间中比v大的值或者比v小的值,且其一般存储元素的下标. 思路:两个单调队列维护最大值与最小值的下标,如果区间的最大值最小值之差大于给定范围,则选择队首靠左的删 ...

  3. [hdu3530]Subsequence (单调队列)

    题意:求在一段序列中满足m<=max-min<=k的最大长度. 解题关键:单调队列+dp,维护前缀序列的最大最小值,一旦大于k,则移动左端点,取max即可. #include<cst ...

  4. 【专题系列】单调队列优化DP

    Tip:还有很多更有深度的题目,这里不再给出,只给了几道基本的题目(本来想继续更的,但是现在做的题目不是这一块内容,以后有空可能会继续补上) 单调队列——看起来就是很高级的玩意儿,显然是个队列,而且其 ...

  5. BestCoder Round #89 B题---Fxx and game(单调队列)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945     问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路:  B ...

  6. 单调队列 && 斜率优化dp 专题

    首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...

  7. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

  8. BZOJ 1047 二维单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...

  9. 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列

    第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...

随机推荐

  1. iOS之百度导航SDK的坐标转换

    百度导航 iOS SDK的坐标转换代码示例,有需要的朋友可以参考下. //导航坐标--------------> 地图坐标 //假设从导航sdk取到了一个点坐标是(116.304847, 40. ...

  2. Linux0.11内核--进程调度分析之1.初始化

    [版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5596746.html ] 首先看main.c里的初始化函数main函数里面有个函数是对进程调度 ...

  3. C++实现Ping

    这是一个老话题了,但是我刚学会... 我们的目的是实现这么个东西: 之所以用红框框一下是因为,从baidu.com到123.125.114.144的过程是DNS解析,我们暂时先实现ping的部分. 基 ...

  4. SQL SERVER 属性OWNER不可用于数据库xxx。该对象可能没有此属性,也可能是访问权限不足而无法检索。

    今天遇到一个案例:右键单击数据库的属性时出现下面错误提示: 属性Owner不可用于数据库xxx,该对象可能没有此属性,也可能是访问权限不足而无法检索. 使用脚本查看该数据库的Owner时发现Owner ...

  5. js中操作数组的一些方法

    增 push   在数组的末尾添加一个或多个元素,并返回新的长度.  array.push(1,2,3.........) unshift  在数组的开头添加一个或多个元素,并返回新的长度. arra ...

  6. MongoDB学习笔记~为IMongoRepository接口更新指定字段

    回到目录 对于MongoDB来说,它的更新建议是对指定字段来说的,即不是把对象里的所有字段都进行update,而是按需去更新,这在性能上是最优的,这当然也是非常容易理解的,我们今天要实现的就是这种按需 ...

  7. mvn常用命令

    1. mvn compile 编译源代码 2. mvn test-compile 编译测试代码 3. mvn test 运行测试 4. mvn package 打包,根据pom.xml打成war或ja ...

  8. Linux初学:(二)Shell环境与命令基础

    博客园(FOREVER_ENJOY):http://www.cnblogs.com/zyx1314/ 本文版权归作者所有:欢迎转载!请注明文章作者和原文连接 Shell是什么? 1. Shell作为应 ...

  9. [django]windows下用Django,静态文件请求失败,出现UnicodeDecodeError

    问题:windows下用Django,静态文件请求失败,出现UnicodeDecodeError:'utf-8' codec can't decode byte 0xb0 in position 1: ...

  10. Azure HDInsight 和 Spark 大数据实战(二)

    HDInsight cluster on Linux 登录 Azure portal (https://manage.windowsazure.com ) 点击左下角的 NEW 按钮,然后点击 DAT ...