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
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
OUTPUT DETAILS: The cows at positions x=5 and x=6 are both crowded.
HINT
Source
题解:一个萌萌哒RMQ问题(HansBug:吐槽下翻译——输入里面明明说了the location is distinct,也就是说每只牛坐标唯一,这样子虽然没有实质性变化,但是简化了不少问题,毕竟题目说是左边和右边的牛,没说此位置上的= =),将坐标排序,然后就是区间查询最大值啦,然后没了
/**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ var
i,j,k,l,m,n:longint;
a,b:array[..,..] of longint;
c:array[..,..] of longint;
function max(x,y:longint):longint;
begin
if x>y then max:=x else max:=y;
end;
procedure swap(var x,y:longint);
var z:longint;
begin
z:=x;x:=y;y:=z;
end;
procedure sort(l,r:longint);
var i,j,x:longint;
begin
i:=l;j:=r;x:=a[(l+r) div ,];
repeat
while a[i,]<x do inc(i);
while a[j,]>x do dec(j);
if i<=j then
begin
swap(a[i,],a[j,]);
swap(a[i,],a[j,]);
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if l<j then sort(l,j);
end;
function getmax(x,y:longint):longint;
var i:longint;
begin
if y<x then exit(-);
i:=trunc(ln(y-x+)/ln());
exit(max(c[i,x],c[i,y-trunc(exp(ln()*i))+]));
end;
begin
readln(n,m);
for i:= to n do readln(a[i,],a[i,]);
sort(,n);
for i:= to n do c[,i]:=a[i,];
for i:= to trunc(ln(n)/ln()+) do
for j:= to n-trunc(exp(ln()*i))+ do
c[i,j]:=max(c[i-,j],c[i-,j+trunc(exp(ln()*(i-)))]);
k:=;fillchar(b[],sizeof(b[]),);
for i:= to n do
begin
while (a[k,]+m)<a[i,] do inc(k);
if getmax(k,i-)>=(a[i,]*) then b[i,]:=;
end;
k:=n;fillchar(b[],sizeof(b[]),);
for i:=n- downto do
begin
while (a[k,]-m)>a[i,] do dec(k);
if getmax(i+,k)>=(a[i,]*) then b[i,]:=;
end;
l:=;for i:= to n do inc(l,(b[i,]+b[i,]) div );
writeln(l);
readln; end.