bzoj3314[Usaco2013 Nov]Crowded Cows*

bzoj3314[Usaco2013 Nov]Crowded Cows

题意:

n头牛,如果某头牛左边距离D以内有高度至少是它的两倍的牛,右边也有,则此牛会感觉到不舒服。问多少牛会不舒服。n≤50000

题解:

用单调队列维护距离D以内的区间最大值,判断是否至少是当前牛的两倍,再倒回去做一遍即可。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define inc(i,j,k) for(int i=j;i<=k;i++)
 5 #define maxn 100010
 6 using namespace std;
 7 
 8 inline int read(){
 9     char ch=getchar(); int f=1,x=0;
10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
11     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
12     return f*x;
13 }
14 struct nd{int x,h;}; nd nds[maxn];
15 bool cmp(nd a,nd b){return a.x<b.x;}
16 int n,d,q1[maxn],q2[maxn],l,r,ans; bool unc[maxn];
17 int main(){
18     n=read(); d=read(); inc(i,1,n)nds[i].x=read(),nds[i].h=read(); sort(nds+1,nds+n+1,cmp); r=0; l=1;
19     inc(i,1,n){
20         while(l<=r&&nds[i].x-q1[l]>d)l++; if(l<=r&&q2[l]>=(nds[i].h<<1))unc[i]=1;
21         while(l<=r&&q2[r]<nds[i].h)r--; q1[++r]=nds[i].x; q2[r]=nds[i].h;
22     }
23     r=0; l=1;
24     for(int i=n;i>=1;i--){
25         while(l<=r&&q1[l]-nds[i].x>d)l++; if(l<=r&&q2[l]>=(nds[i].h<<1)&&unc[i])ans++;
26         while(l<=r&&q2[r]<nds[i].h)r--; q1[++r]=nds[i].x; q2[r]=nds[i].h;
27     }
28     printf("%d",ans); return 0;
29 }

20160812

原文地址:https://www.cnblogs.com/YuanZiming/p/5774486.html