HDU 3530 Subsequence 单调队列

Subsequence

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


 

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

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

 

 

Recommend

zhengfeng

 


题意

给出一个n个数的数列,求出最长的区间满足区间中的最大值和最小值的差在[m,k]之间。


题解

对原系列建立两个单调队列。一个单调递减队列和单调递增队列。

每次比较最大值和最小值的差值c

如果c > k 说明需要将最大值或者最小值出队。下标越小先出队。直到c <= k 为止,同时记录下离当前i最远的下标now

如果当前序列的c满足 >=m 时说明(now,i]是一个可行解。


代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int n,m,k;
int arr[maxn],qu1[maxn<<1],qu2[maxn<<1];
int h1,h2,t1,t2;
void debug() {
    for(int i=h1;i<=t1;i++) printf("%d ",qu1[i]);printf("\n");
    for(int i=h2;i<=t2;i++) printf("%d ",qu2[i]);printf("\n");
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k)) {
        h1 = h2 = 1;
        t1 = t2 = 0;
        int ans = 0,now = 0;
        for(int i=1;i<=n;i++) {
            scanf("%d",&arr[i]);
            while(h1 <= t1 && arr[i] > arr[qu1[t1]]) t1--;
            qu1[++t1] = i;
            while(h2 <= t2 && arr[i] < arr[qu2[t2]]) t2--;
            qu2[++t2] = i;
            //debug();
            /// now 是满足条件 离i最远的下标。
            while(h1 <= t1 && h2 <= t2 && (arr[qu1[h1]] - arr[qu2[h2]]) > k) {
                if(qu1[h1] < qu2[h2]) now = qu1[h1++];
                else now = qu2[h2++]; 
            }
            if(h1 <= t1 && h2 <= t2 && (arr[qu1[h1]] - arr[qu2[h2]]) >= m) {
                ans = max(ans,i-now);
            }
            
        }
        printf("%d\n",ans);
    }  
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值