bzoj 2276 [ Poi 2011 ] Temperature —— 单调队列

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2276

维护 l 递减的单调队列,队头的 l > 当前的 r 就出队,因为不能是连续一段了;

注意答案是上一个出队的实际后一个位置开始,而不是队头的位置,因为不在队列里的那些位置 l 都比队头小,也是合法的。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const maxn=1e6+5;
int n,l[maxn],r[maxn],h,t,ans;
struct N{int l,r,pos;}q[maxn];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d%d",&l[i],&r[i]);
    h=1; t=0;
    for(int i=1;i<=n;i++)
    {
        while(h<=t&&q[h].l>r[i])h++;
        ans=max(ans,i-q[h-1].pos);//
        while(h<=t&&q[t].l<l[i])t--;
        q[++t].l=l[i]; q[t].r=r[i]; q[t].pos=i;
    }
    printf("%d\n",ans);
    return 0;
}

 

posted @ 2018-08-16 19:44  Zinn  阅读(130)  评论(0编辑  收藏  举报