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
  1.  
5 0 0 1 1 1 1 1 5 0 3 1 2 3 4 5
 
Sample Output
  1.  
5 4
 
Source
 
 
题意:
求n个数里面最长的一段,其最大值减最小值的差>=m && <= k。
思路:
维护2个单调队列。一个存最大值,一个存最小值。
 
  1. /*
  2. * Author: sweat123
  3. * Created Time: 2016/7/12 9:09:45
  4. * File Name: main.cpp
  5. */
  6. #include<set>
  7. #include<map>
  8. #include<queue>
  9. #include<stack>
  10. #include<cmath>
  11. #include<string>
  12. #include<vector>
  13. #include<cstdio>
  14. #include<time.h>
  15. #include<cstring>
  16. #include<iostream>
  17. #include<algorithm>
  18. #define INF 1<<30
  19. #define MOD 1000000007
  20. #define ll long long
  21. #define lson l,m,rt<<1
  22. #define key_value ch[ch[root][1]][0]
  23. #define rson m+1,r,rt<<1|1
  24. #define pi acos(-1.0)
  25. using namespace std;
  26. const int MAXN = ;
  27. int a[MAXN];
  28. deque<int>q1,q2;
  29. int n,m,k;
  30. int main(){
  31. while(~scanf("%d%d%d",&n,&m,&k)){
  32. q1.clear();
  33. q2.clear();
  34. for(int i = ; i <= n; i++){
  35. scanf("%d",&a[i]);
  36. }
  37. int ans = ;
  38. int bf = ;
  39. for(int i = ; i <= n; i++){
  40. while(!q1.empty() && a[q1.back()] < a[i]){
  41. q1.pop_back();
  42. }
  43. while(!q2.empty() && a[q2.back()] > a[i]){
  44. q2.pop_back();
  45. }
  46. q1.push_back(i);
  47. q2.push_back(i);
  48. while(!q1.empty() && !q2.empty() && a[q1.front()] - a[q2.front()] > k){
  49. if(q1.front() < q2.front()){
  50. bf = q1.front();
  51. q1.pop_front();
  52. } else if(q1.front() > q2.front()){
  53. bf = q2.front();
  54. q2.pop_front();
  55. } else {
  56. bf = q1.front();
  57. q1.pop_front();
  58. q2.pop_front();
  59. }
  60. }
  61. if(!q1.empty() && !q2.empty() && a[q1.front()] - a[q2.front()] >= m){
  62. ans = max(ans,i - bf);
  63. }
  64. }
  65. printf("%d\n",ans);
  66. }
  67. return ;
  68. }

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. Linux0.11内核--fork进程分析

    [版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5597818.html ] 据说安卓应用里通过fork子进程的方式可以防止应用被杀,大概原理就是 ...

  2. 谈谈Fragment中的onActivityResult

    大家或许有遇到这个神坑,在Fragment中使用startActivityForResult能够成功,可是在Fragment中的onActivityResult却无法被调用.一不注意就让人一夜愁白了头 ...

  3. centos6.5和centos7如何搭建php环境

    作者:白狼 出处:http://www.manks.top/linux_php.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责 ...

  4. .NET应用程序调试—原理、工具、方法

    阅读目录: 1.背景介绍 2.基本原理(Windows调试工具箱..NET调试扩展SOS.DLL.SOSEX.DLL) 2.1.Windows调试工具箱 2.2..NET调试扩展包,SOS.DLL.S ...

  5. Windows 64位下装Oracle 11g,PLSQL Developer的配置问题,数据库处显示为空白的解决方案

    安装pl sql 后,若下图的数据库处为空.则需要安装32位的客户端,说明pl sql不支持64位客户端连接. 解决办法:  1.下载32位Oracle客户端,并安装 2.设置PLSQL Develo ...

  6. Linux IPC socket 广播,组播

    getsockopt()/setsockopt() //获得sockfd指向的socket的属性 int getsockopt(int sockfd, int level, int optname, ...

  7. C++的友元类和友元函数实例

    #include <math.h> #include<iostream> using namespace std; class Point { public: Point(do ...

  8. android 实现点击listview 空白地方隐藏菜单

    思路:重写ListView的setOnTouchListener事件: ListView.setOnTouchListener(new OnTouchListener(){ @Override pub ...

  9. Android开机启动程序

    android程序实现开机启动的原理,简单点说就是做一个广播接收器,接收到开机广播时就启动activity或service或执行其它操作.Android系统在启动的时候会发出一个开机广播,内容为ACT ...

  10. linux 文件系统结构及命令

    1.linux 文件系统结构 / 根目录 root |--mnt/ | |--sdcard/  挂载点 | |--usb0 | |--cdrom |--home | |--soft01 <- 用 ...