HDU 1589 Stars Couple(计算几何求二维平面的最近点对和最远点对)

Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 930    Accepted Submission(s): 200

Problem Description

Can you believe it? After Gardon had solved the problem, Angel accepted him! They were sitting on the lawn, watching the stars. 
"I still can‘t believe this!" Gardon said.
Angel smiled and said: "The reason why I love you does not rest on of who you are, but on who I am when I am with you."
Gardon answered :"In my view, it‘s not because I‘m lonely and it‘s not because it‘s the Valentine‘s Day. It‘s because when you realize you want to spend the rest of your life with somebody, you want the rest of your life to start as soon as possible!"
"Watch the stars! How beautiful!"
"Just like your eyes!" Gardon replied.
Angel smiled again:" Did you hear about this: one star means one person. When two people fall in love, their stars will be always nearby."
"So we are the nearest couple?"
Now there is the question. Can you point out which couple of stars is nearest? Besides, can you fingle out which couple are most distant?

Input

Input contains serveral test cases. Each cases starts with a integer N (2<=N<=50,000). Then N lines follow. Each line have two integers Xi and Yi(-10^9<Xi,Yi<10^9), which show the position of one star.
The input will be ended with a integer 0.

Output

For each case, print the distance of the nearest couple and the most distant couple. 
Print a blank line after each case.

Sample Input

3

1 1

0 0

0 1

0

Sample Output

Case 1:

Distance of the nearest couple is 1.000

Distance of the most distant couple is 1.414

