传送门

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

思路

维护两个单调队列,一个单调递增,维护最小值,一个单调递减,维护最大值。
#include<stdio.h>
#include<string.h>
const int maxn = 100005;
int a[maxn],q1[maxn],q2[maxn];

int main()
{
	int n,m,k;
	while (~scanf("%d%d%d",&n,&m,&k))
	{
		int res = 0,pos = 0;
		memset(a,0,sizeof(a));
		memset(q1,0,sizeof(q1));
		memset(q2,0,sizeof(q2));
		for (int i = 1;i <= n;i++)	scanf("%d",&a[i]);
		int head1 = 1,head2 = 1,tail1 = 0,tail2 = 0;
		for (int i = 1;i <= n;i++)
		{
			while (head1 <= tail1 && a[i] <= a[q1[tail1]])	tail1--;  //队头元素最小
			q1[++tail1] = i;
			while (head2 <= tail2 && a[i] >= a[q2[tail2]])	tail2--;  //队头元素最大
			q2[++tail2] = i;
			while (head1 <= tail1 && head2 <= tail2 && a[q2[head2]] - a[q1[head1]] > k)
			{
				if (q1[head1]<q2[head2])	pos = q1[head1++];
				else	pos = q2[head2++];
			}
			if (head1 <= tail1 && head2 <= tail2 && a[q2[head2]] - a[q1[head1]] >= m)	res = res>(i-pos)?res:(i-pos);

		}
		printf("%d\n",res);
	}
	return 0;
}

  

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

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

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

  2. hdu 3530 Subsequence 单调队列

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

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

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

  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. 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!

    今年微软Build 2016大会最让开发人员兴奋的消息之一,就是在Windows上可以原生运行Linux bash,对开发人员来说,这是一个喜闻乐见的消息. 1 安装 你必须安装开发者预览版本,才能使 ...

  2. python强大的区间处理库interval用法介绍

    原文发表在我的博客主页,转载请注明出处 前言 这个库是在阅读别人的源码的时候看到的,觉得十分好用,然而在网上找到的相关资料甚少,所以阅读了源码来做一个简单的用法总结.在网络的路由表中,经常会通过掩码来 ...

  3. bash中变量+=,if大小判断,随机休眠

    #!/bin/bash index= while true;do echo "hello" (( index+=)) echo `date "+%H:%M:%S" ...

  4. [转]Android Studio创建Xposed模块项目时BridgeApi的正确添加方式

    使用Android Studio创建的空项目作为Xposed Module App,对于Api Jar包的引用方式,一开始是按照傻瓜式Jar Lib的处理方式,复制XposedBridgeApi-54 ...

  5. DevExpress中设置PanelControl背景的方法

    首先当然是设置BackColor的颜色,但是设置完之后往往是没有反映的,这就Dev的好处带来的不好,然后我们需要自己定义两个属性 1.设置LookAndFeel下的style为Flat或UtralFl ...

  6. 20160205 - Windows 10 家庭版没有组策略

    问题描述:买笔电自带的正版系统,有一个需要使用gpedit.msc,发现并不存在. 解决办法:升级到 Windows 10 专业版

  7. 通过UserAgent判断设备为Android、Ios、Pc访问

    public static bool CheckAgent() { bool flag = false; string agent = HttpContext.Current.Request.User ...

  8. [BZOJ1579][Usaco2009 Feb]Revamping Trails 道路升级(二维最短路问题)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1579 分析: 设d[i][j]表示从1走到i.改了j条边的最短路径长度 如果设i相连的 ...

  9. JavaScript学习笔记- 省市级联效果

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  10. android之读取联系人信息

    联系人信息被存放在一个contacts2.db的数据库中 主要的两张表 读取联系人 读取联系人需要知道联系人内容提供者的地址,以及对应的操作对象.一般情况下操作对象是的命名方式和表明是一致的. 布局文 ...