洛谷P3088 [USACO13NOV] Crowded Cows S 题解

题目传送门。

首先正常优先队列,看到左边和右边,肯定得扫两遍,至于如何判断是否存在奶牛的身高使它拥挤,只需要判断队首是否使它拥挤就行了,因为根据优先队列的性质,你会发现队首就是最高的那个人,如果最高的人不满足,那其他人更不可能满足。
代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 5e4+5;
struct node
{
    int x;
    int y;
}a[N];
int q[N];
int cmp(node x,node y)
{
    return x.x<y.x;
}
int bx[N];
signed main()
{
    int n,d;
    scanf("%d %d",&n,&d);
    for(int i = 1;i<=n;i++)
    {
        scanf("%d %d",&a[i].x,&a[i].y);
    }
    sort(a+1,a+n+1,cmp);
    int h = 1,t = 0;
    int ans = 0;
    for(int i = 1;i<=n;i++)
    {
        while(h<=t&&a[q[t]].y<a[i].y)
        {
            t--;
        }
        q[++t] = i;
        while(h<=t&&a[q[h]].x<a[i].x-d)
        {
            h++;
        }
        if(a[q[h]].y>=a[i].y*2)
        {
            bx[i] = 1;
        }
    }
    h = 1,t = 0;
    for(int i = n;i;i--)
    {
        while(h<=t&&a[q[t]].y<a[i].y)
        {
            t--;
        }
        q[++t] = i;
        while(h<=t&&a[q[h]].x>a[i].x+d)
        {
            h++;
        }
        if(a[q[h]].y>=a[i].y*2&&bx[i])
        {
            ans++;
        }
    }
    printf("%d",ans);
    return 0;
}
posted @ 2025-05-09 16:06  林晋堃  阅读(78)  评论(0)    收藏  举报