当前位置:编程学习 > C/C++ >>

hdu 1007_平面最近点对模板

简单题目,直接模板上
http://acm.hdu.edu.cn/showproblem.php?pid=1007   Quoit Design
O(n * log(n) *log(n) )的复杂度
[cpp] view plaincopy
#include <cstdio>  
#include <ctime>  
#include <cstdlib>  
#include <cstring>  
#include <queue>  
#include <string>  
#include <set>  
#include <stack>  
#include <map>  
#include <cmath>  
#include <vector>  
#include <iostream>  
#include <algorithm>  
#include <bitset>  
#include <fstream>  
using namespace std;  
  
  
//LOOP  
#define FF(i, a, b) for(int i = (a); i < (b); ++i)  
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)  
#define FED(i, b, a) for(int i = (b); i>= (a); --i)  
#define REP(i, N) for(int i = 0; i < (N); ++i)  
#define CLR(A,value) memset(A,value,sizeof(A))  
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)  
  
  
//OTHER  
#define SZ(V) (int)V.size()  
#define PB push_back  
#define MP make_pair  
#define all(x) (x).begin(),(x).end()  
  
  
//INPUT  
#define RI(n) scanf("%d", &n)  
#define RII(n, m) scanf("%d%d", &n, &m)  
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)  
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)  
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)  
#define RS(s) scanf("%s", s)  
  
  
//OUTPUT  
#define WI(n) printf("%d\n", n)  
#define WS(n) printf("%s\n", n)  
  
  
//debug  
//#define online_judge  
#ifndef online_judge  
#define debugt(a) cout << (#a) << "=" << a << " ";  
#define debugI(a) debugt(a) cout << endl  
#define debugII(a, b) debugt(a) debugt(b) cout << endl  
#define debugIII(a, b, c) debugt(a) debugt(b) debugt(c) cout << endl  
#define debugIV(a, b, c, d) debugt(a) debugt(b) debugt(c) debugt(d) cout << endl  
#else  
#define debugI(v)  
#define debugII(a, b)  
#define debugIII(a, b, c)  
#define debugIV(a, b, c, d)  
#endif  
  
#define sqr(x) (x) * (x)  
typedef long long LL;  
typedef unsigned long long ULL;  
typedef vector <int> VI;  
const int INF = 0x3f3f3f3f;  
const double EPS = 1e-10;  
const int MOD = 100000007;  
const int MAXN = 100010;  
const double PI = acos(-1.0);  
  
  
inline int dcmp(double x)  
{  
    if(fabs(x) < EPS) return 0;  
    else return x < 0 ? -1 : 1;  
}  
struct Point  
{  
    double x, y;  
    Point(double x=0, double y=0):x(x),y(y) { }  
    inline void read()  
    {  
        scanf("%lf%lf", &x, &y);  
    }  
};  
  
  
  
//最近点对  
Point point[MAXN];  
int tmpt[MAXN], Y[MAXN];  
  
inline bool cmpxy(Point a, Point b)  
{  
    if(a.x != b.x)  
        return a.x < b.x;  
    return a.y < b.y;  
}  
  
inline bool cmpy(int a, int b)  
{  
    return point[a].y < point[b].y;  
}  
  
inline double dist(int x, int y)  
{  
    Point& a = point[x], &b = point[y];  
    return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));  
}  
  
double Closest_Pair(int left, int right)  
{  
    double d = 1e100;  
    if(left == right)  
        return d;  
    if(left + 1 == right)  
        return dist(left, right);  
    int mid = (left + right) >> 1;  
    double d1 = Closest_Pair(left, mid);  
    double d2 = Closest_Pair(mid + 1, right);  
    d = min(d1, d2);  
    int k = 0;  
    //分离出宽度为d的区间  
    FE(i, left, right)  
    {  
        if(fabs(point[mid].x - point[i].x) <= d)  
            tmpt[k++] = i;  
    }  
    sort(tmpt, tmpt + k, cmpy);  
    //线性扫描  
    REP(i, k)  
    {  
        for(int j = i + 1; j < k && point[tmpt[j]].y-point[tmpt[i]].y < d; j++)  
        {  
            double d3 = dist(tmpt[i],tmpt[j]);  
            if(d > d3)  
                d = d3;  
        }  
    }  
    return d;  
}  
  
int main()  
{  
    //freopen("input.txt", "r", stdin);  
    int n;  
    while (~RI(n) && n)  
    {  
        REP(i, n)  
        {  
            point[i].read();  
        }  
        sort(point, point + n, cmpxy);  
        printf("%.2f\n", Closest_Pair(0, n - 1) / 2);  
    }  
    return 0;  
}  
 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,