欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poj3714(平面最近点对)

程序员文章站 2022-03-30 08:39:35
...

感觉这题有点强行出题的感觉,数据都是设计好给这个算法过的,十分不科学。。

然而还是要练的。。

此题就是直接分治,然后多了一步判断是否在同一集合内才维护最值。。然而如果这2个集合是水平分离的。。那肯定要退化成n^2...

这里提出一点想法吧。。将集合的重心都求出来,然后得到2重心对应直线斜率,这样就可以旋转坐标系,把这2个重心都移动到y轴上。。这样应该可以降低一点复杂度。。。然而实际上能不能行窝也不造。。。

然后这题要和凸包最近距离区别开来,这个是边上可以。。要用旋转卡壳。。

详见:http://blog.csdn.net/qkoqhh/article/details/79321090



/**
 *        ┏┓    ┏┓ 
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃   
 *        ┃   ━    ┃ 
 *        ┃ >   < ┃ 
 *        ┃       ┃ 
 *        ┃... ⌒ ...  ┃ 
 *        ┃       ┃ 
 *        ┗━┓   ┏━┛ 
 *          ┃   ┃ Code is far away from bug with the animal protecting           
 *          ┃   ┃   神兽保佑,代码无bug 
 *          ┃   ┃            
 *          ┃   ┃         
 *          ┃   ┃ 
 *          ┃   ┃            
 *          ┃   ┗━━━┓ 
 *          ┃       ┣┓ 
 *          ┃       ┏┛ 
 *          ┗┓┓┏━┳┓┏┛ 
 *           ┃┫┫ ┃┫┫ 
 *           ┗┻┛ ┗┻┛ 
 */  
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 400005
#define nm 1000498
#define pi 3.1415926535897931
using namespace std;
const ll inf=9*1e18;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}




struct P{
	ll x,y;int t;
	P(ll x=0,ll y=0):x(x),y(y){}
	P operator-(const P&o){return P(x-o.x,y-o.y);}
	bool operator<(const P&o){return x<o.x||(x==o.x&&y<o.y);}
	ll operator*(const P&o){return x*o.y-y*o.x;}
}p[NM];
ll dis(P o){return sqr(o.x)+sqr(o.y);}
bool cmp(P x,P y){return x.y<y.y;}
int n,tmp[NM];
ll ans;

void part(int x,int y){
	if(x==y)return;
	if(x+1==y){if(p[x].t^p[y].t)ans=min(ans,dis(p[x]-p[y]));return;}
	part(x,mid);part(mid+1,y);
	int m=0;
	inc(i,x,y)if(sqr(p[i].x-p[mid].x)<=ans)tmp[++m]=i;
	sort(tmp+1,tmp+1+m,cmp);
	inc(i,1,m)inc(j,i+1,m)
		if(sqr(p[tmp[j]].y-p[tmp[i]].y)<=ans){if(p[tmp[i]].t^p[tmp[j]].t)ans=min(ans,dis(p[tmp[i]]-p[tmp[j]]));}
		else break;
}

int main(){
	//freopen("data.in","r",stdin);
	int _=read();
	while(_--){
		n=read();ans=inf;
		inc(i,1,n)p[i].x=read(),p[i].y=read(),p[i].t=0;
		inc(i,n+1,n<<1)p[i].x=read(),p[i].y=read(),p[i].t=1;
		n<<=1;
		sort(p+1,p+1+n);
		part(1,n);
		printf("%.3lf\n",sqrt((double)ans));
	}
}



Raid
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 14330   Accepted: 4100

Description

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]