Subsequence

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

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
 
题意:给出一个长度为n的数列,求一个最长的区间,使得区间中最小值和最大值的差在[m,k]之间 (来自vj)
思路:
维护两个单调队列,这里的单调队列并不是维护一个固定滑动窗口的值,而是有自己的出队规则。
两个单调队列,一个非增,一个非降。
按输入顺序开始更新两个单调队列,倘若当前的值会更新最小值,并且与目前单调队列的最大值的差大于k,那么就要舍弃这个最大值。显而易见,这个最大值之前的位置也一定不会是要求区间的左端点。
倘若当前的值会更新最大值,同理。
但是在代码中,并没有判断当前更新的是最大还是最小值。因为如果你更新的是最小值,维护最小值的单调队列首部位置就是当前位置,而维护最大值的单调队列首部位置一定小于等于当前位置,如果更新的是最大值同理。所以只需要去掉那个靠前的就行了。
用一个pre维护最后一个去掉的位置,这个位置的后一个位置就可能是要求区间的左端点。
 #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); struct node{
int num,pos;
};
int num[maxn];
deque<node>q1,q2; int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin); int n,m,k;
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
for(int i=;i<=n;i++){
scanf("%d",&num[i]);
}
int ans=;int pre=;
q1.clear();
q2.clear();
for(int i=;i<=n;i++){
while(!q1.empty()&&q1.back().num<num[i]){
q1.pop_back();
}while(!q2.empty()&&q2.back().num>num[i]){
q2.pop_back();
}
q1.push_back(node{num[i],i});
q2.push_back(node{num[i],i});
while(q1.front().num-q2.front().num>k){
if(q1.front().pos<q2.front().pos){
pre=q1.front().pos;
q1.pop_front();
}
else{
pre=q2.front().pos;
q2.pop_front();
}
}
if(q1.front().num-q2.front().num>=m){
ans=max(ans,i-pre);
}
}
printf("%d\n",ans);
} return ;
}
 

HDU - 3530 Subsequence (单调队列)的更多相关文章

  1. hdu 3530 Subsequence 单调队列

    题目链接 题目给出n个数, 一个下界m, 一个上界k, 让你求出最长的一段序列, 满足这段序列中的最大的数-最小的数<=k&&>=m, 输出这段长度. 可以维护两个队列, ...

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

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

  3. HDU 3530 Subsequence(单调队列)

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

  4. hdu 3530 Subsequence

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3530 Subsequence Description There is a sequence of i ...

  5. HDU 3401 Trade(单调队列优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:炒股.第i天买入一股的价钱api,卖出一股的价钱bpi,最多买入asi股,最多卖出bsi股 ...

  6. Hdu 3410 【单调队列】.cpp

    题意: 给出一个数组,问你对于第i个数,从最后一个比它大的数到它之间比它小的数中最大的那个数的下标,以及它右边到第一个比它大的数中比它小的数中最大的那一个数的下标<下标从1开始>. eg: ...

  7. HDU 5749 Colmerauer 单调队列+暴力贡献

    BestCoder Round #84   1003 分析:(先奉上zimpha巨官方题解) 感悟:看到题解单调队列,秒懂如何处理每个点的范围,但是题解的一句算贡献让我纠结半天 已知一个点的up,do ...

  8. HDU 5289 Assignment(单调队列)

    题意:给T足数据,然后每组一个n和k,表示n个数,k表示最大同意的能力差,接下来n个数表示n个人的能力,求能力差在k之内的区间有几个 分析:维护一个区间的最大值和最小值,使得他们的差小于k,于是採用单 ...

  9. hdu 3530 "Subsequence" (单调队列)

    传送门 题意: 给出一个序列,求最长的连续子序列,使得 m ≤ Max-Min ≤ k 我的理解: 定义数组 a[] 存储输入的 n 个数: 定义两个双端队列: deque<int >qM ...

随机推荐

  1. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十二章:几何着色器(The Geometry Shader)

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十二章:几何着色器(The Geometry Shader) 代码工 ...

  2. bzoj4152 The Captain

    Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. Input 第一行包含一个正整数n(2 ...

  3. php上传文件与图片到七牛的实例详解

    上传文件到七牛最简单的方式就是使用七牛官方最新的SDK 用composer安装PHP SDK composer require qiniu/php-sdk 上传文件到七牛 use Qiniu\Auth ...

  4. jQuery 图片高亮显示

    主要的jQuery代码: $(function() { $("ul li").hover(function() { // over //鼠标移入当前列透明度为1 其他列为0.5 $ ...

  5. DateFormat 日期格式化类(必须掌握)

    package com.sxt.utils.date1; import java.util.Date; import java.text.DateFormat; import java.text.Si ...

  6. @atcoder - AGC036E@ ABC String

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个仅由 A, B, C 组成的字符串 S. 求 S 的一个 ...

  7. poj 3159 Candies (dij + heap)

    3159 -- Candies 明明找的是差分约束,然后就找到这题不知道为什么是求1~n的最短路的题了.然后自己无聊写了一个heap,518ms通过. 代码如下: #include <cstdi ...

  8. Scoop 包管理工具 安装

    本人浏览器已经跨域 https://get.scoop.sh 访问该地址成功 安装前提(所需要的环境): 操作环境:win10. 确保你的 PowerShell 版本 >= 3. win7或许低 ...

  9. Python--day69--ORM多对多查询

    ManyToManyField class RelatedManager "关联管理器"是在一对多或者多对多的关联上下文中使用的管理器. 它存在于下面两种情况: 外键关系的反向查询 ...

  10. 不通过DataRow,直接往DataTable中添加新行DataTable.LoadDataRow(object[],bool)

    DataTable dtver = new DataTable();                dtver.Columns.Add("VERSION");            ...