//by zyy

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int M=50005;
typedef struct Point
{
double x;
double y;
}Point;
Point p[M];
Point pp[M];
bool bo[M];
int stack[M];//form 1 to t;
double dis(Point A,Point B)
{
return sqrt((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y));
}
bool cmp(Point a,Point b)
{
if(a.x<b.x)
    return true;
if(a.x>b.x)
    return false;
if(a.y<b.y)
    return true;
return false;
}
double Xdet(Point A,Point B,Point C)
{
double x1,x2,y1,y2;
x1=B.x-A.x;
y1=B.y-A.y;
x2=C.x-A.x;
y2=C.y-A.y;
return x1*y2-x2*y1;//大于0在左手边,逆时针
}
//把点集凸包化Gram_Scan算法(使用水平序)
void Gram_Scan(Point *p,int &n)//p从1-n,把点集土包化
{
int i,t;
sort(p+1,p+1+n,cmp);
for(t=0,i=1;i<=n;i++)
{
    if(i>1&&p[i].x==p[i-1].x&&p[i].y==p[i-1].y)
      continue;
    p[++t]=p[i];
}
n=t;
t=0;
memset(bo+1,true,n*sizeof(bo[0]));
if(n>0)
{
    stack[++t]=1;
    bo[stack[t]]=false;
}
if(n>1)
{
    stack[++t]=2;
    bo[stack[t]]=false;
}
if(n>2)
{
    for(i=3;i<n;i++)
      if(bo[i]&&Xdet(p[stack[t-1]],p[stack[t]],p[i])>=0)
      {
        stack[++t]=i;
        bo[i]=false;
      }
      else
      {
        while(t>=2&&Xdet(p[stack[t-1]],p[stack[t]],p[i])<0)
        {
          bo[stack[t]]=true;
          t--;
        }
        stack[++t]=i;
        bo[stack[t]]=false;
      }
   for(i=n;i>=1;i--)
     if(bo[i]&&Xdet(p[stack[t-1]],p[stack[t]],p[i])>=0)
     {
       stack[++t]=i;
       bo[i]=false;
     }
     else
     {
       while(t>=2&&Xdet(p[stack[t-1]],p[stack[t]],p[i])<0)
       {
         bo[stack[t]]=true;
         t--;
       }
       stack[++t]=i;
       bo[stack[t]]=false;
     }
     t--;
}
for(i=1;i<=t;i++)
    pp[i]=p[stack[i]];
memcpy(p+1,pp+1,t*sizeof(Point));
n=t;
}
int n,o[M],on;
int dcmp(double a,double b)
{
    if(a-b<1e-10&&b-a<1e-10)
        return 0;
    if(a>b)
        return 1;
    return -1;
}
bool cmp1(const Point &a,Point &b)
{
    return dcmp(a.x,b.x)<0;
}
bool cmp2(const int&a,const int&b)
{
    return dcmp(p[a].y,p[b].y)<0;
}
double min(double a,double b)
{
    return a<b?a:b;
}
double search(int s,int t)
{
    int mid=(s+t)/2,i,j;
    double ret=1e300;
    if(s>=t)
        return ret;
    for(i=mid;i>=s&&!dcmp(p[i].x,p[mid].x);i--);ret=search(s,i);
    for(i=mid;i<=t&&!dcmp(p[i].x,p[mid].x);i++);ret=min(ret,search(i,t));on=0;
    for(i=mid;i>=s&&dcmp(p[mid].x-p[i].x,ret)<=0;i--)o[++on]=i;
    for(i=mid+1;i<=t&&dcmp(p[i].x-p[mid].x,ret)<=0;i++)o[++on]=i;
    sort(o+1,o+on+1,cmp2);
    for(i=1;i<=on;i++)
        for(j=1;j<=10;j++)
            if(i+j<=on)
                ret=min(ret,dis(p[o[i]],p[o[i+j]]));
    return ret;
}
int main()
{
    int n,i,count=0,j;
    double shortdis,longdis;
    while(scanf("%d",&n),n)
    {
        for(i=1;i<=n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        sort(p+1,p+n+1,cmp1);
        shortdis=search(1,n);
        longdis=0;
        Gram_Scan(p,n);
        for(i=1;i<=n-1;i++)
            for(j=i+1;j<=n;j++)
                if(dis(p[i],p[j])>longdis)
                    longdis=dis(p[i],p[j]);
        printf("Case %d:\n",++count);
        printf("Distance of the nearest couple is %.3lf\n",shortdis);
        printf("Distance of the most distant couple is %.3lf\n\n",longdis);
    }
    return 0;
}
时间: 2024-10-26 07:34:19

HDU 1589 Stars Couple(计算几何求二维平面的最近点对和最远点对)的相关文章

二维平面最近点-分治

题目描述给出二维平面上的n个点,求其中最近的两个点的距离的一半.输入包含多组数据,每组数据第一行为n,表示点的个数:接下来n行,每行一个点的坐标.当n为0时表示输入结束,每组数据输出一行,为最近的两个点的距离的一半.输入样例:20 01 121 11 13-1.5 00 00 1.50输出样例:0.710.000.75题目解析:采用分治的思想,把n个点按照x坐标进行排序,以坐标mid为界限分成左右两个部分, 对左右两个部分分别求最近点对的距离,然后进行合并.对于两个部分求得的最近距离d, 合并过

hdu 5839 Special Tetrahedron 计算几何 求特殊四面体个数

Special Tetrahedron Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 64 Problem Description Given n points which are in three-dimensional space(without repetition).

基于分治的二维平面最近点对算法实现

摘要: 网上有很多关于分治方法求最近点对的讨论,但是没有完整的可运行代码,本文主要对于该问题介绍一完整的可运行代码, 供有兴趣者参考. 正文: 作为对比,我们也同时实现了最近点对的枚举解法,具体的主函数如下: #include<stdio.h> #include<stdlib.h> #include "node.h" void initList(node* p) { p[0].x= 2.0; p[0].y= 1.0; p[1].x= 1.0; p[1].y= 2

二维平面曼哈顿距离最小生成树模版

#include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include <limits.h> #include <vector> #include <string> #include <time.h> #i

golang 二维平面求多重遮挡三角形总面积

解决问题描述:二维平面有很多三角形错落,可能会相互叠加落在一起,也可能互相远离.目标求出这些三角形的总占地面积. 我最开始想的解决方案是用总面积-总重叠面积 = 总占地面积.后来实现起来发现当面临多次重叠时,这样算是错误的. 后来参考了一些文献,得出结论:这个问题抽象出来就是求n个集合的并集问题.公式如下: A1∪A2∪......∪An = A1 + A2 + ......+ An ﹣(A1∩A2 + A1∩A3 + ......+ A[n-1]∩An) + (A1∩A2∩A3 + A1∩A2

Board Wrapping(计算几何求凸包加向量的旋转)

UVA - 10652 Board Wrapping Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description Problem B Board Wrapping Input: standard input Output: standard output Time Limit: 2 seconds The small sa

POJ 3241 Object Clustering 二维平面曼哈顿距离最小生成树

题目链接:点击打开链接 题意: 给定二维平面上的n个点坐标,常数k 下面n行给出坐标 求一个最小生成树,问第k大的边是多少. 任意两个点间建一条边的花费是其曼哈顿距离. 思路:转自:点击打开链接 一.曼哈顿距离最小生成树 曼哈顿距离最小生成树问题可以简述如下: 给定二维平面上的N个点,在两点之间连边的代价为其曼哈顿距离,求使所有点连通的最小代价. 朴素的算法可以用O(N2)的Prim,或者处理出所有边做Kruskal,但在这里总边数有O(N2)条,所以Kruskal的复杂度变成了O(N2logN

数字之魅:寻找二维平面上的最近的点对

在二维平面上的n个点中,如何快速的找出最近的一对点,就是最近点对问题. 初看这个题,可能感觉有点儿复杂. 方案一:蛮力法.数组中总共包含N个数,所以我们可以把平面内所有的点按X轴排序,然后依次算出后一个坐标与前面所有左边的距离,然后用Min和position来记录最近的距离和两个坐标.该方案和在一维空间求两个最近点的距离有点儿类似,其时间复杂度为:O(N*N). 方案二:在一维空间里,我们知道如果数组有序,我们可以很快找出最近的两个点.我们可以用O(N*logN)的时间复杂度来对数据进行排序[快

给定二维平面整数点集输出“最大点集”算法(今日头条面试题)

引子 最近自己的独立游戏上线了,算是了却了一桩心愿.接下来还是想找一份工作继续干,创业的事有缘再说. 找工作之前,总是要浏览一些实战题目,热热身嘛.通过搜索引擎,搜到了今日头条的一道面试题. 题目 P为给定的二维平面整数点集.定义 P 中某点x,如果x满足 P 中任意点都不在 x 的右上方区域内(横纵坐标都大于x),则称其为"最大的".求出所有"最大的"点的集合.(所有点的横坐标和纵坐标都不重复, 坐标轴范围在[0, 1e9) 内) 如下图:实心点为满足条件的点的集