Quoit Design(hdu1007)最近点对问题。模版哦!

Quoit Design

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

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

题意:找任意两点之间距离最短的输出!!!

方法:模版题,最近点对问题。                            我又多了份模版。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define Max(x,y) (x)>(y)?(x):(y)
struct Q
{
    double x, y;
} q[100001], sl[10], sr[10];

int cntl, cntr, lm, rm;

double ans;
int cmp(const void*p1, const void*p2)
{
    struct Q*a1=(struct Q*)p1;
    struct Q*a2=(struct Q*)p2;
    if (a1->x<a2->x)return -1;
    else if (a1->x==a2->x)return 0;
    else return 1;
}
double CalDis(double x1, double y1, double x2, double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void MinDis(int l, int r)
{
    if (l==r) return;
    double dis;
    if (l+1==r)
    {
        dis=CalDis(q[l].x,q[l].y,q[r].x,q[r].y);
        if (ans>dis) ans=dis;
        return;
    }
    int mid=(l+r)>>1, i, j;
    MinDis(l,mid);
    MinDis(mid+1,r);
    lm=mid+1-5;
    if (lm<l) lm=l;
    rm=mid+5;
    if (rm>r) rm=r;
    cntl=cntr=0;
    for (i=mid; i>=lm; i--)
    {
        if (q[mid+1].x-q[i].x>=ans)break;
        sl[++cntl]=q[i];
    }
    for (i=mid+1; i<=rm; i++)
    {
        if (q[i].x-q[mid].x>=ans)break;

        sr[++cntr]=q[i];
    }
    for (i=1; i<=cntl; i++)
        for (j=1; j<=cntr; j++)
        {
            dis=CalDis(sl[i].x,sl[i].y,sr[j].x,sr[j].y);
            if (dis<ans) ans=dis;
        }
}
int main()
{
    int n, i;
    while (scanf("%d",&n)==1&&n)
    {
        for (i=1; i<=n; i++)
            scanf("%lf%lf", &q[i].x,&q[i].y);
        qsort(q+1,n,sizeof(struct Q),cmp);
        ans=CalDis(q[1].x,q[1].y,q[2].x,q[2].y);
        MinDis(1,n);
        printf("%.2lf\n",ans/2.0);
    }
    return 0;
}

Quoit Design(hdu1007)最近点对问题。模版哦!

时间: 2024-08-08 19:29:29

Quoit Design(hdu1007)最近点对问题。模版哦!的相关文章

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

【HDOJ】P1007 Quoit Design (最近点对)

题目意思很简单,意思就是求一个图上最近点对. 具体思想就是二分法,这里就不做介绍,相信大家都会明白的,在这里我说明一下如何进行拼合. 具体证明一下为什么只需要检查6个点 首先,假设当前左侧和右侧的最小值为d,那么对于一个点,如果有个最小值小于d,那么一定存在于上d下d左d右d的一块区域内,又因为是从左到右,从上到下,所以左侧的那部分匹配的区域会重叠,也就是对于左侧的区域,完全没有必要去进行匹配.所以只需要对右侧d,上下d的区域进行匹配,而假设这个区域内的所以点的距离为d那么最多为6个,那么如果长

HDOJ-1007 Quoit Design(最近点对问题)

http://acm.hdu.edu.cn/showproblem.php?pid=1007 给出n个玩具(抽象为点)的坐标 求套圈的半径 要求最多只能套到一个玩具 实际就是要求最近的两个坐标的距离 典型的最近点对问题 最近点对详解 http://blog.csdn.net/lonelycatcher/article/details/7973046 //最近点对 # include <stdio.h> # include <algorithm> # include <math

hdu 1007 Quoit Design, 平面最近点对

平面最近点对的经典做法就是分治.所有点先按x再按y排序,然后取中间位置的点,利用其x坐标值将点划分成左右两部分,分别求出两侧的最近点对(设其距离为δ),然后合并.参看下面的图片(来自guyulongcs的专栏,http://blog.csdn.net/guyulongcs/article/details/6841550)合并的时候需要注意,大致有两种做法,一种是把分界线两侧δ范围内的点分别加进一个数组,然后对右侧数组按y坐标值排序,之后依次为每个左侧数组中的点,在右侧数组中寻找与其y坐标值最近的

HDU 1007 Quoit Design (最近点对 分治法)

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 ca

HDU1007 Quoit Design 【分治】

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30505    Accepted Submission(s): 8017 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

hdu 1007 Quoit Design 分治求最近点对

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29344    Accepted Submission(s): 7688 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

Quoit Design(hdu1007)

---恢复内容开始--- Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 47126    Accepted Submission(s): 12323 Problem Description Have you ever played quoit in a playground? Quoit is a game

HDU 1007 Quoit Design【计算几何/分治/最近点对】

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 58566    Accepted Submission(s): 15511 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat