BZOJ 2276 Poi2011 Temperature 单调队列

题目大意:给定一个序列,每个元素的大小有一个取值范围,求一段区间满足区间内元素可能单调不降
L 维护一个单调不增的单调队列,一旦新插入的R值比队头的 L <script type="math/tex" id="MathJax-Element-58">L</script>值小就把队头弹掉
这样可以保证单调队列中的元素是合法的极大子区间
然后更新答案就行了
乱写读入优化害死人啊QwQ

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 1001001
using namespace std;
int n,ans;
pair<int,int> a[M];
namespace IStream{
    #define L (1<<3)
    char Get_Char()
    {
        static char buffer[M],*S,*T;
        if(S==T)
        {
            T=(S=buffer)+fread(buffer,1,L,stdin);
            if(S==T) return EOF;
        }
        return *S++;
    }
    int Get_Int()
    {
        int re=0,mark=0;
        char c=Get_Char();
        while(c<'0'||c>'9')
        {
            if(c=='-')
                mark=1;
            c=Get_Char();   
        }
        while(c>='0'&&c<='9')
            re=(re<<3)+(re<<1)+(c-'0'),c=Get_Char();
        return mark?-re:re;
    }
}
int main()
{
    using namespace IStream;
    //freopen("tem.in","r",stdin);
    //freopen("tem.out","w",stdout);
    int i;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        a[i].first=Get_Int();
        a[i].second=Get_Int();
    }
    static int q[M],r,h;
    for(i=1;i<=n;i++)
    {
        while( r-h>=1 && a[i].first >=a[q[r]].first )
            q[r--]=0;
        q[++r]=i;
        while( r-h>=1 && a[i].second < a[q[h+1]].first )
            ++h;
        ans=max(ans,q[r]-q[h]);
    }
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值