hdu3530 Subsequence 单调队列
来源:互联网 发布:js防水涂料哪个牌子 编辑:程序博客网 时间:2024/05/29 09:34
Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4903 Accepted Submission(s): 1615
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.
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 01 1 1 1 15 0 31 2 3 4 5
Sample Output
54
问最长的一个子序列长度是多少,子序列满足最大值减去最小值在m和k之间。
用两个队列,最大值队列和最小值队列,最大值队列单调递减,最小值队列单调递增,最大值队列里的点u表示从u开始到当前点的最大值是a[u],最小值队列里的点u表示从u开始到当前点的最小值是a[u]。一旦最大值队列首元素的值和最小值队列首元素的值之差不大于k,就判断这两个值的差是否不小于m,是就更新答案,注意更新答案的长度是从两个队列队首上个点加1里面大的那个值到当前点这段长度。如果最大值队列首元素的值和最小值队列首元素的值之差大于k,就把靠前的那个front加1,再继续判断。要注意细节问题,我为了方便处理先在队首加了个权值INF的点。
用单调队列处理后复杂度是O(N)。
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;typedef long long LL;const LL MAXN=100010;const LL MAXM=5010;const LL INF=0x3f3f3f3f;int N,M,K;int a[MAXN],q1[MAXN],q2[MAXN];int main(){ freopen("in.txt","r",stdin); while(scanf("%d%d%d",&N,&M,&K)!=EOF){ for(int i=1;i<=N;i++) scanf("%d",&a[i]); a[0]=INF; int front1=0,rear1=0,front2=0,rear2=0; q1[0]=q2[0]=0; int ans=0; for(int i=1;i<=N;i++){ while(front1<=rear1&&a[q1[rear1]]<=a[i]) rear1--; q1[++rear1]=i; while(front2<=rear2&&a[q2[rear2]]>=a[i]) rear2--; q2[++rear2]=i; while(a[q1[front1]]-a[q2[front2]]>K){ if(q1[front1]<q2[front2]) front1++; else front2++; } if(a[q1[front1]]-a[q2[front2]]>=M){ if(front2<=0) ans=max(ans,i-q1[front1-1]); ans=max(ans,i-max(q1[front1-1],q2[front2-1])); } } printf("%d\n",ans); } return 0;}
0 0
- hdu3530 Subsequence 单调队列
- hdu3530 Subsequence 单调队列
- hdu3530 Subsequence 单调队列
- hdu3530 Subsequence 单调队列
- hdu3530 Subsequence 单调队列
- hdu3530 Subsequence(单调队列)
- #HDU3530#Subsequence(单调队列)
- hdu3530 Subsequence 单调队列
- hdu3530——Subsequence(单调队列优化DP)
- hdu3530单调队列
- HDU3530(单调队列)
- hdu3530 单调队列
- hdu3530(单调队列)
- hdu3530单调队列(双)
- HDU3530 单调队列的应用
- hdu3530(最长连续子串+单调队列)
- hdu3530 Subsequence
- hdu3530 Subsequence
- Java PermGen 去哪里了?
- Kali Linux安装360免费wifi驱动。
- Win7 64位中MinGW和MSYS的安装
- matlab中for循环的循环变量在循环体中不能改变的问题
- Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型
- hdu3530 Subsequence 单调队列
- 勾股定理
- C语言中的union用法
- leetcode#19 Remove Nth Node From End of List
- 2015程序设计实习之第二次上机周六
- mysql中int、bigint、smallint 和 tinyint的区别详细介绍
- Linux进程间通信——信号集函数
- 30.因为绘画,我在豆瓣上认识了老婆
- 钟表维修管理系统技术解析(八) 保修记录、新增、修改、报废