【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列

第一次写单调队列太垃圾。。。

左右各扫一遍即可。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 50010
using namespace std;
struct data
{
int x,h;
}a[N],q[N];
int n,m,l,r;
int ok1[N],ok2[N];
inline int read()
{
int f=,ans=;
char c;
while (!isdigit(c=getchar())) if (c=='-') f=-;
ans=c-'';
while (isdigit(c=getchar())) ans=ans*+c-'';
return ans*f;
}
bool cmp(data a,data b) {return a.x<b.x;}
int main()
{
n=read(); m=read();
for (int i=;i<=n;i++) a[i].x=read(),a[i].h=read();
sort(a+,a+n+,cmp);
l=; r=;
for (int i=;i<=n;i++)
{
while (l<=r && q[r].h<a[i].h) r--;
q[++r]=a[i];
while (l<=r && q[l].x<a[i].x-m) l++;
if (q[l].h>=*a[i].h) ok1[i]=;
}
l=; r=;
for (int i=n;i>=;i--)
{
while (l<=r && q[r].h<a[i].h) r--;
q[++r]=a[i];
while (l<=r && q[l].x>a[i].x+m) l++;
if (q[l].h>=*a[i].h) ok2[i]=;
}
int ans=;
for (int i=;i<=n;i++)
if (ok1[i]&&ok2[i]) ans++;
printf("%d\n",ans);
return ;
}

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.

N头牛在一个坐标轴上,每头牛有个高度。现给出一个距离值D。

如果某头牛在它的左边,在距离D的范围内,如果找到某个牛的高度至少是它的两倍,且在右边也能找到这样的牛的话。则此牛会感觉到不舒服。

问有多少头会感到不舒服。

Input

* 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

* Line 1: The number of crowded cows.

Sample Input

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

INPUT DETAILS: 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.

Sample Output

2
OUTPUT DETAILS: The cows at positions x=5 and x=6 are both crowded.

HINT

 

Source

上一篇:Javascript动画效果(二)


下一篇:openjudge666:放苹果—题解