[Usaco2013 Nov]Crowded Cows

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. 
我是用了两个单调队列来做。d1是x左边牛的高度的单调递减队列,d2是x右边牛的高度的单调递减队列。(写得有点丑。。。)
接下来上代码:
{$inline on}
var
d1,d2,a,b:array[0..60000]of longint;
t1,w1,t2,w2,d,ans,now,ed,n,m,i,j,k:longint;
procedure swap(var a,b:longint);
var
t:longint;
begin
t:=a; a:=b; b:=t;
end;

procedure kp(l,r:longint);
var
mm,i,j,m,t:longint;
begin
i:=l;j:=r;m:=a[(l+r) div 2];mm:=b[(l+r) div 2];
while i<=j do
begin
   while (a[i]<m)or((m=a[i])and(b[i]<mm)) do inc(i);
   while (a[j]>m)or((m=a[j])and(b[j]>mm)) do dec(j);
   if i<=j then
      begin
          swap(a[i],a[j]);
          swap(b[i],b[j]);
          inc(i); dec(j);
      end;
end;
if i<r then  kp(i,r);
if j>l then   kp(l,j);
end;

procedure insert1(x:longint);inline;
begin
while (b[d1[w1]]<b[x])and(w1>=t1) do
  dec(w1);
inc(w1);
d1[w1]:=x;
end;

procedure insert2(x:longint);inline;
begin
while (b[d2[w2]]<b[x])and(w2>=t2) do
  dec(w2);
inc(w2);
d2[w2]:=x;
end;

procedure doit(x:longint);inline;
var
i,j,k:longint;
begin
while (t1<w1)and(a[x]-a[d1[t1]]>d) do
  inc(t1);
while (t2<w2)and((a[d2[t2]]-a[x]>d)or(a[d2[t2]]-a[x]<0))do
  inc(t2);
while (a[now]-a[x]<=d)and(now<=n) do
  begin
    insert2(now);
    inc(now);
  end;
if b[d1[t1]]>=b[x]*2 then
  if b[d2[t2]]>=b[x]*2 then
      inc(ans);
insert1(x);
end;

begin
readln(n,d);
for i:=1 to n do
readln(a[i],b[i]);
kp(1,n);
now:=2;    t1:=1; t2:=1;
for i:=1 to n do
  doit(i);
writeln(ans);
end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值