首页 > 代码库 > BZOJ3314: [Usaco2013 Nov]Crowded Cows

BZOJ3314: [Usaco2013 Nov]Crowded Cows

3314: [Usaco2013 Nov]Crowded Cows

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 86  Solved: 61
[Submit][Status]

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

Silver

题解:
单调队列维护可行域内的最大值。
1A是对我的一种补偿吗?T_T
代码:
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 100000+100014 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)22 #define mod 100000000723 using namespace std;24 inline int read()25 {26     int x=0,f=1;char ch=getchar();27     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}28     while(ch>=0&&ch<=9){x=10*x+ch-0;ch=getchar();}29     return x*f;30 }31 struct rec{int x,y;}a[maxn],q[maxn];32 int n,m;33 bool can1[maxn],can2[maxn];34 inline bool cmp(rec a,rec b)35 {36     return a.x<b.x;37 }38 int main()39 {40     freopen("input.txt","r",stdin);41     freopen("output.txt","w",stdout);42     n=read();m=read();43     for1(i,n)a[i].x=read(),a[i].y=read();44     sort(a+1,a+n+1,cmp);45     int l=1,r=0;46     for1(i,n)47     {48         while(l<=r&&q[r].y<a[i].y)r--;49         q[++r]=a[i];50         while(l<=r&&q[l].x<a[i].x-m)l++;51         if(q[l].y>=a[i].y*2)can1[i]=1;52     }53     l=1,r=0;54     for3(i,n,1)55     {56         while(l<=r&&q[r].y<a[i].y)r--;57         q[++r]=a[i];58         while(l<=r&&q[l].x>a[i].x+m)l++;59         if(q[l].y>=a[i].y*2)can2[i]=1;60     }61     int ans=0;62     for1(i,n)if(can1[i]&&can2[i])ans++;63     printf("%d\n",ans);64     return 0;65 }
View Code

 

BZOJ3314: [Usaco2013 Nov]Crowded Cows