hdu1007(平面最近点对->分治)

主要想弄出一个最近点对的模板出来。。。

主要运用分治思想。。思路大概是这样的:

先把点按x排序,然后按x进行分治。。。分当然简单。。难点就在合并了。。

怎么合并?先找出中点,然后以已经求得的局部最短距离d为基准,将与中点的x距离不超过d的加入集合,然后按y对该点集进行排序,然后枚举点i,再枚举点j,维护最短距离d,如果y距离超过d便不再枚举。。

语文不好。。。如果流程不清楚可以看代码或其他blog。。。


然后关键是这个时间复杂度据说是nlogn。。本人并不是很承认这个复杂度。。我感觉应该是nlognlogn。。下面进行证明。。

参考:http://blog.csdn.net/lu1012123053/article/details/9825175

主要关注这个文章的鸽巢原理,它证明了在d*2d范围内最多存在6点,那么同理在2d*d中最多也只存在6点。我们可以得知,在代码 53行起的2层循环中,j最多循环6次,那么,这个看似是O(n^2)的复杂度实际上已经降到了O(n)

那么主要复杂度就来源于排序了。。。引文就直接说该算法的复杂度是nlogn实在是无法认同,因为还得将那6个点给挑出来。。怎么挑?只能排序。。

所以每次合并的时间为nlogn,那么合起来就是O(nlognlogn)了。。

然而。。上面是考虑了最坏的情况,即每次合并时,都将所有点都包含进来了,实际中只有部分点会包含进来,所以实际复杂度可能并没有达到O(nlognlogn)。。

而且在特定情况下我们还可以尽可能将合并时的点集减少,这个可以参考我以前的文章:

http://blog.csdn.net/qkoqhh/article/details/79177389







#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#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 inf 10000
#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 100005
#define nm 100498
#define pi 3.1415926535897931
using namespace std;
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{
	double x,y;
	P(double x=0,double y=0):x(x),y(y){}
	P operator-(const P&o){return P(x-o.x,y-o.y);}
	double operator*(const P&o){return x*o.y-y*o.x;}
	bool operator<(const P&o){return x<o.x||(x==o.x&&y<o.y);}
}p[NM];
double dis(P o){return sqrt(sqr(o.x)+sqr(o.y));}
bool cmp(int x,int y){return p[x].y<p[y].y;}
int n,m,tmp[NM];
double ans;
double part(int x,int y){
	double d=inf;
	if(x==y)return d;
	if(x+1==y)return dis(p[x]-p[y]);
	d=min(part(x,mid),part(mid+1,y));
	int tot=0;
	inc(i,x,y)if(fabs(p[mid].x-p[i].x)+eps<d)tmp[++tot]=i;
	sort(tmp+1,tmp+1+tot,cmp);
	inc(i,1,tot)inc(j,i+1,tot)
		if(p[tmp[j]].y-p[tmp[i]].y+eps<d)d=min(d,dis(p[tmp[i]]-p[tmp[j]]));
		else break;
	return d;
}

int main(){
	//freopen("data.in","r",stdin);
	while(n=read()){
		inc(i,1,n)scanf("%lf%lf",&p[i].x,&p[i].y);
		sort(p+1,p+1+n);
		printf("%.2lf\n",part(1,n)/2);
	}
}







Quoit Design

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 58976    Accepted Submission(s): 15632


Problem Description
Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.

Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.
 

Input
The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0.
 

Output
For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.
 

Sample Input
  
  
2 0 0 1 1 2 1 1 1 1 3 -1.5 0 0 0 0 1.5 0
 

Sample Output
  
  
0.71 0.00 0.75
 

Author
CHEN, Yue
 

Source
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1006  1009  1005  1008  1004 
 

Statistic |  Submit |  Discuss | Note

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值