标签:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 46340 Accepted Submission(s): 12084
这题可以分治:
首先按照x方向排序,然后分别对 两边求最短距离。
然后按照y排序,统计距离分割点-d 和+d之间的点,算了,忘了。
但是可以直接排序,然后枚举相邻三个点之间到距离。这三个点的组合总计三个距离,可以证明最短到距离一定存在于某个
三元组里。
#include<iostream> #include<stdio.h> #include<algorithm> #include<math.h> using namespace std; const int maxx=100005; struct Node{ double x; double y; }node[maxx]; bool cmp(Node a,Node b){ if(a.x!=b.x) return a.x<b.x; return a.y<b.y; } double dis(Node a,Node b){ double dx,dy; dx=a.x-b.x; dy=a.y-b.y; double ans=dx*dx+dy*dy; return sqrt(ans); } double min(double a,double b,double c){ double tmp=0.0; tmp=(a<=b)?a:b; tmp= (tmp<=c)?tmp:c; //cout<<a<<" "<<b<<" "<<c<<" "<<tmp<<endl; return tmp; } int main(){ int n; while(scanf("%d",&n)&&n!=0){ for(int i=0;i<n;i++){ scanf("%lf%lf",&node[i].x,&node[i].y); } sort(node,node+n,cmp); double ans=1000000.0; if(n==1){ printf("0.00\n"); continue; }else if(n==2){ printf("%.2lf\n",dis(node[0],node[1])/2); continue; } double tmp; for(int i=1;i<n-1;i++){ tmp=min(dis(node[i-1],node[i]),dis(node[i],node[i+1]),dis(node[i-1],node[i+1])); ans=min(ans,tmp); } ans=ans/2; printf("%.2lf\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/superxuezhazha/p/5693071.html