HDU 1007 Quoit Design(分治)

Posted AC_Arthur

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;



以上是关于HDU 1007 Quoit Design(分治)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1007 Quoit Design | 平面分治

HDU 1007 [Quoit Design] 分治

HDU 1007 Quoit Design计算几何/分治/最近点对

HDU1007Quoit Design(分治)

HDU 1007 Quoit Design(分治)

Quoit Design---hdu1007(最近点对问题 分治法)