赞
踩
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.
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.
For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.
问题描述
你有没有在操场上玩过quoit?Quoit是一款游戏,其中一些玩具上装有扁平环,所有玩具都被包围。在Cyberground领域,每个玩具的位置都是固定的,戒指经过精心设计,一次只能包围一个玩具。另一方面,为了使游戏看起来更具吸引力,环被设计成具有最大半径。给定字段的配置,您应该找到这样一个环的半径。
假设所有玩具都是飞机上的点。如果点与环的中心之间的距离严格小于环的半径,则环被环包围。如果两个玩具放置在同一点,则环的半径被认为是0。
输入
输入包含几个测试用例。对于每种情况,第一行包含整数N(2 <= N <= 100,000),即该字段中玩具的总数。然后是N行,每行包含一对(x,y),它们是玩具的坐标。输入由N = 0终止。
产量
对于每个测试用例,在一行中打印Cyberground管理员所需的环的半径,精确到2位小数。
这道题其实就是最近对问题,https://blog.csdn.net/qq_40452317/article/details/88403397在这篇博客,已经解决用分治法最近对问题。其代码可以直接拿来使用。
- #include<iostream>
- #include<math.h> //计算距离
- #include<algorithm> //排序
- #include<iomanip>
-
- using namespace std;
- #define MAX 0x3f3f3f3f //定义无穷大
- #define M 999999
-
- struct point {
- double x, y;
- }p[M];
-
- int a[M];// 保存排序的索引
-
- int cmpx(const point& a, const point& b) //排序升序
- {
- return a.x < b.x;
- }
-
- int cmpy(int &a, int &b) //排序升序
- {
- return p[a].y < p[b].y;
- }
-
- inline double min(double a, double b) //返回两个值中较小的
- {
- return a < b ? a : b;
- }
- inline double dist(const point& a, const point& b)
- {
- return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
- }
- double closeset(int low, int high)
- {
- if (low == high)
- return MAX;
- if (low + 1 == high) //即n=2,返回两点之间的距离
- return dist(p[low], p[high]);
- int mid = (low + high)>>1; //右移一位,相当于除以2,但右移的运算速度更快,若使用(low+high)/2求中间位置容易溢出
- double ans = min(closeset(low, mid), closeset(mid+1, high)); //递归求出两边最小距离
- int i, j, c = 0;
- for (i = low; i <= high; i++) //统计那些点位于两虚线内,并记录
- {
- if (p[mid].x - ans <= p[i].x && p[i].x <= p[mid].x + ans)
- a[c++] = i;
- }
- sort(a, a + c, cmpy);
- for(i = 0; i < c; i++)//比较s1中虚线内的点和s2中虚线内的点的距离是否存在有小于两侧最小对的
- {
- int k = i+7 > c ? c : i+7;
- for (j = i+1; j < k; j++)
- {
- if (p[a[j]].y - p[a[i]].y > ans) //如果位于中位线两侧的点的距离大于anx则跳出第一个循环
- break;
- ans = min(dist(p[a[i]], p[a[j]]), ans); //如果有两个点小于两侧的最近对,则选出最小的点
- }
- }
- return ans;
- }
-
- int main()
- {
- int n;
- double dmin;
- while(cin>>n&&n)
- {
- for(int i=0;i<n;i++) //循环读入文件
- cin>>p[i].x>>p[i].y;
- sort(p,p+n,cmpx); //按照x轴排序
- dmin=closeset(0, n-1);
- cout<<setiosflags(ios::fixed)<<setprecision(2)<<dmin/2<<endl;
- }
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。