Quoit Design(最近点对+分治)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007

Quoit Design

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

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
 

最近点对问题:

分治的思想,如果是大于3个点的时候,将所有的点按照x坐标(或者y坐标)排序,然后从中间线一份两半,分别求出左边的点的最近点距ldis,和右边点的最近点距rdis,那么问题就是要将两个部分的点合起来,令dis = min(rdis,ldis);那么中间如果想出现最近点对的时候必须要出现在中间线两侧距离为d的区域内,那么考虑区域内的左侧的点,与其产生最近点的点一定在坐标的右侧,那么为了不用枚举区域内右边所有的点,就要考虑对于每个点它如果要产生距离小于dis的对应点,肯定是在以它为圆心的dis为半径的园内,那么这个圆覆盖这个带状区域面积最大的情况就是圆心在分界线上的时候。如图

Quoit Design(最近点对+分治)

Quoit Design(最近点对+分治)

注意:在处理中间带的时候将带状区域内的点按照y坐标排序(如果之前是按照y排序的,则现在按照x坐标排序)那么因为右侧的任意两点之间的距离要大于等于dis所以要想极限情况,就是在这个圆区域内找等边三角形,因为从上到下处理的点,所以当前点的上方的点不在考虑,由于排序的时候是左右两边的点一起排序的所以要在当前点的编号向后处理7个点,如上图中划出的三个点,如果发现他们来自同一侧则不再处理

下面是模板代码:

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std; #define N 100005
const int L = -;
const int R = ;
struct Node{
int id;
double x;
double y;
};
Node num[N],cp[N]; //double Dis(Node a, Node b){
// return sqrt(pow((a.x - b.x),2.0)+pow((a.y-b.y),2.0));
//}
double Dis(Node a, Node b){
double x = a.x-b.x, y = a.y-b.y;
return sqrt(x*x+y*y);
} bool cmpx(Node a, Node b)
{
if(a.x==b.x) return a.y<b.y;
else return a.x < b.x;
}
bool cmpy(Node a, Node b)
{
if(a.y==b.y) return a.x<b.x;
else return a.y<b.y;
}
double solve(int low, int high)
{
double dis;
int sum = high - low;
if(sum == ){ return ; }//只有一个数
else if(sum == ){//两个数
dis = Dis(num[low],num[high]);
}
else if(sum == ){//三个数
double tm1,tm2,tm3;
tm1 = Dis(num[low],num[low+]);
tm2 = Dis(num[low+],num[high]);
tm3 = Dis(num[low],num[high]);
dis = min(tm1,min(tm2,tm3));
}
else //大于三个数
{
double lmin,rmin,mmin;
int mid = (low+high)/;
int p = ;
int i, j;
lmin = solve(low,mid);
rmin = solve(mid+,high);
dis = min(lmin,rmin);
/**-----------------提出来会变快-----------------*/
double ldis = num[mid].x-dis;
double rdis = num[mid].x+dis;
for( i = low; i <= mid; i++) /**-----小于等于,不能是小于,因为下面标记 L R 了*/
{
if(num[i].x >= ldis)
{
cp[p].id = L;//标记为属于左边的部分
cp[p].x = num[i].x;
cp[p].y = num[i].y;
p++;
}
}
for( ; i <= high; i++)
{
if(num[i].x <= rdis)
{
cp[p].id = R;//标记为右边的点
cp[p].x = num[i].x;
cp[p].y = num[i].y;
p++;
}
}
sort(cp,cp+p,cmpy);
for( i = ; i < p; i++)
{
for( j = ; (j <= )&&(i+j)<p; j++)
{
if(cp[i].id != cp[i+j].id)//最小值可能出现在分界线不同的两边
{
mmin = Dis(cp[i],cp[i+j]);
if(mmin<dis)
dis = mmin;
}
}
}
}
return dis;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n!=)
{
double result = ;
for(int i = ; i < n; i++)
{
num[i].id = ;
scanf("%lf%lf",&num[i].x,&num[i].y);
}
sort(num,num+n,cmpx);
result = solve(,n-);
printf("%.2f\n",result/);
}
return ;
}

下面是一开始wa的代码:

