I think:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> struct port { long long x,y,l,r; }; int cmp(const void *a,const void *b) { struct port *x=(struct port *)a; struct port *y=(struct port *)b; return x->l - y->l; } struct port p[1000000]; int main(void) { long long wayLen,d,i,v,sum; long long c,tem; while(scanf("%lld%lld%lld",&wayLen,&d,&v)!=EOF) { sum=1; for(i=0; i<v; i++) { scanf("%lld%lld",&p[i].x,&p[i].y); c=sqrt(d*d-p[i].y*p[i].y); p[i].r=c+p[i].x; p[i].l=p[i].x-c; } qsort(p,v,sizeof(p[0]),cmp); tem=p[0].r; for(i=1; i<v; i++) { if(p[i].l<=tem) continue; else { tem=p[i].r; ++sum; } } printf("%lld ",sum); } return 0; }