HDU 1007 Quoit Design(分治)

题目链接:点击打开链接

思路:

经典的分治法, 网上讲解很多我就不多说了, 这是nlognlogn复杂度, 大多数情况是够用的。。优化了一下排序函数, 跑了780ms

细节参见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <ctime>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const double INF = 1e15;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 1e5 + 10;
int T,n,m;
struct node {
    double x, y;
    node(int x=0, int y=0):x(x), y(y) {}
    bool operator < (const node& rhs) const {
        return y < rhs.y;
    }
}p[maxn], temp[maxn];
bool cmpx(const node& a, const node& b) {
    return a.x < b.x;
}
bool cmpy(const node& a, const node& b) {
    return a.y < b.y;
}
double dist(node &a, node &b) {
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
double solve(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;
    d = min(solve(l, mid), solve(mid+1, r));
    int cnt = 0;
    for(int i = mid; i >= l && p[mid].x - p[i].x <= d; i--) {
        temp[cnt++] = p[i];
    }
    for(int i = mid+1; i <= r && p[i].x - p[mid].x <= d; i++) {
        temp[cnt++] = p[i];
    }
    sort(temp, temp+cnt);
    for(int i = 0; i < cnt; i++) {
        for(int j = i + 1; j < cnt && temp[j].y - temp[i].y < d; j++)
            d = min(d, dist(temp[i], temp[j]));
    }
    return d;
}
int main() {
    while(~scanf("%d", &n) && n) {
        for(int i = 1; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
        sort(p+1, p+n+1, cmpx);
        printf("%.2f\n", solve(1, n)/2.0);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值