poj3714——分治法求最近点对

题目链接:http://poj.org/problem?id=3714

After successive failures in the battles against the Union, the Empire retreated to its last stronghold. Depending on its powerful defense system, the Empire repelled the six waves of Union's attack. After several sleepless nights of thinking, Arthur, General of the Union, noticed that the only weakness of the defense system was its energy supply. The system was charged by N nuclear power stations and breaking down any of them would disable the system.

The general soon started a raid to the stations by N special agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur soon realized that he needed to rearrange the plan. The first thing he wants to know now is that which agent is the nearest to any power station. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?

Input

The first line is a integer T representing the number of test cases.
Each test case begins with an integer N (1 ≤ N ≤ 100000).
The next N lines describe the positions of the stations. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the station.
The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the agent.  

Output

For each test case output the minimum distance with precision of three decimal placed in a separate line.

Sample Input

2
4
0 0
0 1
1 0
1 1
2 2
2 3
3 2
3 3
4
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

Sample Output

1.414
0.000

题目翻译:

在反对联邦的战斗中接连失败后,帝国撤退到其最后一个据点。依靠强大的防御系统,帝国击退了六波联盟的攻击。经过几个不眠之夜的思考,联邦将军阿瑟注意到防御系统的唯一弱点是它的能源供应。该系统‎‎由N‎‎核电站充电,并打破其中任何一个将禁用系统。‎

‎将军很快开始对驻地‎‎的N‎‎特工进行突袭,他们被扔进了据点。不幸的是,由于帝国空军的攻击,他们未能降落在预期的位置。作为一名经验丰富的将军,亚瑟很快意识到他需要重新安排计划。他现在想知道的第一件事是哪个代理是最接近任何发电站的。首席官,你能帮助将军计算代理人和车站之间的最小距离吗?‎

‎输入‎

‎第一行是表示测试用例数的整数‎‎T。‎
‎每个测试用例都以整数‎‎N‎‎ (1 = ‎‎N‎‎ = 100000) 开头。‎
‎接下来的‎‎N‎‎行描述站的位置。每条线由两个整数‎‎X‎‎ (0 = ‎‎X‎‎ = 100000000) 和‎‎Y‎‎ (0 = ‎‎Y‎‎ = 100000000) 组成,指示站的位置。‎
‎以下‎‎N‎‎行描述代理的位置。每行由两个整数‎‎X‎‎ (0 = ‎‎X‎‎ = 100000000) 和‎‎Y‎‎ (0 = ‎‎Y‎‎ = 100000000) 组成,指示代理的位置。‎

‎输出‎

‎对于每个测试用例,输出最小距离,精度为三位十进制放置在单独的行中。

题意理解:

给你n个x类型的点,以及n个y类型的点

求不在同一个类型的点的最近距离。

 

分治法求最近点对的经典题,只要理解了分治法的代码,这个题就很好做出来。

只需要在分治法求最近点对模板里面加一句判断是否属于同一类型的就行了。

相当于分治法考虑的情况里面减去在同一多边形的里面的最近距离的情况。

这里推几篇比较好的分治法求最近点对的解析

https://blog.csdn.net/qq_28666193/article/details/53351482(代码比较好)

https://www.cnblogs.com/detrol/p/7533606.html(讲解的很好)

#include<iostream>
#include<cmath>
#include<algorithm> 
using namespace std;
const int maxn=2e5+5;
const double inf=1e50;
struct Point {
    double x,y;
    int flag;
    bool operator<(const Point &a)const {
        return x<a.x;
    }
}p[maxn],q[maxn];
double Dist(const Point &p1, const Point &p2) {
    return sqrt((p1.x - p2.x)*(p1.x - p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
bool cmp2(Point a,Point b){
	return a.y<b.y;
}
double ClosestPointPair(int l,int r){
	if(l==r) return inf;
	int cnt=0;
	int mid=(l+r)>>1;
	double ans=min(ClosestPointPair(l,mid),ClosestPointPair(mid+1,r));
	for(int i=l;i<=r;i++)
		if(fabs(p[i].x-p[mid].x)<=ans)
			q[cnt++]=p[i];
	sort(q,q+cnt,cmp2);
	for(int i=0;i<cnt;i++){
		for(int j=i+1;j<cnt;j++){
			if(q[i].flag==q[j].flag) continue;
			if(q[j].y-q[i].y>ans) break;
			ans=min(ans,Dist(q[i],q[j]));
		}
	}
	return ans;
} 
bool cmp(Point a,Point b){
	return a.x<b.x;
}
int main(){
	int T,n;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		for(int i=0;i<2*n;i++){
			scanf("%lf%lf",&p[i].x,&p[i].y);
			if(i<n) p[i].flag=0;
			else p[i].flag=1;
		}
		sort(p,p+2*n);
		printf("%.3f\n",ClosestPointPair(0,2*n-1));
	}
	return 0;
}

 

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值