微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Subsequence

Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10796    Accepted Submission(s): 3613


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 0 1 1 1 1 1 5 0 3 1 2 3 4 5
 

 

Sample Output
5 4
#pragma GCC optimize(2)
#include <bits/stdc++.h>

using namespace std;
const int maxn=1e5+108;
typedef long long ll;

int n,m,k;
int c[maxn];
int max_queue[maxn],min_queue[maxn];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("1.txt","r",stdin);
#endif
    while(scanf("%d%d%d",&n,&m,&k)!=EOF){
        int max_head=1,max_tail=0,min_head=1,min_tail=0,res=0;
        int f=0;
        for(register int i=1;i<=n;++i){
            scanf("%d",&c[i]);
            while(max_head<=max_tail&&c[i]>=c[max_queue[max_tail]])max_tail--;
            while(min_head<=min_tail&&c[i]<=c[min_queue[min_tail]])min_tail--;
            max_queue[++max_tail]=i;
            min_queue[++min_tail]=i;
            while(c[max_queue[max_head]]-c[min_queue[min_head]]>k){
                f=min(min_queue[min_head],max_queue[max_head]);
                if(f==min_queue[min_head]){
                    ++min_head;
                }
                if(f==max_queue[max_head]){
                    ++max_head;
                }
            }
            if(c[max_queue[max_head]]-c[min_queue[min_head]]>=m)res=max(res,i-f);
        }
        printf("%d\n",res);
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