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

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=;
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+,p++n,cmp);
for(t=,i=;i<=n;i++)
{
if(i>&&p[i].x==p[i-].x&&p[i].y==p[i-].y)
continue;
p[++t]=p[i];
}
n=t;
t=;
memset(bo+,true,n*sizeof(bo[]));
if(n>)
{
stack[++t]=;
bo[stack[t]]=false;
}
if(n>)
{
stack[++t]=;
bo[stack[t]]=false;
}
if(n>)
{
for(i=;i<n;i++)
if(bo[i]&&Xdet(p[stack[t-]],p[stack[t]],p[i])>=)
{
stack[++t]=i;
bo[i]=false;
}
else
{
while(t>=&&Xdet(p[stack[t-]],p[stack[t]],p[i])<)
{
bo[stack[t]]=true;
t--;
}
stack[++t]=i;
bo[stack[t]]=false;
}
for(i=n;i>=;i--)
if(bo[i]&&Xdet(p[stack[t-]],p[stack[t]],p[i])>=)
{
stack[++t]=i;
bo[i]=false;
}
else
{
while(t>=&&Xdet(p[stack[t-]],p[stack[t]],p[i])<)
{
bo[stack[t]]=true;
t--;
}
stack[++t]=i;
bo[stack[t]]=false;
}
t--;
}
for(i=;i<=t;i++)
pp[i]=p[stack[i]];
memcpy(p+,pp+,t*sizeof(Point));
n=t;
}
int n,o[M],on;
int dcmp(double a,double b)
{
if(a-b<1e-&&b-a<1e-)
return ;
if(a>b)
return ;
return -;
}
bool cmp1(const Point &a,Point &b)
{
return dcmp(a.x,b.x)<;
}
bool cmp2(const int&a,const int&b)
{
return dcmp(p[a].y,p[b].y)<;
}
double min(double a,double b)
{
return a<b?a:b;
}
double search(int s,int t)
{
int mid=(s+t)/,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=;
for(i=mid;i>=s&&dcmp(p[mid].x-p[i].x,ret)<=;i--)o[++on]=i;
for(i=mid+;i<=t&&dcmp(p[i].x-p[mid].x,ret)<=;i++)o[++on]=i;
sort(o+,o+on+,cmp2);
for(i=;i<=on;i++)
for(j=;j<=;j++)
if(i+j<=on)
ret=min(ret,dis(p[o[i]],p[o[i+j]]));
return ret;
}
int main()
{
int n,i,count=,j;
double shortdis,longdis;
while(scanf("%d",&n),n)
{
for(i=;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
sort(p+,p+n+,cmp1);
shortdis=search(,n);
longdis=;
Gram_Scan(p,n);
for(i=;i<=n-;i++)
for(j=i+;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 ;
}

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

  1. Codeforces Gym 100286A. Aerodynamics 计算几何 求二维凸包面积

    Problem A. AerodynamicsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...

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

    解决问题描述:二维平面有很多三角形错落,可能会相互叠加落在一起,也可能互相远离.目标求出这些三角形的总占地面积. 我最开始想的解决方案是用总面积-总重叠面积 = 总占地面积.后来实现起来发现当面临多次 ...

  3. HDU 5130 Signal Interference(计算几何 + 模板)

    HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...

  4. 关于线段树or 树状树状 在二维平面搞事情!Orz

    第一式:https://ac.nowcoder.com/acm/contest/143/I 题意: 有 n 个点,一个点集 S 是好的,当且仅当对于他的每个子集 T,存在一个右边无限长的矩形,使得这个 ...

  5. hdu 1255 覆盖的面积(求覆盖至少两次以上的面积)

    了校赛,还有什么途径可以申请加入ACM校队?  覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  6. 求二维数组最大子数组的和。郭林林&胡潇丹

    求二维数组子数组的最大值,开始思路不太清晰.先从最简单的开始. 以2*2的简单数组为例找规律, 假设最大数为a[0][0],则summax=a[0][0],比较a[0][0]+a[0][1].a[0] ...

  7. BOI2007 Mokia | cdq分治求二维点数模板

    题目链接:戳我 也没什么,其实主要就是为了存一个求二维坐标上矩形内点的个数的模板.为了之后咕咕咕地复习使用 不过需要注意的一点是,树状数组传x的时候可千万不要传0了!要不然会一直死循环的...qwqw ...

  8. Problem N: 求二维数组中的鞍点【数组】

    Problem N: 求二维数组中的鞍点[数组] Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2764  Solved: 1728[Submit][S ...

  9. php实现求二进制中1的个数(右移、&、int32位)(n = n & (n - 1);)

    php实现求二进制中1的个数(右移.&.int32位)(n = n & (n - 1);) 一.总结 1.PHP中的位运算符和java和c++一样 2.位移运算符看箭头方向,箭头向左就 ...

随机推荐

  1. 实现简单的ORM

    介绍 本篇将介绍实现简单的ORM,即:对数据表的通用操作:增.删.改.查 数据访问层 数据访问层类图 类说明: 1.DbProvider(供应):为数据操作提供基本对象,如:连接.操作对象.事务... ...

  2. Django内置模版过滤器

    Django内置过滤器总览 可以查询下表来总览Django的内置过滤器: 过滤器 说明 add 加法 addslashes 添加斜杠 capfirst 首字母大写 center 文本居中 cut 切除 ...

  3. nginx反向代理和正向代理,优缺点

    http://blog.csdn.net/lishaojun0115/article/details/53200629 nginx反向代理代理的是服务器,正向代理代理的是客户端, 反向代理是客户点发送 ...

  4. [转]mysql日常工作手记

    1. 给navy加show权限: 1 2 update mysql.user set Show_db_priv='Y'  where user='navy'; flush privileges; 2. ...

  5. Android之MVC模式的使用

    MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码 ...

  6. Python的第二次作业

    羊车门问题 1.我认为 会 增加选中汽车的机会,原因如下: 不换的情况:对于参赛者而言无论选哪一扇门都有1/3的几率能获得车子. 换的情况  :对于参赛者而言,有两种情况「1.参赛者第一次就选择到了正 ...

  7. hdu1238 kmp

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  8. TitanX服务器登陆网关

  9. python-day21--序列化模块模块

    什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化   序列化的目的: 1.以某种存储形式使自定义对象持久化: 2.将对象从一个地方传递到另一个地方. 3.使程序更具维护性. ...

  10. TCP-IP详解:Nagle算法

    在使用一些协议通讯的时候,比如Telnet,会有一个字节字节的发送的情景,每次发送一个字节的有用数据,就会产生41个字节长的分组,20个字节的IP Header 和 20个字节的TCP Header, ...