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. android 性能分析案例

    本章以实际案例分析在android开发中,性能方面的优化和处理.设计到知识点有弱引用,memory monitor,Allocation Tracker和leakcanary插件. 1.测试demo ...

  2. SDWebImage原理及使用(转)

    转自http://www.cnblogs.com/jys509/p/5199997.html SDWebImage托管在github上.https://github.com/rs/SDWebImage ...

  3. linux 学习随笔-shell简单编写

    脚本最好都放在/usr/local/sbin中 脚本的执行 sh -x 脚本.sh -x可以查看执行过程 1在脚本中使用变量 使用变量的时候,需要使用$符号:  #!/bin/bash  ##把命令赋 ...

  4. 深圳 Maker Faire 2016 & Microsoft Booth

    首先,感谢Hackster.io和微软,因为发表在Hackster.io的项目<A fall detection system based on Arduino, Windows and Azu ...

  5. SQL SERVER修改排序规则——脚本篇

    在上篇MS SQL 排序规则总结中,大致就数据库服务器排序规则(或者叫数据库实例排序规则).数据库排序规则.列的排序规则粗浅的叙说了一遍,重点讲述了修改数据库服务器排序规则(数据库实例排序规则),其中 ...

  6. sublime3 集成angularJs插件

    sublime是web开发中一款轻量级高效编辑器,十分适合前端开发(安装sublime是需要注册的,请支持正版) 1.安装sublime3(http://www.sublimetext.com/3) ...

  7. stl之截取:以一段字符串截取字符串

    string dforder = line.substr(0,line.find("\t")).c_str(); 解析: line为传进来的string类型 substr查找第0位 ...

  8. iOS视图弹出、平移、旋转、翻转、剪切等变换效果实现

    效果图: 1.定义属性 @property (nonatomic, strong) UIView *transformView;//发生变换的试图 @property (nonatomic, stro ...

  9. Ubuntu配置Ruby和Rails

    安装curl sudo apt-get install curl 安装RVM curl -L https://get.rvm.io | bash -s stable 通过RVM来安装Ruby rvm ...

  10. [原]经典bootstrap模态框使用文章

    1,Bootstrap 模态对话框和简单使用 <div id="myModal" class="modal hide fade"> <div ...