传送门

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. 套用GGTalk做项目的经验总结——GGTalk源码详解系列(一)

    坦白讲,我们公司其实没啥技术实力,之所以还能不断接到各种项目,全凭我们老板神通广大!要知道他每次的饭局上可都是些什么人物! 但是项目接下一大把,就凭咱哥儿几个的水平,想要独立自主.保质保量保期地一个个 ...

  2. 完全开源Android网络框架 — 基于JAVA原生的HTTP框架

    HttpNet网络请求框架基于HttpUrlConnection,采用Client + Request + Call的请求模型,支持https默认证书,数字安全证书.支持http代理!后续将会实现队列 ...

  3. react实现的tab切换组件

    我有点想要吐槽,因为用原生的js实现起来挺简单的一个小东西,改用react来写却花了我不少时间,也许react的写法只有在复杂的web应用中才能体现出它的优势吧!不过吐槽归吐槽,对react这种优雅的 ...

  4. [BZOJ1854][Scoi2010]游戏(二分图匹配/并查集)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1854 分析:很裸的一道二分图匹配对吧,但是在hzwer的blog上看见神奇的并查集做法 ...

  5. 最小/大费用最大流模板(codevs1914)

    void addedge(int fr,int to,int cap,int cos){ sid[cnt].fr=fr;sid[cnt].des=to;sid[cnt].cap=cap;sid[cnt ...

  6. python内置数据类型-字典和列表的排序 python BIT sort——dict and list

    python中字典按键或键值排序(我转!)   一.字典排序 在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的.因此,为了使统计得到的结果更方便查看需要进行排序. Py ...

  7. android 资讯阅读器(二)

    接着上次的博客,上次移植完了tab以后整个app的框架就算是定下来了. 本次目标: 1.数据的获取与展示(ListView) 2.官方的下拉刷新效果(SwipeRefreshLayout) 3.数据接 ...

  8. Using LINQ to SharePoint

    LINQ and LINQ Providers   LINQ is a feature of the programming languages C# and Microsoft Visual Bas ...

  9. 前端构建工具 webpack

    一.自我初级认知  (是什么?     能干什么,有卵用?       有选择为什么要选你?(比较优势在哪) )     适合的才是最好的 模块打包器(module bundler)     根据项目 ...

  10. Java--笔记(4)

    31.中间件是一种独立的系统软件或服务程序,分布式应用软件借助这种软件在不同的技术之间共享资源.中间件位于客户机/ 服务器的操作系统之上,管理计算机资源和网络通讯.是连接两个独立应用程序或独立系统的软 ...