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. Effective Modern C++:05右值引用、移动语义和完美转发

    移动语义使得编译器得以使用成本较低的移动操作,来代替成本较高的复制操作:完美转发使得人们可以撰写接收任意实参的函数模板,并将其转发到目标函数,目标函数会接收到与转发函数所接收到的完全相同的实参.右值引 ...

  2. C#中App.config文件配置获取

    最新的framework使用如下方法: using System.Configuration; ConfigurationManager.AppSettings["key"]; A ...

  3. Length of Last Word输出最后单词的字母个数

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  4. Selenium-------ActionChainApi接口详解

    ActionChains UI自动化测试过程中,经常遇到那种,需要鼠标悬浮后,要操作的才会元素出现的这种场景,那么我们就要模拟鼠标悬浮到某一个位置,做一系列的连贯操作,Selenium给我们提供了Ac ...

  5. 三.使用JDBC处理MySql大数据

    一.基本概念 大数据也称之为LOB(Large Objects),LOB又分为:clob和blob,clob用于存储大文本,blob用于存储二进制数据,例如图像.声音.二进制文等. 在实际开发中,有时 ...

  6. poj 2229 【完全背包dp】【递推dp】

    poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 828 ...

  7. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十章:混合

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十章:混合 代码工程地址: https://github.com/j ...

  8. oracle 创建新表,并复制旧表数据

    需求 备份数据,用于恢复. 语法规则 CREATE TABLE NEW_TAB AS SELECT * FROM OLD_TAB WHERE 1=1; 或者 CREATE TABLE NEW_TAB ...

  9. SDUT_2502:火星计数法

    火星人的计数规则里只有a,b,c,d四个字母,计数规则从小到大是 a,b,c,d,aa,ab,ac,ad,ba,……. 给出来由a,b,c,d四种字母组成的火星数字,算出该数字是第几个(从1开始). ...

  10. phpexcel使用说明3

    下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...