hdu3530 Subsequence

G - Subsequence
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 题目意思,就是给你两个数列,要求找到一个最长子序列,要求,最大与最小的值的差别大于m小于k
//建两个队列,一个最大值,一个最小值 ,如果两个极值之差,比K要大,就要去掉,最小值和最大值中的,最小坐标,来进行更新,如果两个极值之差比m小,就更加入新值不用处理,如果比m大就是合法壮态,可以更新,
#include <iostream>
#include <stdio.h>
#include<cmath>
using namespace std;
int n,m,k,num[1000000];
int queuemax[1000000];
int queuemin[1000000];
int maxx(int a,int b)
{
    if(a>b)
    return a;
    else
    return b;
}
int main()
{
    int i,ans,j,frontmin,frontmax,rearmin,rearmax,lastmin,lastmax;
   while(scanf("%d%d%d",&n,&m,&k)!=EOF)
   {
       frontmin=0;rearmin=0;
       frontmax=0;rearmax=0;
       lastmin=0;lastmax=0;
       ans=0;
       for(i=1;i<=n;i++)
       scanf("%d",&num[i]);
       for(i=1;i<=n;i++)
       {

           
            while(frontmin<rearmin&&num[queuemin[rearmin-1]]>num[i])//最小优先队列插入
            {
                rearmin--;
            }
            //printf("%d %dqueumin\n",rearmin,i);
            queuemin[rearmin++]=i;
            while(frontmax<rearmax&&num[queuemax[rearmax-1]]<num[i])//最大优先队列的插入
            {
                rearmax--;
            }
            queuemax[rearmax++]=i;
          //  printf("%d %dqueumin\n",rearmin,i);
            while(num[queuemax[frontmax]]-num[queuemin[frontmin]]>k)
            {
                if(queuemax[frontmax]<queuemin[frontmin])//选取下标最小的更新
                {
                    lastmax=queuemax[frontmax];
                    frontmax++;
                }
                else
                {
                    lastmin=queuemin[frontmin];
                    frontmin++;
                }


            }
            if(num[queuemax[frontmax]]-num[queuemin[frontmin]]>=m)//符合条件就更新
            {
                ans=maxx(ans,i-maxx(lastmin,lastmax));
            }


       }



       printf("%d\n",ans);
   }


    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值