hdu3530Subsequence

来源:互联网 发布:淘宝销售的案例 编辑:程序博客网 时间:2024/06/05 16:20
ime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4827    Accepted Submission(s): 1587


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.
 

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
 

Source
2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU

1.假设我们现在知道序列(i,j)是符合标准的,那么如果第j+1个元素不比(i,j)最大值大也不比最小值小,那么(i,j+1)也是合法的

2.如果(i,j)不合法的原因是差值比要求小,那在(i,j)范围内的改动是无效的,需要加入j+1元素充当最大值或者最小值才可能获得合法的序列

3.假设序列(i,j)的差值比要求大,那么我们必须将其中的最大值或者最小值从序列中删除出去,才可能获得一个合法的序列,只往里加入元素是不可能令序列合法的

用两个单调队列分别维护前i个元素中的最大值与最小值的下标以及当前最左边的边界点

#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<iostream>#include<algorithm>#include<bitset>#include<climits>#include<list>#include<iomanip>#include<stack>#include<set>using namespace std;int q1[100010],q2[100010],a[100010];int main(){int n,m,k;while(cin>>n>>m>>k){for(int i=1;i<=n;i++)cin>>a[i];int head1=0,tail1=0,head2=0,tail2=0,ans=0,index=0;for(int i=1;i<=n;i++){while(head1<tail1&&a[q1[tail1-1]]<a[i])tail1--;while(head2<tail2&&a[q2[tail2-1]]>a[i])tail2--;q1[tail1++]=i;q2[tail2++]=i;while(head1<tail1&&head2<tail2&&a[q1[head1]]-a[q2[head2]]>k){if(q1[head1]<q2[head2])index=q1[head1++];elseindex=q2[head2++];}if(head1<tail1&&head2<tail2&&a[q1[head1]]-a[q2[head2]]>=m)ans=max(ans,i-index);}cout<<ans<<endl;}}





0 0
原创粉丝点击