对应的错误在上面代码中用用/** -------*标识

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 100005
const int L = -;
const int R = ;
struct Node{
int id;
double x;
double y;
};
Node num[N],cp[N]; double Dis(Node a, Node b){
return sqrt(pow((a.x - b.x),2.0)+pow((a.y-b.y),2.0));
} bool cmpx(Node a, Node b)
{
if(a.x==b.x) return a.y<b.y;
else return a.x < b.x;
}
bool cmpy(Node a, Node b)
{
if(a.y==b.y) return a.x<b.x;
else return a.y<b.y;
} double solve(int low, int high)
{
double dis;
int sum = high - low;
if(sum == ){ return ; }//只有一个数
else if(sum == ){//两个数
dis = Dis(num[low],num[high]);
}
else if(sum == ){//三个数
double tm1,tm2,tm3;
tm1 = Dis(num[low],num[low+]);
tm2 = Dis(num[low+],num[high]);
tm3 = Dis(num[low],num[high]);
dis = min(tm1,min(tm2,tm3));
}
else //大于三个数
{
double lmin,rmin,mmin;
int mid = (low+high)/;
int p = ;
int i, j;
lmin = solve(low,mid);
rmin = solve(mid+,high);
dis = min(lmin,rmin);
for( i = low; i < mid; i++)
{
double ldis = num[mid].x - dis;
if(num[i].x >= ldis)
{
cp[p].id = L;//标记为属于左边的部分
cp[p].x = num[i].x;
cp[p].y = num[i].y;
p++;
}
}
for( ; i < high; i++)
{
double rdis = num[mid].x+dis;
if(num[i].x <= rdis)
{
cp[p].id = R;//标记为右边的点
cp[p].x = num[i].x;
cp[p].y = num[i].y;
p++;
}
}
sort(cp,cp+p,cmpy);
for( i = ; i < p; i++)
{
for( j = ; (j <= )&&(i+j)<p; j++)
{
if(cp[i].id != cp[i+j].id)//最小值可能出现在分界线不同的两边
{
mmin = Dis(cp[i],cp[i+j]);
if(mmin<dis)
dis = mmin;
}
}
}
}
return dis;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n!=)
{
double result = ;
for(int i = ; i < n; i++)
{
num[i].id = ;
scanf("%lf%lf",&num[i].x,&num[i].y);
}
sort(num,num+n,cmpx);
result = solve(,n-);
printf("%.2f\n",result/);
}
return ;
}

下面给出大神的模板代码:(虽然内容差不多,感觉一下代码风格)

 #include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100007 using namespace std; struct Point
{
double x,y;
}pt[N];
int a[N]; int n; bool cmp(Point a, Point b)
{
if (a.x != b.x) return a.x < b.x;
else return a.y < b.y;
}
bool cmp_y(int id1, int id2){
return pt[id1].y < pt[id2].y;
}
double getDis(const Point &a, const Point &b)
{
double x = a.x - b.x;
double y = a.y - b.y;
return sqrt(x*x + y*y);
}
double solve(int l, int r)
{
double ans = ;
if (r - l + <= )
{
if (r - l + == ) return ans;
ans = getDis(pt[l], pt[l + ]);
if (r - l + == ) return ans;
for (int i = l; i < r; ++i)
{
for (int j = i + ; j <= r; ++j)
{
ans = min(ans, getDis(pt[i],pt[j]));
}
}
return ans;
}
int m = (l + r) >> ;
double s1 = solve(l, m);
double s2 = solve(m + , r);
ans = min(s1,s2);
int k = ;
for (int i = m - ; i >= l && pt[m].x - pt[i].x <= ans; --i) a[k++] = i;
for (int i = m + ; i <= r && pt[i].x - pt[m].x <= ans; ++i) a[k++] = i;
sort(a, a + k, cmp_y);
for (int i = ; i < k; ++i)
{
for (int j = i + ; j < k && j <= i + ; ++j)
{
ans = min(ans, getDis(pt[a[i]], pt[a[j]]));
}
}
return ans;
}
int main()
{
while (~scanf("%d",&n))
{
if (!n) break;
for (int i = ; i < n; ++i) scanf("%lf%lf",&pt[i].x, &pt[i].y);
sort(pt, pt + n, cmp);
printf("%.2lf\n",solve(, n - )/2.0);
}
return ;
}
上一篇:OpenTSDB - 分布式可扩展的监控系统


下一篇:解决javascript动态改变img的src属性图片不显示问题