hdu3530 Subsequence 单调队列
2015-06-03 20:11
501 查看
Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4903 Accepted Submission(s): 1615
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print the length of the subsequence on a single line.
[align=left]Sample Input[/align]
5 0 0 1 1 1 1 1 5 0 3 1 2 3 4 5
[align=left]Sample Output[/align]
5 4
问最长的一个子序列长度是多少,子序列满足最大值减去最小值在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; }
相关文章推荐
- build.xml编译时 程序包javax.servlet不存在
- MelFrequencyFilterBank API 及Mel频率滤波器
- IOS开发 使用XIB自定义一个UIView
- Request for package information failed: tunneling socket could not be established, cause=14033355594
- Json转换利器Gson之实例二-Gson注解和GsonBuilder
- android studio 的build tools的下载 网址
- U3D NGUI插件讲解
- Number Sequence
- [FindBugs分析记录]Potentially dangerous use of non-short-circuit logic
- CSS-MediaQuery
- UIView
- "Cannot convert value '0000-00-00' from column 2 to TIMESTAMP"mysql时间转换bug
- Bluemix云平台实践:构建可穿戴应用(2)
- Bluemix云平台实践:构建可穿戴应用(1)
- QuickTest Professional 11无限试用期破解方法
- iOS之UI控件之TableView详细解释
- github上的androidUI开源项目大全,酷炫到令人发指
- SharePoint自动化系列——通过Coded UI录制脚本自动化创建SharePoint Designer Reusable Workflow
- 试用Bluemix的Virtual Machine
- LeetCode Unique Paths(dp)