袭击POJ3714

袭击POJ3714(待补)

思路

题干在这:POJ3714
就是求两组点之间的最近距离。分治+平面最近点对,但不知道我的代码为什么超时了……(待补)

tl代码

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#define MAX 100001
#define inf 2e9
using namespace std;
struct point {
	double x;
	double y;
};
point station[MAX] = { 0 };
point agent[MAX] = { 0 };
point eff[MAX] = { 0 };
double d = inf;
int n;
bool cmpxy(const point& a, const point& b) {
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}
double distance(int i, int j) {
	return sqrt((station[i].x - agent[j].x) * (station[i].x - agent[j].x) + (station[i].y - agent[j].y) * (station[i].y - agent[j].y));
}
int lower(int i) {                      
	int l = 1, r = n;
	while (l != r) {
		int mid = (l + r) / 2;
		if (agent[mid].x > station[i].x + d)
			l = mid+1;
		else
			r = mid;
	}
	return r;
}
double mindis(int begin, int end) {
	int mid = (begin + end) / 2;
	if (begin == end) {
		int i = lower(mid);
		for (; i <= n; i++) {
			if (station[i].x - agent[mid].x > d)
				break;
			d = min(d, distance(i, mid));
		}
		return d;
	}
	else {
		d = min(mindis(begin, mid), mindis(mid + 1, end));
		return d;
	}
}
int main() {
	int t;
	scanf_s("%d", &t);
	for (int i = 0; i < t; i++) {
		scanf_s("%d", &n);
		for (int i = 1; i <= n; i++) 
			scanf_s("%lf%lf", &station[i].x, &station[i].y);
			//cin >> station[i].x >> station[i].y;
		for (int i = 1; i <= n; i++)
			scanf_s("%lf%lf", &agent[i].x, &agent[i].y);
			//cin >> agent[i].x >> agent[i].y;
		d = inf;
		sort(station + 1, station + n + 1, cmpxy);
		sort(agent + 1, agent + n + 1, cmpxy);
		printf("%.3lf", mindis(1, n));
	}
}
发布了24 篇原创文章 · 获赞 0 · 访问量 356

猜你喜欢

转载自blog.csdn.net/qq_45616764/article/details/104198871