题意:有若干组数据,每组数据第一行为n,后面有n行,每行有两个数据,代表一个点的坐标x,y。求这些点之间最近两点的距离的一半,结果保留两位小数。
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007
思路:分治,将所有点分成两部分,有些特殊情况,一点在左区域,另一点在右区域,在已知的最短距离一半的范围内查找是否存在更短的点对(参考:http://www.cnblogs.com/AdaByron/archive/2011/10/07/2200966.html)。
注意点:cin关闭流同步也会超时。用scanf能过。
以下为AC代码:
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
12928122 | 2015-02-10 15:08:47 | Accepted | 1007 | 1216MS | 4340K | 2265 B | G++ | luminous11 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
//const double pi = acos(-1);
const double eps = 1e-10;
//const int dir[[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
const int maxn = 100010;
const double inf = 1e+20;
struct point{
double x, y;
};
point p[maxn], tmpt[maxn];
inline double dist ( point a, point b ){
return sqrt ( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
bool cmpxy ( point a, point b ){
if ( a.x != b.x )
return a.x < b.x;
else
return a.y < b.y;
}
bool cmpy ( point a, point b ){
return a.y < b.y;
}
double closest_pair ( int l, int r ){
double d = inf;
if ( l == r )return d;
if ( l + 1 == r )return dist( p[l], p[r] );
int mid = ( l + r ) >> 1;
double d1 = closest_pair( l, mid );
double d2 = closest_pair( mid + 1, r );
d = min ( d1, d2 );
int k = 0;
for ( int i = l; i <= r; i ++ ){
if ( fabs( p[mid].x - p[i].x ) <= d ){
tmpt[k++] = p[i];
}
}
sort ( tmpt, tmpt + k, cmpy );
for ( int i = 0; i < k; i ++ ){
for ( int j = i +1; j < k && tmpt[j].y - tmpt[i].y < d; j ++ ){
d = min ( d, dist ( tmpt[i], tmpt[j] ) );
}
}
return d;
}
int main()
{
ios::sync_with_stdio( false );
int n;
while ( scanf ( "%d", &n ) && n ){
//while ( cin >> n && n ){
for ( int i = 0; i < n; i ++ ){
scanf ( "%lf%lf", &p[i].x, &p[i].y );
//cin >> p[i].x >> p[i].y;
}
sort ( p, p + n, cmpxy );
printf ( "%0.2f\n", closest_pair( 0, n - 1 ) / 2 );
//cout << fixed << setprecision(2) << closest_pair( 0, n - 1 ) / 2 << endl;
}
return 0;
}