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. 对Tomcat启动或运行时,项目对jar包依赖路径的一些粗浅认知

    Tomcat在运行webapp项目的时候,需要各种依赖jar包.它会从2个地方去找这些包 1.Tomcat自己的lib目录中 2.webapp目录下,webapps\{项目}\WEB-INF\lib ...

  2. hdu 5586 Sum 基础dp

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Desc ...

  3. vmware 安装ubuntu

    点击自定义硬件 即将完毕 下面就是安装啦

  4. android listview的HeadView左右切换图片(仿新浪,网易,百度等切换图片)

    首先我们还是看一些示例:(网易,新浪,百度) 显示效果都不错,可是手感就不一样了,百度最棒,网易还行,新浪就操作很不好,这里我说的是滑动切换图片.自己可以测试一下.不得不说牛叉的公司确实有哦牛叉的道理 ...

  5. SVN同步版本库与网站目录2

    定义: SVN版本库  = /home/svn/repos  网站目录     = /var/www/web 1.检出一个项目到网站目录 #svn checkout file:///home/svn/ ...

  6. English trip -- VC(情景课)3 A Family

    xu言: 今天,老天很给面子.我在路上的时候基本上没有~然而我也没有带雨具.难道这就是传中的天道酬勤~或者说只要你努力,老天爷会给让路 Talk about the picture 看图说话 Look ...

  7. 2018焦作网络赛Give Candies

    一开始忽略了欧拉定理指数部分是modphi(n-1)没有memset,减法后面没加0:

  8. 从mysql数据库删除重复记录只保留其中一条

    这两天做了一个调用第三方接口的小程序,因为是实时更新数据,所以请求接口的频率就很高,这样有时会出现往数据库插入重复的数据,对数据库造成压力也不方便管理,因为要通过原生sql语句,解决数据库的去重问题. ...

  9. hdu-6319-单调队列

    Problem A. Ascending Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K ...

  10. Leetcode 86

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...