acwing119袭击 经典分治,平面最近点对
求颜色不同的最近点对
给定 2N个点,每个点只能是红色或黑色,求最近点对(A,B),并且(A,B)颜色不能
相同
y总的视频讲的非常好了!!
但是还是想推荐一波福建师范大学的老师讲的最近点对视频(1个小时左右)
https://www.bilibili.com/video/BV1wT4y1G7HE?from=search&seid=1546110901679554585
本题和普通的最近点对不同点是要求颜色不同
只需在求dist的时候加判断即可
double dist(Point& A, Point& B) {
if(A.color == B.color) return 1e18; //颜色不同直接返回正无穷
return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}
然后就是分治法
如图:
在中间矩形框内最多
存在8
个点,
所以我们按y轴
归并排序后只需向后暴力8
个点即可
double closep(int lef, int rig) {
if(lef == rig) return 1e18;
if(lef+1 == rig) return dist(a[lef], a[rig]);
int mid = (lef + rig) >> 1;
double ret = closep(lef, mid);
ret = min(ret, closep(mid+1, rig));
#if 1
double mid_x = a[mid].x;
int i = lef, j = mid+1, sz = 0;
while(i<=mid && j<=rig) { //按y归并
if(a[i].y < a[j].y) T[++sz] = a[i++];
else T[++sz] = a[j++];
}
while(i <= mid) T[++sz] = a[i++];
while(j <= rig) T[++sz] = a[j++];
for(i=1, j=lef; i<=sz; i++, j++) a[j] = T[i];
sz = 0;
for(i=lef, j=lef; i<=rig; i++) //把所有矩形框内的点筛选出来到T[]里
if(fabs(a[i].x-mid_x) < ret) T[++sz] = a[i];
for(i=1; i<=sz; i++) //每个点只需向后面暴力8个点
for(j=i+1; j<=min(sz, i+8); j++)
ret = min(ret, dist(T[i], T[j]));
#endif
return ret;
}
完整代码如下
#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)4e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)
using namespace std;
#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
namespace FastIO{
char print_f[105];
void read() {}
void print() { putchar('\n'); }
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
x = 0;
char ch = getchar();
ll f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
ll p3=-1;
if(x<0) putchar('-'), x=-x;
do{
print_f[++p3] = x%10 + 48;
} while(x/=10);
while(p3>=0) putchar(print_f[p3--]);
putchar(' ');
print(oth...);
}
} // namespace FastIO
using FastIO::print;
using FastIO::read;
int n, m, Q, K;
//平面上的颜色不同的最近点对
//给定2N个点,每个点可能是红色,可能是黑色,求颜色不同的最近点对
struct Point {
double x, y;
bool color;
} a[MAXN], T[MAXN];
bool cmpx(Point& A, Point& B) {
return A.x==B.x ? A.y < B.y : A.x < B.x;
}
double dist(Point& A, Point& B) {
if(A.color == B.color) return 1e18; //颜色不同直接返回正无穷
return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}
double closep(int lef, int rig) {
if(lef == rig) return 1e18;
if(lef+1 == rig) return dist(a[lef], a[rig]);
int mid = (lef + rig) >> 1;
double ret = closep(lef, mid);
ret = min(ret, closep(mid+1, rig));
#if 1
double mid_x = a[mid].x;
int i = lef, j = mid+1, sz = 0;
while(i<=mid && j<=rig) { //按y归并
if(a[i].y < a[j].y) T[++sz] = a[i++];
else T[++sz] = a[j++];
}
while(i <= mid) T[++sz] = a[i++];
while(j <= rig) T[++sz] = a[j++];
for(i=1, j=lef; i<=sz; i++, j++) a[j] = T[i];
sz = 0;
for(i=lef, j=lef; i<=rig; i++) //把所有矩形框内的点筛选出来到T[]里
if(fabs(a[i].x-mid_x) < ret) T[++sz] = a[i];
for(i=1; i<=sz; i++) //每个点只需向后面暴力8个点
for(j=i+1; j<=min(sz, i+8); j++)
ret = min(ret, dist(T[i], T[j]));
#endif
return ret;
}
int main() {
#ifdef debug
freopen("test", "r", stdin);
clock_t stime = clock();
#endif
scanf("%d ", &Q);
while(Q--) {
scanf("%d ", &n);
for(int i=1; i<=n; i++) {
scanf("%lf %lf ", &a[i].x, &a[i].y);
a[i].color = 0;
}
for(int i=n+1; i<=(n<<1); i++) {
scanf("%lf %lf ", &a[i].x, &a[i].y);
a[i].color = 1;
}
sort(a+1, a+1+(n<<1), cmpx); //注意要先按y轴排序
double ans = closep(1, (n<<1));
printf("%.3lf\n", ans);
}
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}