平面上有N个圆,他们的圆心都在X轴上,给出所有圆的圆心和半径,求有多少对圆是相离的。
例如:4个圆分别位于1, 2, 3, 4的位置,半径分别为1, 1, 2, 1,那么{1, 2}, {1, 3} {2, 3} {2, 4} {3, 4}这5对都有交点,只有{1, 4}是相离的。
Input
第1行:一个数N,表示圆的数量(1 <= N <= 50000) 第2 - N + 1行:每行2个数P, R中间用空格分隔,P表示圆心的位置,R表示圆的半径(1 <= P, R <= 10^9)
Output
输出共有多少对相离的圆。
Input示例
4 1 1 2 1 3 2 4 1
Output示例
1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=50000+100;
struct node
{
int l;
int r;
} a[maxn];
int b[maxn];
bool cmp(node x,node y)
{
if(x.l<y.l) return true;
else if(x.l==y.l && x.r<y.r) return true;
return false;
}
int main()
{
int ans,n,i,j,x,r,t1,t2,t3;
cin>>n;
for(i=0;i<n;i++) {
cin>>x>>r;
a[i].l=x-r;
a[i].r=x+r;
}
ans=0;
sort(a,a+n,cmp);
for(i=0;i<n;i++) b[i]=a[i].l;
for(i=0;i<n;i++) {
t1=a[i].r;
t2=upper_bound(b,b+n,t1)-b;
ans+=n-t2;
}
cout<<ans<<endl;
return 0;
}
本文介绍了一种计算平面上多个圆是否相离的方法。这些圆的圆心位于X轴上,通过输入每个圆的圆心坐标和半径,可以高效地计算出任意两个圆相离的对数。

1778

被折叠的 条评论
为什么被折叠?



