HDOJ 1007 Quoit Design

2 篇文章 0 订阅
1 篇文章 0 订阅

题意:有若干组数据,每组数据第一行为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 IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
129281222015-02-10 15:08:47Accepted10071216MS4340K2265 BG++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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值