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
Output
Input示例
Output示例
例如: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; }
相关文章推荐
- HDU 3072 Intelligence System (tarjan-scc缩点 + 最小树形图)
- caffe layer `EltwiseLayer`
- C++除法实现方式及负数取模
- iOS-事务相关
- Mysql 无法登陆 Error 1045
- Sql Server插入随机数
- 数据保存(三):数据库
- Kafka单机安装
- log4j源码阅读
- java架构师之路:JAVA程序员必看的15本书的电子版下载地址
- ruby 第五次作业 part 1(分类、排序)
- poj 2299(离散化+树状数组)
- UESTC 1217(树状数组+离散化)
- redis 安装与配置
- ubuntu上安装配置mysql
- 为初次使用linux设置 root密码
- spark sql DataFrame操作
- 对字符串中的字符进行统计
- css布局的学习笔记
- 实现 strcmp