[USACO13NOV]Crowded Cows

Posted Neworld2002

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[USACO13NOV]Crowded Cows相关的知识,希望对你有一定的参考价值。

题目链接-洛谷

#include<cstdio>
#define MAXN 500005
using namespace std;

int x[MAXN];
int h[MAXN];

int dq1[MAXN],dq2[MAXN];

inline void qs(int l,int r){
    int i=l,j=r,s=x[(l+r)>>1],t;
    while(true){
        while(x[i]<s)++i;
        while(x[j]>s)--j;
        if(i<=j){
            t = x[i];x[i] = x[j];x[j] = t;
            t = h[i];h[i] = h[j];h[j] = t;
            ++i;--j;
        }
        if(i>j)break;
    }
    if(i<r)qs(i,r);
    if(l<j)qs(l,j);
}

int N,D;
int l1 = 1;
int r1 = 0;
int l2 = 1;
int r2 = 0;

int main(){
    
    scanf("%d%d",&N,&D);
    for(register int i=1;i<=N;++i){
        scanf("%d%d",&x[i],&h[i]);
    }
    qs(1,N);
    
    int ans = 0;
    dq1[1] = h[1];
    r1=1;
    
    int head = 1;
    int tail = 3;
    
    while(tail<=N&&x[2]+D>=x[tail]){
        int y = h[tail++];
        while(l2<=r2&&dq2[r2]<y)r2--;
        dq2[++r2]=y;
    }
    
    for(register int i=2;i<N;++i){
        
        int num1 = dq1[l1]>>1;int num2 = dq2[l2]>>1;
        if(num1>=h[i]&&num2>=h[i])ans++;
        
        int j = i+1;
        if(j==N)continue;
        
        while(head<j&&x[j]-D>x[head]){
            if(h[head++]==dq1[l1])l1++;
        }
        
        if(x[i]>=x[j]-D){
            while(l1<=r1&&dq1[r1]<h[i])r1--;
            dq1[++r1]=h[i];
        }
        
        if(h[j]==dq2[l2])l2++;
        
        while(tail<=N&&x[j]+D>=x[tail]){
            int y = h[tail++];
            while(l2<=r2&&dq2[r2]<y)r2--;
            dq2[++r2]=y;
        }
    }
    
    printf("%d",ans);
    return 0;
}

以上是关于[USACO13NOV]Crowded Cows的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ 3314 [Usaco2013 Nov]Crowded Cows:单调队列

? 洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows

BZOJ3016: [Usaco2012 Nov]Clumsy Cows 贪心 单调栈

[USACO08NOV]奶牛混合起来Mixed Up Cows

[USACO08NOV]奶牛混合起来Mixed Up Cows

bzoj3016[Usaco2012 Nov]Clumsy Cows*