HDU 3530 Subsequence(单调队列)
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(单调队列)的更多相关文章
- HDU - 3530 Subsequence (单调队列)
Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 3530 Subsequence 单调队列
题目链接 题目给出n个数, 一个下界m, 一个上界k, 让你求出最长的一段序列, 满足这段序列中的最大的数-最小的数<=k&&>=m, 输出这段长度. 可以维护两个队列, ...
- 【单调队列+尺取】HDU 3530 Subsequence
acm.hdu.edu.cn/showproblem.php?pid=3530 [题意] 给定一个长度为n的序列,问这个序列满足最大值和最小值的差在[m,k]的范围内的最长子区间是多长? [思路] 对 ...
- hdu 3530 Subsequence
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3530 Subsequence Description There is a sequence of i ...
- HDU 3401 Trade(单调队列优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:炒股.第i天买入一股的价钱api,卖出一股的价钱bpi,最多买入asi股,最多卖出bsi股 ...
- Hdu 3410 【单调队列】.cpp
题意: 给出一个数组,问你对于第i个数,从最后一个比它大的数到它之间比它小的数中最大的那个数的下标,以及它右边到第一个比它大的数中比它小的数中最大的那一个数的下标<下标从1开始>. eg: ...
- HDU 5749 Colmerauer 单调队列+暴力贡献
BestCoder Round #84 1003 分析:(先奉上zimpha巨官方题解) 感悟:看到题解单调队列,秒懂如何处理每个点的范围,但是题解的一句算贡献让我纠结半天 已知一个点的up,do ...
- HDU 5289 Assignment(单调队列)
题意:给T足数据,然后每组一个n和k,表示n个数,k表示最大同意的能力差,接下来n个数表示n个人的能力,求能力差在k之内的区间有几个 分析:维护一个区间的最大值和最小值,使得他们的差小于k,于是採用单 ...
- hdu 3530 "Subsequence" (单调队列)
传送门 题意: 给出一个序列,求最长的连续子序列,使得 m ≤ Max-Min ≤ k 我的理解: 定义数组 a[] 存储输入的 n 个数: 定义两个双端队列: deque<int >qM ...
随机推荐
- Sql server使用Merge关键字做插入或更新操作
Merge是关于对于两个表之间的数据进行操作的. 要使用Merge的场景比如: 数据同步 数据转换 基于源表对目标表做Insert,Update,Delete操作 MERGE语句的基本语法: MERG ...
- webpack入坑之旅(五)加载vue单文件组件
这是一系列文章,此系列所有的练习都存在了我的github仓库中vue-webpack,在本人有了新的理解与认识之后,会对文章有不定时的更正与更新.下面是目前完成的列表: webpack入坑之旅(一)不 ...
- 使用iframe标签结合springMvc做文件上传
1.iframe.jsp <body> <h1>测试iframe文件上传</h1> <!-- 1.要求表单的target属性名称与iframe的name名字一 ...
- linux 安装samba
1. yum -y install samba 2. 配置 vi /etc/samba/smb.conf [global] 下面的 修改 workgroup = MYGROUPsecurity = s ...
- Android点击EditText文本框之外任何地方隐藏键盘的解决办法
1,实现方法一:通过给当前界面布局文件的父layout设置点击事件(相当于给整个Activity设置点击事件),在事件里进行键盘隐藏 <LinearLayout xmlns:android=&q ...
- iOS开发小技巧--利用MJExtension解决数据结构复杂的模型转换
一.开发中难免会遇到,系统返回的数据中字典套集合,集合里面又套一层字典,然后字典里面还有字典或者集合等等的复杂结构的数据...MJExtension轻松搞定这类问题 1.解决方法一: 例:百思项目中帖 ...
- HSV
HSV 相比于面向机器的RGB, HSV或HSI更贴近于人对颜色的描述: H: Hue, 颜色本身 S: Saturation, 纯色被白色稀释的程度. V: Value, 或I, Intensity ...
- 用MathType编辑横三角形的方法
如果常常接触数学公式,你会发现同一个符号如果变换方向使用就可以代表不同的数学含义,这是非常常见的一种数学现象了.对于这种情况在数学公式编辑器中,我们可以使用不同的模板来进行编辑.比如横着的三角形符号, ...
- Struts2+Spring+Mybatis+Junit 测试
Struts2+Spring+Mybatis+Junit 测试 博客分类: HtmlUnit Junit Spring 测试 Mybatis package com.action.kioskmoni ...
- Extract Fasta Sequences Sub Sets by position
cut -d " " -f 1 sequences.fa | tr -s "\n" "\t"| sed -s 's/>/\n/g' & ...