题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1007
const double MAX = 10e100;
const double eps = 0.00001;
double closest(Point *, Point *, Point *, int, int);
double dis(Point, Point);
int cmp_x(const void *, const void*);
int cmp_y(const void *, const void*);
int merge(Point *, Point *, int, int, int);
inline double min(double, double);
scanf("%lf%lf", &(a[i].x), &(a[i].y));
qsort(a, n, sizeof(a[0]), cmp_x);
memcpy(b, a, n *sizeof(a[0]));
qsort(b, n, sizeof(b[0]), cmp_y);
d = closest(a, b, c, 0, n - 1);
double closest(Point a[], Point b[], Point c[], int p, int q)
double x1 = dis(a[p], a[q]);
double x2 = dis(a[p + 1], a[q]);
double x3 = dis(a[p], a[p + 1]);
for (i = p, j = p, k = m + 1; i <= q; i++)
//数组c左半部保存划分后左部的点, 且对y是有序的.
d1 = closest(a, c, b, p, m);
d2 = closest(a, c, b, m + 1, q);
merge(b, c, p, m, q); //数组c左右部分分别是对y坐标有序的, 将其合并到b.
for (i = p, k = p; i <= q; i++)
if (fabs(b[i].x - b[m].x) < dm)
//找出离划分基准左右不超过dm的部分, 且仍然对y坐标有序.
for (j = i + 1; j < k && c[j].y - c[i].y < dm; j++)
double temp = dis(c[i], c[j]);
double dis(Point p, Point q)
double x1 = p.x - q.x, y1 = p.y - q.y;
return sqrt(x1 *x1 + y1 * y1);
int merge(Point p[], Point q[], int s, int m, int t)
for (i = s, j = m + 1, k = s; i <= m && j <= t;)
memcpy(q + s, p + s, (t - s + 1) *sizeof(p[0]));
int cmp_x(const void *p, const void *q)
double temp = ((Point*)p)->x - ((Point*)q)->x;
else if (fabs(temp) < eps)
int cmp_y(const void *p, const void *q)
double temp = ((Point*)p)->y - ((Point*)q)->y;
else if (fabs(temp) < eps)
inline double min(double p, double q)
return (p > q) ? (q): (p);
版本二:(使用STL,未能AC掉,还得继续测试。。。)
const double eps = 0.00001;
point(double x1=0.0f,double y1=0.0f,int id=0):x(x1),y(y1),id(id)
double Distance(const point& rhs)
return sqrt(dx*dx+dy*dy);
friend ostream& operator << (ostream& out,const point& rhs)
out<<rhs.x<<" "<<rhs.y<<" id is:"<<rhs.id<<endl;
bool operator < (const point& rhs)
point& operator = (const point& rhs)
bool cmp_onY(const point& p,const point& q)
return p.getY()<q.getY();
double min(const double& a,const double &b)
void printVector(vector<point>& v)
vector<point>::iterator iter;
for(iter = v.begin();iter!=v.end();++iter)
int merge(vector<point>& a,vector<point>& b,int begin,int mid,int end)
for (i = begin, j = mid + 1, k = begin; i <= mid && j <= end;)
if (b[i].getY() > b[j].getY())
vector<point>::iterator iter = a.begin();
copy(iter+begin,iter+(end-begin+1),b.begin());
double Closest(vector<point>& a,vector<point>& b,vector<point>& c,int begin,int last)
return a[begin].Distance(a[last]);
double t1 = a[begin].Distance(a[last]);
double t2 = a[begin].Distance(a[begin+1]);
double t3 = a[begin+1].Distance(a[last]);
int mid = (begin+last)/2;//分割点
for(i = begin,j = begin,k = mid+1;i<=last;++i)
d1 = Closest(a,c,b,begin,mid);
d2 = Closest(a,c,b,mid+1,last);
merge(b,c,begin,mid,last);
for(i=begin,k=begin;i<=last;++i)
if(fabs(b[i].getX()-b[mid].getX()) < dm)
for(j = i+1;j<k&&(c[j].getY()-c[i].getY()<dm);j++)
double temp = c[i].Distance(c[j]);
double x1,y1,result=0.0f;
while(cin>>nPoints&&nPoints!=0)
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end(),cmp_onY);
result = Closest(v1,v2,v3,0,nPoints-1);
cout<<setiosflags(ios::fixed)<<setprecision(2);
v1.erase(v1.begin(),v1.end());
v2.erase(v2.begin(),v2.end());
v3.erase(v3.begin(),v3.end());
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/12/26/1015609.html,如需转载请自行联系原作者