hdu-3530-Subsequence

Subsequence

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


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
 

Source
 

Recommend
zhengfeng   |   We have carefully selected several similar problems for you:   3535  3529  3528  3527  3415

题目大意:给你n个数求一段最长的连续序列使得该序列的最大值减去最小值要大于等于m小于等于k。
思路:维护两个单调队列,一个单调递增的队列,一个递减的队列,并且维护一个指针p(代表满足当前序列的最左边的端点,开始时赋值为0)。然后分三种情况讨论当值小于m时,这时候队列中肯定不存在成立的值,所有就必须再继续进队,当刚好成立的时候,更新ans,当大于k的时候,这时候就要把单调队列的头指针出队了,然后这时候必须要选择头指针位置小的那个队列的头指针出队,因为你把位置大的出队的话位置小的那个元素有被用到,这时候明显不符合实际,因为位置小的存在,位置大的必定也存在,所以这时候必须把位置小的出队,然后更新指针p = 出队的那个元素位置加1。

这是做的第一道单调队列,这道题目中单调队列里面存的是数组元素的下标,当是它是维护的是数组值的单调性,感觉最重要的一点是队列里面的下标值肯定都是从小到大的,
因为在进队过程中,是把前面不合符的都出队了,所以它所在的位置肯定是最右边的,这样就能保证了i-p是最优答案了,因为p这时候是为队头位置+1,而出队的队头位置是最小的。所以p这是是最小的。

代码:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
int n,m,k,st[100050];
int a[100050],b[100050];
int main()
{
    int i,j;
    while (scanf("%d %d %d",&n,&m,&k)!=EOF)
    {
        int ans = 0;
        a[0] = 0;
        b[0] = 0;
        int ok = 0;
        int pa1 = 0, pa2 = 1,pb1 = 0,pb2 = 1, p = 0;
        for(i = 0; i<n; i++)
        {
            scanf("%d",&st[i]);
        }
        for(i = 1; i < n; i++)
        {
            while(pa1 != pa2 && st[i] > st[a[pa2-1]])
            {
                pa2--;
            }
            a[pa2++] = i;
            while(pb1 != pb2 && st[i] < st[b[pb2-1]])
            {
                pb2--;
            }
            b[pb2++] = i;   //维护单调队列
            if(abs(st[a[pa1]] - st[b[pb1]]) < m)    //第一种情况
                continue;
            while(abs(st[a[pa1]] - st[b[pb1]]) > k)
            {
                if(a[pa1] == b[pb1])
                    break;
                if(a[pa1] > b[pb1])
                {
                    p = b[pb1] + 1;
                    pb1++;
                }
                else
                {
                    p = a[pa1] + 1;
                    pa1++;
                }
            }                                      //第三种情况
            if(i - p > ans)
            {
                ans = i - p;
            }                                      //第二种情况
         }
         if(ans == 0 && m == 0)
         {
             cout<<"1"<<endl;
         }
         else if(ans == 0)
            cout<<"0"<<endl;

         else
         cout<<ans + 1<<endl;
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值