您的位置:首页 > 其它

51nod 1278 相离的圆 (排序)

2015-11-04 00:28 309 查看
平面上有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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: