ZOJ - 2107 Quoit Design [分治]

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17751

题目描述:求最近点对

题目分析:分治,(nlogn);

为什么,第二轮按排序:http://noalgo.info/793.html

代码:

//problem:   zoj 2107 Quoit Design
//author:    ACsorry
//result:    Yes

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#define INF 1<<29
#define maxInt 0x7fffffff
#define SUP 0x80000000
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

typedef long long LL;
const int N=100007;

struct Point{
    double x,y;
}p[N];
int tmp[N];

double dis(int a,int b)
{
    return sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y));
}

int cmpxy(Point a,Point b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}

int cmpy(int a,int b)
{
    return p[a].y<p[b].y;
}

double closestPair(int L,int R)
{
    double ret=1e20;
    if(L==R) return ret;
    if(L+1==R)
        return dis(L,R);
    int mid=(L+R)>>1;

    ret=min(closestPair(L,mid),closestPair(mid+1,R));
    int cnt=0;
    for(int i=L;i<=R;i++){
        if(fabs(p[i].x-p[mid].x)<=ret)
            tmp[cnt++]=i;
    }
    sort(tmp,tmp+cnt,cmpy);//取ret X 2*ret范围内的点最多7个

    for(int i=0;i<cnt;i++)
    {
        for(int j=i+1;j<cnt&&p[tmp[j]].y-p[tmp[i]].y<ret;j++)
        {
            ret=min(ret,dis(tmp[i],tmp[j]));
        }
    }
    return ret;
}

int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        sort(p,p+n,cmpxy);
        printf("%.2lf\n",closestPair(0,n-1)/2);//求半径;dis/2;
    }
    return 0;
}
时间: 2024-08-11 06:23:13

ZOJ - 2107 Quoit Design [分治]的相关文章

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

HDU 1007 [Quoit Design] 分治

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 题目大意:N个点,求最近一对点的距离的一半. 关键思想:最傻瓜式的枚举所有点对的距离找最短 O(n^2),会TLE. 分治可以优化成O(nlogn).二分区间,考虑三种情形:点对的两点都在左区间.两点都在右区间.两点一左一右 前两种情况,可以递归地解出来,分别为d1,d2.第三种,可以依据min(d1,d2)收缩成一条带状区域(勾股定理显然). 然后对带中所有点进行处理,此时又可依据min(d

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 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 ri

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

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

分治 [HDU 1007] Quoit Design

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

poj 1007 Quoit Design(分治)

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

zoj 2107&amp;&amp;hdu 1007最近点对问题

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1107 Quoit Design Time Limit: 5 Seconds      Memory Limit: 32768 KB 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