51nod 1278 相离的圆(二分)
题解: 将圆的最小最大用结构体表示,然后以最小的用sort排下序,然后每个用二分找到最小的符合的下标j,算一下每个符合的个数(即n-j)。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define INF 0x3f3f3f
#define pi acos(-1.0)
#define MAX 1000010
#define N 50010
struct point
{
int x,y;
}a[N];
bool cmp(point a,point b)
{
return a.x < b.x;
}
int bsearch(int begin, int end, int e)
{
int mid, left = begin, right = end;
while(left <= right)
{
mid = (left+right) >> 1;
if(a[mid].x > e)
right = mid-1;
else
left = mid+1;
}
return left;
}
int main()
{
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
int mid,r;
scanf("%d%d",&mid,&r);
a[i].x = mid-r;
a[i].y = mid+r;
}
sort(a,a+n,cmp);
int ans = 0;
for(int i = 0; i < n; i++)
{
int j = bsearch(i+1,n-1,a[i].y);
ans += n-j;
}
printf("%d\n",ans);
return 0;
}