HDU1007:Quoit Design——题解

http://acm.hdu.edu.cn/showproblem.php?pid=1007

题目大意:给n个点,求点对最短距离/2.

——————————————————————

平面分治裸题。

暂时还不想讲为什么这么做。

所以原理暂割。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef double dl;
const dl INF=1e20;
const int N=100001;
struct node{
    dl x;
    dl y;
}p[N],a[N],b[N],c[N];
bool cmp(node A,node B){
    return A.x<B.x;
}
inline dl dis(int i,int j){
    return sqrt(pow(b[i].x-c[j].x,2)+pow(b[i].y-c[j].y,2));
}
dl solve(int l,int r){
    if(l>=r)return INF;
    int mid=(l+r)>>1;
    dl x0=(p[mid].x+p[mid+1].x)/2.0;
    dl d=min(solve(l,mid),solve(mid+1,r));
    int l1=l,r1=mid+1,bnum=0,cnum=0;
    for(int i=l;i<=r;i++){
    if(l1<=mid&&(r1>r||p[l1].y<p[r1].y)){
        a[i]=p[l1++];
        if(x0-d<a[i].x)b[++bnum]=a[i];
    }else{
        a[i]=p[r1++];
        if(a[i].x<x0+d)c[++cnum]=a[i];
    }
    }
    for(int i=l;i<=r;i++)p[i]=a[i];

    for(int i=1,j=1;i<=bnum||j<=cnum;){
    if(i<=bnum&&(j>cnum||b[i].y<c[j].y)){
        for(int k=j-1;k>=1;k--){
        if(b[i].y-d>=c[k].y)break;
        d=min(d,dis(i,k));
        }
        i++;
    }else{
        for(int k=i-1;k>=1;k--){
        if(c[j].y-d>=b[k].y)break;
        d=min(d,dis(k,j));
        }
        j++;
    }
    }
    return d;
}
int main(){
    int n;
    while(scanf("%d",&n)!=EOF&&n){
    for(int i=1;i<=n;i++){
        scanf("%lf%lf",&p[i].x,&p[i].y);
    }
    sort(p+1,p+n+1,cmp);
    printf("%.2f\n",solve(1,n)/2.0);
    }
    return 0;
}
时间: 2024-10-30 00:34:44

HDU1007:Quoit Design——题解的相关文章

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

[HDU1007]Quoit Design

题目描述 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

Hdoj 1007 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 ring is careful

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

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): 130 Accepted Submission(s): 72   Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings a

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

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 46340    Accepted Submission(s): 12084这题可以分治: 首先按照x方向排序,然后分别对 两边求最短距离. 然后按照y排序,统计距离分割点-d 和+d之间的点,算了,忘了. 但是可以直接排序,然后枚举相邻三个点之间到距离.这三个点

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>