题目链接 | 难度等级 | 完成状态 | 完成分数 | 最后编辑时间 | 需要注意 |
---|---|---|---|---|---|
Quoit Design | ★★☆☆☆ | 答案正确 | 100 | 2015-02-06 21:36:45 | 无 |
求平面内的最近点对。
1007.cpp代码已折叠
展开折叠内容
|
---|
#include<cstdio> #include<cmath> #include<climits> #include<algorithm> using namespace std; #define ci const int & #define cd const double & #define cv const Vector2 & class Vector2 { public: double x,y; Vector2(cd _x,cd _y):x(_x),y(_y){}; Vector2():x(0),y(0){}; friend Vector2 operator -(cv lhs,cv rhs) { return Vector2(lhs.x-rhs.x,lhs.y-rhs.y); } operator double() const { return sqrt(x*x+y*y); } } p[120000]={}; Vector2 *inRange[120000]={}; size_t k; bool cmpy(Vector2 *x,Vector2 *y){return x->y<y->y;} bool cmpx(const Vector2 &x,const Vector2 &y){return x.x==y.x?x.y<y.y:x.x<y.x;} double getAns(ci l,ci r) { if(l==r)return 1e+9; if(l==r-1)return double(p[r]-p[l]); int m=(l+r)>>1; double rt=min(getAns(l,m),getAns(m+1,r)); k=0; for(int i=l;i<=r;++i) { if(abs(p[m].x-p[i].x)<=rt) inRange[++k]=&p[i]; } sort(inRange+1,inRange+k+1,cmpy); for(int i=1;i<=k;++i) for(int j=i+1;j<=k&&inRange[j]->y-inRange[i]->y<rt;++j) rt=min(double(*inRange[i]-*inRange[j]),rt); return rt; } int main() { while(1) { int n; scanf("%d",&n); if(!n)return 0; for(int i=1;i<=n;++i) { scanf("%lf%lf",&p[i].x,&p[i].y); } sort(p+1,p+n+1,cmpx); printf("%.2lf\n",getAns(1,n)/2); } } |