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. git 密钥

    为什么配置SHH呢?是为了方便我们剪切代码的时间免密码输入,特别方便如何配置呢? 首先安装git: 先到官网下载:官网下载git 然后安装后在桌面任意空白处右击,选择Git Base Here即可如下 ...

  2. JAVA高级特性--自动拆箱-装箱,枚举类型

    基本数据类型转换为引用类型对象 一个自动装箱的例子 Integer i=10; 相当于 Integer i=new Integer(10); 一个自动拆箱的例子 Integer m=10; int n ...

  3. HTTP参考

    HTTP参考 一.HTTP码应码响应码由三位十进制数字组成,它们出现在由HTTP服务器发送的响应的第一行. 响应码分五种类型,由它们的第一位数字表示: 1.1xx:信息,请求收到,继续处理 2.2xx ...

  4. sql select时增加常量列

    阅读更多 string sql="select a,b,'常量' as c from table" 注:单引号' ' 很重要,否则编译时会把其看成查询参数,从而提示参数未指定错误. ...

  5. XAML 很少人知道的科技 - walterlv

    原文:XAML 很少人知道的科技 - walterlv XAML 很少人知道的科技 发布于 2019-04-30 02:30 更新于 2019-04-30 11:08 本文介绍不那么常见的 XAML ...

  6. python 字典索引

  7. @loj - 2093@ 「ZJOI2016」线段树

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 小 Yuuka 遇到了一个题目:有一个序列 a1,a2,..., ...

  8. jq 监听返回事件

    <script> $(document).ready(function(e) {             var counter = 0;            if (window.hi ...

  9. @codeforces - 1106F@ Lunar New Year and a Recursive Sequence

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 定义递推数列 f: (1)f[1] = f[2] = ... f ...

  10. Mysql到Java数据类型映射的JDBC规范