http://codeforces.com/problemset/problem/527/D
题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集合中的点两两之间存在边
化开有
xi-xj>=wi+wj ==> wj+xj<=wi-xi
xj-xi>=wi+wj ==> wi+wx<=wj-xj
发现本质相同,我们按x+w排序,从最小的往大的贪心连边,因为如果有一条边,那么比它小的也全部可以连到边。
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<cstring> 5 #include<iostream> 6 struct node{ 7 int x,w; 8 }p[500005]; 9 int n; 10 int read(){ 11 int t=0,f=1;char ch=getchar(); 12 while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} 13 while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();} 14 return t*f; 15 } 16 bool cmp(node a,node b){ 17 return a.w+a.x<b.x+b.w; 18 } 19 int main(){ 20 n=read(); 21 for (int i=1;i<=n;i++) 22 p[i].x=read(),p[i].w=read(); 23 std::sort(p+1,p+1+n,cmp); 24 int ans=1,t=p[1].w+p[1].x; 25 for (int i=2;i<=n;i++){ 26 if (t<=p[i].x-p[i].w){ 27 ans++; 28 t=p[i].x+p[i].w; 29 } 30 } 31 printf("%d ",ans); 32 return 0; 33 }