HDU 3530 Subsequence

26 篇文章 0 订阅
19 篇文章 0 订阅

Subsequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7550 Accepted Submission(s): 2555
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 | We have carefully selected several similar problems for you: 3535 3529 3528 3527 3415

//维护两个单调队列
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 100010
using namespace std;
inline int max(int a ,int b) { return a>b?a:b; }
int s1[N],s2[N],a[N];
int main() {
    int n,m,k,top1,top2,last1,last2,tail1,tail2,Ans;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF) {
        for(int i=1; i<=n; i++) scanf("%d",&a[i]);
        memset(s1,0,sizeof(s1));
        memset(s2,0,sizeof(s2));
        top1 = 0,top2 = 0,tail1 = 0,tail2 = 0;
        Ans = 0,last1 = 0,last2 = 0;
        for(int i=1; i<=n; i++){
            //max
            while(top1 < tail1 && a[s1[tail1 - 1]] <= a[i]) tail1--;  //top1最大元素
            s1[tail1++] = i;
            //min
            while(top2 < tail2 && a[s2[tail2 - 1]] >= a[i]) tail2--;  //top2最小元素
            s2[tail2++] = i;
            while(a[s1[top1]] - a[s2[top2]] > k){
                if(s1[top1] < s2[top2])
                    last1 = s1[top1++];
                else last2 = s2[top2++];
            }
            if(a[s1[top1]] - a[s2[top2]] >= m)
                Ans = max(Ans,i - max(last1,last2));
        }
        printf("%d\n",Ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值