平面最近点对(加强版)
Posted zxyqzy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了平面最近点对(加强版)相关的知识,希望对你有一定的参考价值。
题目描述
给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的
输入输出格式
输入格式:第一行:n;2≤n≤200000
接下来n行:每行两个实数:x y,表示一个点的行坐标和列坐标,中间用一个空格隔开。
输出格式:仅一行,一个实数,表示最短距离,精确到小数点后面4位。
输入输出样例
说明
0<=x,y<=10^9
就当练手了;
分治就行了;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize(2) using namespace std; #define maxn 300005 #define inf 0x3f3f3f3f #define INF 9999999999 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9 + 7; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-3 typedef pair<int, int> pii; #define pi acos(-1.0) const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) typedef pair<int, int> pii; inline ll rd() { ll x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == ‘-‘) f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } ll sqr(ll x) { return x * x; } /*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans; } */ ll qpow(ll a, ll b, ll c) { ll ans = 1; a = a % c; while (b) { if (b % 2)ans = ans * a%c; b /= 2; a = a * a%c; } return ans; } struct node { int set; double x, y; }a[maxn],b[maxn]; bool cmpx(node x, node y) { return x.x < y.x; } bool cmpy(node x, node y) { return x.y < y.y; } double dis(node a, node b) { return sqrt(1.0*(a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)); } double sol(int l, int r) { if (l == r)return inf; int mid = (l + r) >> 1; double ans = inf * 1.0; ans = min(sol(l, mid), sol(mid + 1, r)); int cnt = 0; for (int i = l; i <= r; i++) { if (fabs(a[i].x - a[mid].x) <= ans)b[++cnt] = a[i]; } sort(b + 1, b + 1 + cnt, cmpy); for (int i = 1; i <= cnt; i++) { for (int j = i + 1; j <= cnt; j++) { if (b[j].y - b[i].y > ans)break; ans = min(ans, dis(b[i], b[j])); } } return ans; } int n; int main() { //ios::sync_with_stdio(0); rdint(n); for (int i = 1; i <= n; i++) { rdlf(a[i].x); rdlf(a[i].y); a[i].set = 0; } sort(a + 1, a + 1 + n, cmpx); printf("%.4lf ", sol(1, n)*1.0); return 0; }
以上是关于平面最近点对(加强版)的主要内容,如果未能解决你的问题,请参考以下文章