Luo Gu P3088 [USACO13NOV] cow milking Crowded Cows solution to a problem

P3088 [USACO13NOV] cow milking Crowded Cows

Title Description

Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i is standing at location x(i) and has height h(i) (1 <= x(i),h(i) <= 1,000,000,000).

A cow feels "crowded" if there is another cow at least twice her height within distance D on her left, and also another cow at least twice her height within distance D on her right (1 <= D <= 1,000,000,000). Since crowded cows produce less milk, Farmer John would like to count the number of such cows. Please help him.

FJ has N (1 <= N <= 50,000) cows graze along one dimension of the fence, cows i-th target point X (i), its height is h (i) (1 <= x (i ), h (i) <= 1,000,000,000).

When a cow left in the distance D and D from the right side and there is a height at least twice its cows, t (1 <= D <= 1,000,000,000), it will feel crowded.

Calculate the number of cows feel crowded.

Input Format

  • Line 1: Two integers, N and D.

  • Lines 2..1+N: Line i+1 contains the integers x(i) and h(i). The locations of all N cows are distinct.

Output Format

  • Line 1: The number of crowded cows.

Sample input and output

Input # 1 copy

6 4
10 3
6 2
5 3
9 7
3 6
11 2

Output # 1 copy

2

Description / Tips

There are 6 cows, with a distance threshold of 4 for feeling crowded. Cow #1 lives at position x=10 and has height h=3, and so on.

The cows at positions x=5 and x=6 are both crowded.

[Thinking]

Monotonous queue

[Analysis] title

The length of the interval d on both sides of each cow has a
long interval on both sides have not less than twice the height of the cow cow
he would feel crowded
seeking feel crowded cattle number

[Prefix] thought

There it is at least twice the height of the cow?
This is entirely up to the other end of the comparison cow ah?
As long as he is twice as high as the highest ratio
would be at least some of
, if not as high as the highest of his twice
it must not have more than twice as high as his
so you can compare the highest
range of the maximum value?
Of course, the queue is monotonous
but two intervals?
Then two monotonous queue!

[Thinking] final

Process again sweep along the left of the dot in each interval
the highest cow
backwards sweep over the inner processing zone to the right of each dot
maximum cow
(refer to section herein are legitimate interval less than the interval that is equal to length of d )
and then sweep over each point
to see whether it has twice as high than twice their height cow
if it counts
if it does not continue

[Complete code]

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<deque>
using namespace std;
const int Max = 50004;
struct node
{
    int x;
    int h;
}a[Max];
bool cmp(const node x,const node y)
{
    return x.x < y.x;
}
deque<node>q1,q2;
int l[Max],r[Max];
int main()
{
//  freopen("cow.in","r",stdin);
    int n,d;
    cin >> n >> d;
    for(register int i = 1;i <= n;++ i)
        cin >> a[i].x >> a[i].h;
    sort(a + 1,a + 1 + n,cmp);
    for(register int i = 1;i <= n;++ i)
    {
        while(!q1.empty() && a[i].x - q1.front().x > d)
            q1.pop_front();
        if(!q1.empty())
            l[i] = q1.front().h;
        else
            l[i] = 0;
        while(!q1.empty() && a[i].h > q1.back().h)
            q1.pop_back();
        q1.push_back(a[i]);
    }
    for(register int i = n;i >= 1;i --)
    {
        while(!q2.empty() && q2.front().x - a[i].x > d)
            q2.pop_front();
        if(!q2.empty())
            r[i] = q2.front().h;
        else
            r[i] = 0;
        while(!q2.empty() && a[i].h > q2.back().h)
            q2.pop_back();
        q2.push_back(a[i]);
    }
    int js = 0;
    for(register int i = 1;i <= n;++ i)
        if(l[i] >= a[i].h * 2 && r[i] >= a[i].h * 2)
            js ++;
    cout << js << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/acioi/p/11689817.html