首页 > 其他 > 详细

Subsequence

时间:2019-09-03 21:08:44      阅读:117      评论:0      收藏:0      [点我收藏+]

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;
}

 

Subsequence

原文:https://www.cnblogs.com/czy-power/p/11455046.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!