杭电 ACM 1007 Quoit Design

来源:互联网 发布:中国农行软件 编辑:程序博客网 时间:2024/06/11 09:56

Quoit Design

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


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
20 01 121 11 13-1.5 00 00 1.50
 

Sample Output
0.710.000.75
 

Author
CHEN, Yue
 

Source
ZJCPC2004
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1006 1002 1001 1011 1024 

最近点对分治的问题



代码参考了一位博主的贴出的代码,在修改了他的代码bug后最终如下所示,(不改bug前也AC了(⊙﹏⊙)),谢谢那位博主提供的代码风格,也谢谢他在解答此题时推荐的书,但由于他博客地址找不到了,只能在此口头感谢了



AC(1)这个更耗时间和内存,原本想着new能灵活控制,反而忽略了频繁的new,其本身需要携带比预设一个数组要多的一些管理的信息,更加耗时与耗空间,(而new的时候一开始也忽略了delete,占的内存就更夸张了)
#include <cstdio>#include <algorithm>#include <cmath>using namespace std;#define N 100007using namespace std;struct Point{    double x, y;}*pt;int *a;int n;bool cmp_x(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 = 0;    if (r - l + 1 <= 3)    {        if (r - l + 1 == 1) return ans;        ans = getDis(pt[l], pt[l + 1]);        if (r - l + 1 == 2) return ans;        for (int i = l; i < r; ++i)        {            for (int j = i + 1; j <= r; ++j)            {                ans = min(ans, getDis(pt[i], pt[j]));            }        }        return ans;    }    int m = (l + r) >> 1;    double s1 = solve(l, m);    double s2 = solve(m + 1, r);    ans = min(s1, s2);    int k = 0;    for (int i = m; i >= l && pt[m].x - pt[i].x <= ans; --i)         a[k++] = i;    for (int i = m + 1; i <= r && pt[i].x - pt[m].x <= ans; ++i)         a[k++] = i;    sort(a, a + k, cmp_y);    for (int i = 0; i < k; ++i)    {        for (int j = i + 1; j < k && j <= i + 7; ++j)//此处只搜索点i后的七个点非常关键,不加这个限制会超时,而这个限制的根据由来贴在下面        {            ans = min(ans, getDis(pt[a[i]], pt[a[j]]));        }    }    return ans;}int main(){    while (~scanf("%d", &n))    {        if (!n) break;        pt = new Point[n];        a = new int[n];        for (int i = 0; i < n; ++i) scanf("%lf%lf", &pt[i].x, &pt[i].y);        sort(pt, pt + n, cmp_x);        printf("%.2lf\n", solve(0, n - 1) / 2.0);        delete []pt;        pt = NULL;        delete []a;        a = NULL;    }    return 0;}
//AC(2)
#include <cstdio>#include <algorithm>#include <cmath>using namespace std;#define N 100007using 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 = 0;    if (r - l + 1 <= 3)    {        if (r - l + 1 == 1) return ans;        ans = getDis(pt[l], pt[l + 1]);        if (r - l + 1 == 2) return ans;        for (int i = l; i < r; ++i)        {            for (int j = i + 1; j <= r; ++j)            {                ans = min(ans, getDis(pt[i],pt[j]));            }        }        return ans;    }    int m = (l + r) >> 1;    double s1 = solve(l, m);    double s2 = solve(m + 1,  r);    ans = min(s1,s2);    int k = 0;    for (int i = m; i >= l && pt[m].x - pt[i].x <= ans; --i) a[k++] = i;    for (int i = m + 1; i <= r && pt[i].x - pt[m].x <= ans; ++i) a[k++] = i;    sort(a, a + k, cmp_y);//按照y的大小顺序从小到大排序    for (int i = 0; i < k; ++i)    {        for (int j = i + 1; j < k && j <= i + 7; ++j)        {            ans = min(ans, getDis(pt[a[i]], pt[a[j]]));//使用a[i]可以确保所对比的点就是两区域的中间点,a[x]其实就是第几个点,pt[a[x]]
//是取这个点的详细信息,        }    }    return ans;}int main(){    while (~scanf("%d",&n))    {        if (!n) break;        for (int i = 0; i < n; ++i) scanf("%lf%lf",&pt[i].x, &pt[i].y);        sort(pt, pt + n, cmp);        printf("%.2lf\n",solve(0, n - 1)/2.0);    }    return 0;}


0 0
原创粉丝点击