Subsequence

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

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
 

题解:

一个序列的最大值与最小值的差在m和k之间;

单调队列。

维护最大值和最小值,如果发现最大值和最小值的差大于k,那么就移动下标最靠前的队列。

注意如下数据:

5 2 4

2 1 5 2 2

应该用一个last标记上一个移动的位置,然后答案就是max{i-last},之前没有这个标记wa了一次。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
const int MAXN = ;
int num[MAXN];
int q1[MAXN], q2[MAXN];
int main(){
int n, m, k;
while(~scanf("%d%d%d", &n, &m, &k)){
for(int i = ; i < n; i++)
scanf("%d", num + i);
int h1 = , t1 = -, h2 = , t2 = -;
int ans = ;
int last = -;
for(int i = ; i < n; i++){
while(h1 <= t1 && num[q1[t1]] > num[i])
t1--;
while(h2 <= t2 && num[q2[t2]] < num[i])
t2--;
q1[++t1] = i;
q2[++t2] = i;
while(h1 <= t1 && h2 <= t2 && num[q2[h2]] - num[q1[h1]] > k){
if(q1[h1] < q2[h2])
last = q1[h1++];
else
last = q2[h2++];
}
if(h1 <= t1 && h2 <= t2 && num[q2[h2]] - num[q1[h1]] >= m){
ans = max(ans, i - last);
}
}
printf("%d\n", ans);
}
return ;
}

Subsequence(两个单调队列)的更多相关文章

  1. Subsequence(HDU3530+单调队列)

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

  2. HDU 3530 Subsequence(单调队列)

    传送门 Description There is a sequence of integers. Your task is to find the longest subsequence that s ...

  3. HDU - 3530 Subsequence (单调队列)

    Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 题目大意: 给一棵树,n个节点,每条边有个权值,从每个点i出发有个不经过自己走过的点的最远距离 ...

  5. 【单调队列+尺取】HDU 3530 Subsequence

    acm.hdu.edu.cn/showproblem.php?pid=3530 [题意] 给定一个长度为n的序列,问这个序列满足最大值和最小值的差在[m,k]的范围内的最长子区间是多长? [思路] 对 ...

  6. 求最长的任意两元素差不超过M的子段——双指针+单调队列hdu4123

    换根dp的部分比较容易,难点在于求求最长的任意两元素差不超过M的子段 首先会想到双指针维护(尺取法),如果p1,p2间的max-min>M,那么p1向右移动,直到p1,p2间的max-min&g ...

  7. URAL 1427. SMS(DP+单调队列)

    题目链接 我用的比较传统的办法...单调队列优化了一下,写的有点搓,不管怎样过了...两个单调队列,存两个东西,预处理一个标记数组存... #include <iostream> #inc ...

  8. POJ3162 Walking Race(树形DP+尺取法+单调队列)

    题目大概是给一棵n个结点边带权的树,记结点i到其他结点最远距离为d[i],问d数组构成的这个序列中满足其中最大值与最小值的差不超过m的连续子序列最长是多长. 各个结点到其他结点的最远距离可以用树形DP ...

  9. BZOJ 1047 理想的正方形(单调队列)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1047 题意:给出一个n*m的矩阵.在所有K*K的子矩阵中,最大最小差值最小的是多少? 思 ...

随机推荐

  1. Android : 如何在WebView显示的页面中查找内容

    Android : 如何在WebView显示的页面中查找内容 Author : Aoyousatuo Zhao http://blog.sina.com.cn/aoyousatuo WebView是A ...

  2. Linux备份

    Eking<longpeisky@vip.qq.com>  19:35:17 增量备份是针对于上一次备份(无论是哪种备份):备份上一次备份后,所有发生变化的文件. (增量备份过程中,只备份 ...

  3. Curl命令使用方法

    Curl是Linux下一个很强大的http命令行工具,其功能十分强大.1) 读取网页$ curl http://www.linuxidc.com2) 保存网页$ curl http://www.lin ...

  4. 使用css3属性,大部分浏览器要识别前缀

    例如以下代码的解析 -ms-transform:rotate(7deg); -moz-transform:rotate(7deg); -webkit-transform:rotate(7deg); - ...

  5. (转)C#之玩转反射

    前言 之所以要写这篇关于C#反射的随笔,起因有两个:   第一个是自己开发的网站需要用到   其次就是没看到这方面比较好的文章. 所以下定决心自己写一篇,废话不多说开始进入正题. 前期准备 在VS20 ...

  6. (转)第一天 XHTML CSS基础知识 文章出处:标准之路(http://www.aa25.cn/div_css/902.shtml)

    欢迎大家学习<十天学会web标准>,也就是我们常说的DIV+CSS.不过这里的DIV+CSS是一种错误的叫法,建议大家还是称之为web标准. 学习本系列教程需有一定html和css基础,也 ...

  7. UIView /  UIView的布局

    //! 一个视图可以有n个子视图,但是一个视图只能有一个父视图 struct CGRect {   CGPoint origin;   CGSize size; }; CGRectMake(CGFlo ...

  8. mercurial(Hg) Server 搭建 过程记录

    mercurial(Hg) Server 搭建 过程记录 1.  环境说明 只是测试搭建,环境为本机开发环境:win 8.1 + IIS8.5 软件准备: 2.  软件安装 先安装Python2.7, ...

  9. 1、shell 简介

    Shell 本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥 梁,用户的大部分工作都是通过Shell完成的.Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地 ...

  10. JS继承六大模式

    1.原型链 function SuperType(){this.property = true;} SuperType.prototype.getSuperValue = function(){ret ...