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.
时间: 2024-10-10 05:49:06

3314: [Usaco2013 Nov]Crowded Cows的相关文章

BZOJ 3314: [Usaco2013 Nov]Crowded Cows( 单调队列 )

从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ---------------------------------------------------------------------------- #include<bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; ++i) #define clr(x, c) memset(x, c, sizeof(x)) #define forea

BZOJ3314: [Usaco2013 Nov]Crowded Cows

3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 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

bzoj3312: [Usaco2013 Nov]No Change

题意: K个硬币,要买N个物品.K<=16,N<=1e5 给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的.现希望买完所需要的东西后,留下的钱越多越好,如果不能完成购买任务,输出-1 =>K那么小...那么我们可以想到二进制枚举状态...然后转移...好像算不上状压dp吧...时间复杂度O(K2^Klogn) #include<cstdio> #include<cstring> #include<cctype&g

BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛( dp )

树形dp..水 ------------------------------------------------------------------------ #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; i++ ) #define clr( x , c )

2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛

2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 252  Solved: 185[Submit][Status] Description 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统,里面有N-1条路,每条路连接了一对编号为C1和C2的奶牛

BZOJ3016: [Usaco2012 Nov]Clumsy Cows

3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 71  Solved: 52[Submit][Status] Description Bessie the cow is trying to type a balanced string of parentheses into her new laptop, but she is sufficiently clumsy (due to h

3016: [Usaco2012 Nov]Clumsy Cows

3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 91  Solved: 69[Submit][Status][Discuss] Description Bessie the cow is trying to type a balanced string of parentheses into her new laptop, but she is sufficiently clumsy

BZOJ3315: [Usaco2013 Nov]Pogo-Cow

3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 143  Solved: 79[Submit][Status] Description In an ill-conceived attempt to enhance the mobility of his prize cow Bessie, Farmer John has attached a pogo stick to each of Bes

bzoj 3312: [Usaco2013 Nov]No Change

3312: [Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 <= K <= 16), each with value in the range 1..100,000,000. FJ would like to make a sequence of N purchases (1 &