先快排,再按顺序判断。 //伟人 E-mail:abigwc@gmail.com QQ:270173053 欢迎交流// #include<stdio.h> struct Point { int x; int y; int dis1; int dis2; }P[100001],pivot; int Partition(int low,int high) { pivot=P[low]; while(low<high) { while(low<high&&P[high].dis1>=pivot.dis1) --high; P[low]=P[high]; while(low<high&&P[low].dis1<=pivot.dis1) ++low; P[high]=P[low]; } P[low]=pivot; return low; } void QuickSort(int low,int high) { int pivotloc=low; if(low<high) { pivotloc=Partition(low,high); QuickSort(low,pivotloc-1); QuickSort(pivotloc+1,high); } } int abs(int num) { if(num>=0) return num; else return -num; } int main() { FILE *in,*ou; int x1,y1,x2,y2,N,i,d2=0,result,total; if((in=fopen("missile10.in","r"))==NULL) { printf("Can not open the input file."); exit(0); } if((ou=fopen("missile.ou","w"))==NULL) { printf("Can not open the output file."); exit(0); } fscanf(in,"%d%d%d%d",&x1,&y1,&x2,&y2); fscanf(in,"%d",&N); for(i=0;i<N;i++) { fscanf(in,"%d%d",&P[i].x,&P[i].y); P[i].dis1=abs(P[i].x-x1)*abs(P[i].x-x1)+abs(P[i].y-y1)*abs(P[i].y-y1); P[i].dis2=abs(P[i].x-x2)*abs(P[i].x-x2)+abs(P[i].y-y2)*abs(P[i].y-y2); } QuickSort(0,N-1); result=P[N-1].dis1; for(i=N-1;i>0;i--) { if(P[i].dis2>d2) d2=P[i].dis2; total=P[i-1].dis1+d2; if(total<result) result=total; } printf(ou,"%d",result); fclose(in); fclose(ou); return 0; }