Pro
Sol
把所有牛的身高按照高度排序方便剪枝。从大到小枚举,如果枚举的这个点满足在范围内且是它的二倍的话, 标记一下,记住一定要两个标记,因为范围是两边的,只有两个都满足,这头牛才是满足条件的。剪枝就是,因为我们是按照高度从大到小排序的,所以如果当前枚举到的这头牛不满足条件,后面的肯定不能是这头牛的二倍,这样就把后面多余的枚举给省去。
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct Cow {
long long x , hi;
bool operator < (const Cow &a) const {
return hi > a.hi;
}
};
Cow tag[50005];
long long n;
long long d , ans = 0;
long long myabs(long long x) {
return x>0?x:(-x);
}
int main() {
scanf("%d%lld",&n,&d);
for(long long i=1; i<=n; i++)
scanf("%lld%lld",&tag[i].x,&tag[i].hi);
sort(tag+1 , tag+n+1);
for(long long i=1; i<=n; i++) {
long long flag1 = 0 , flag2 = 0;
for(long long j=1; j<i; j++) {
if(tag[j].hi < tag[i].hi*2)
break;
if(myabs(tag[j].x-tag[i].x) <= d) {
if(tag[j].x > tag[i].x)
flag1 = 1;
if(tag[j].x < tag[i].x)
flag2 = 1;
if(flag1 && flag2) {
ans++;
break;
}
}
}
}
printf("%lld",ans);
return 0;
}