推荐一些能打开的网站/新闻稿发布平台
1278 相离的圆
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注
平面上有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<algorithm>
using namespace std;
struct node {
int p,flag;};
struct node A[100005];
int index=0;
bool cmp(struct node a,struct node b)
{if(a.p!=b.p)return a.p<b.p;else if (a.p==b.p)return a.flag==0;elsereturn false;
}
int main()
{int n,a,b;cin>>n;for (int i=0;i<n;i++){cin>>a>>b;A[++index].p=a-b;A[index].flag=0;A[++index].p=a+b;A[index].flag=1;}sort(A+1,A+index+1,cmp);int ans=0,sum=n;for (int i=1;i<=index;i++){if (!A[i].flag) sum--;else ans+=sum;}cout<<ans;return 0;
}