【多校第一场】【单调队列】HDU 5289 Assignment

多校第一场,抱蛋,我选择吃锅orz。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289


这道题是一道单调队列,维护一个递增队和一个递减队,虽然当时看出来了,但是还是一直wa,原因在于我对于总的头节点直接移动到最值上了,然而其实在中途的最值差就可能小于目标值。嗯,还是太年轻(虽然被一群高中生虐了)。

然后经过身边大神的教导终于把代码写出来了,代码如下:


#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#define longlong __int64

using namespace std;

#define INF 0x3f3f3f3f

long long a[100006],x,y;
long long ans;
int n;
long long k;
long long up[1000006];
long long down[1000006];
long long mi,ni;

void solve()
{
    int Front=0;
    int head1=0,tail1=0;
    int head2=0,tail2=0;

    up[tail1++]=0;
    down[tail2++]=0;

    for(int i=1; i<n; i++)
    {
        while(tail1>head1 && a[up[tail1-1]]>=a[i])
        {
            tail1--;
        }
        up[tail1++]=i;

        while(tail2>head2 && a[down[tail2-1]]<=a[i])
        {
            tail2--;
        }
        down[tail2++]=i;

        while(a[down[head2]]-a[up[head1]]>=k)
        {
            if(Front==down[head2]) head2++;
            if(Front==up[head1]) head1++;
            Front++;
        }

        //cout<<a[up[head1]]<<" "<<a[down[head2]]<<"    ";
        ans+=i-Front+1;
        //cout<<ans<<" "<<Front<<endl;
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld%lld",&n,&k);
        ans=1;
        x=y=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
        }

        solve();
        printf("%lld\n",ans);
    }
    return 0;
}
/*
3
22 9575
3536 12233 20849 18993 10615 18181 13417 29058 25631 8576 12516 22159 10932 23745 343 20688 8807 18061 17545 11196 12636 4802
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值