3314: [Usaco2013 Nov]Crowded Cows

3314: [Usaco2013 Nov]Crowded Cows

Time Limit: 1 Sec  Memory Limit: 128 MB

Submit: 111  Solved: 79

[Submit][Status][Discuss]

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

题解:一个萌萌哒RMQ问题(HansBug:吐槽下翻译——输入里面明明说了the location is distinct,也就是说每只牛坐标唯一,这样子虽然没有实质性变化,但是简化了不少问题,毕竟题目说是左边和右边的牛,没说此位置上的= =),将坐标排序,然后就是区间查询最大值啦,然后没了

 1 /**************************************************************
 2     Problem: 3314
 3     User: HansBug
 4     Language: Pascal
 5     Result: Accepted
 6     Time:608 ms
 7     Memory:6008 kb
 8 ****************************************************************/
 9  
10 var
11    i,j,k,l,m,n:longint;
12    a,b:array[0..100000,1..2] of longint;
13    c:array[0..17,0..60000] of longint;
14 function max(x,y:longint):longint;
15          begin
16               if x>y then max:=x else max:=y;
17          end;
18 procedure swap(var x,y:longint);
19           var z:longint;
20           begin
21                z:=x;x:=y;y:=z;
22           end;
23 procedure sort(l,r:longint);
24           var i,j,x:longint;
25           begin
26                i:=l;j:=r;x:=a[(l+r) div 2,1];
27                repeat
28                      while a[i,1]<x do inc(i);
29                      while a[j,1]>x do dec(j);
30                      if i<=j then
31                         begin
32                              swap(a[i,1],a[j,1]);
33                              swap(a[i,2],a[j,2]);
34                              inc(i);dec(j);
35                         end;
36                until i>j;
37                if i<r then sort(i,r);
38                if l<j then sort(l,j);
39           end;
40 function getmax(x,y:longint):longint;
41          var i:longint;
42          begin
43               if y<x then exit(-1);
44               i:=trunc(ln(y-x+1)/ln(2));
45               exit(max(c[i,x],c[i,y-trunc(exp(ln(2)*i))+1]));
46          end;
47 begin
48      readln(n,m);
49      for i:=1 to n do readln(a[i,1],a[i,2]);
50      sort(1,n);
51      for i:=1 to n do c[0,i]:=a[i,2];
52      for i:=1 to trunc(ln(n)/ln(2)+1) do
53          for j:=1 to n-trunc(exp(ln(2)*i))+1 do
54              c[i,j]:=max(c[i-1,j],c[i-1,j+trunc(exp(ln(2)*(i-1)))]);
55      k:=1;fillchar(b[1],sizeof(b[1]),0);
56      for i:=2 to n do
57          begin
58               while (a[k,1]+m)<a[i,1] do inc(k);
59               if getmax(k,i-1)>=(a[i,2]*2) then b[i,1]:=1;
60          end;
61      k:=n;fillchar(b[1],sizeof(b[1]),0);
62      for i:=n-1 downto 1 do
63          begin
64               while (a[k,1]-m)>a[i,1] do dec(k);
65               if getmax(i+1,k)>=(a[i,2]*2) then b[i,2]:=1;
66          end;
67      l:=0;for i:=1 to n do inc(l,(b[i,1]+b[i,2]) div 2);
68      writeln(l);
69      readln;
70  
71 end.

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏HansBug's Lab

1634: [Usaco2007 Jan]Protecting the Flowers 护花

1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit:...

33080
来自专栏HansBug's Lab

2020: [Usaco2010 Jan]Buying Feed, II

2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec  Memory Limit: 64 MB Subm...

28470
来自专栏HansBug's Lab

3433: [Usaco2014 Jan]Recording the Moolympics

3433: [Usaco2014 Jan]Recording the Moolympics Time Limit: 10 Sec  Memory Limit:...

28040
来自专栏HansBug's Lab

1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场

1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec  Memory Limit: 64 ...

34060
来自专栏HansBug's Lab

3892: [Usaco2014 Dec]Marathon

3892: [Usaco2014 Dec]Marathon Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 1...

35760
来自专栏HansBug's Lab

1644: [Usaco2007 Oct]Obstacle Course 障碍训练课

1644: [Usaco2007 Oct]Obstacle Course 障碍训练课 Time Limit: 5 Sec  Memory Limit: 64 M...

29460
来自专栏HansBug's Lab

1753: [Usaco2005 qua]Who's in the Middle

1753: [Usaco2005 qua]Who's in the Middle Time Limit: 5 Sec  Memory Limit: 64 MB ...

336110
来自专栏HansBug's Lab

1653: [Usaco2006 Feb]Backward Digit Sums

1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MB ...

28040
来自专栏HansBug's Lab

1682: [Usaco2005 Mar]Out of Hay 干草危机

1682: [Usaco2005 Mar]Out of Hay 干草危机 Time Limit: 5 Sec  Memory Limit: 64 MB Subm...

35240
来自专栏HansBug's Lab

1601: [Usaco2008 Oct]灌水

1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 1342  S...

30670

扫码关注云+社区

领取腾讯云代金券