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
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(计算几何求二维平面的最近点对和最远点对)的更多相关文章
- Codeforces Gym 100286A. Aerodynamics 计算几何 求二维凸包面积
Problem A. AerodynamicsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...
- golang 二维平面求多重遮挡三角形总面积
解决问题描述:二维平面有很多三角形错落,可能会相互叠加落在一起,也可能互相远离.目标求出这些三角形的总占地面积. 我最开始想的解决方案是用总面积-总重叠面积 = 总占地面积.后来实现起来发现当面临多次 ...
- HDU 5130 Signal Interference(计算几何 + 模板)
HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...
- 关于线段树or 树状树状 在二维平面搞事情!Orz
第一式:https://ac.nowcoder.com/acm/contest/143/I 题意: 有 n 个点,一个点集 S 是好的,当且仅当对于他的每个子集 T,存在一个右边无限长的矩形,使得这个 ...
- hdu 1255 覆盖的面积(求覆盖至少两次以上的面积)
了校赛,还有什么途径可以申请加入ACM校队? 覆盖的面积 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 求二维数组最大子数组的和。郭林林&胡潇丹
求二维数组子数组的最大值,开始思路不太清晰.先从最简单的开始. 以2*2的简单数组为例找规律, 假设最大数为a[0][0],则summax=a[0][0],比较a[0][0]+a[0][1].a[0] ...
- BOI2007 Mokia | cdq分治求二维点数模板
题目链接:戳我 也没什么,其实主要就是为了存一个求二维坐标上矩形内点的个数的模板.为了之后咕咕咕地复习使用 不过需要注意的一点是,树状数组传x的时候可千万不要传0了!要不然会一直死循环的...qwqw ...
- Problem N: 求二维数组中的鞍点【数组】
Problem N: 求二维数组中的鞍点[数组] Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2764 Solved: 1728[Submit][S ...
- php实现求二进制中1的个数(右移、&、int32位)(n = n & (n - 1);)
php实现求二进制中1的个数(右移.&.int32位)(n = n & (n - 1);) 一.总结 1.PHP中的位运算符和java和c++一样 2.位移运算符看箭头方向,箭头向左就 ...
随机推荐
- flask报错No module named 'flask.ext'
解决: from flask.ext.httpauth import HTTPBasicAuth 改为 from flask_httpauth import HTTPBasicAuth 提示Modul ...
- python3.7 安装pyopengl,环境搭建
安装环境:win10 64位操作系统,python3.7 一.安装py库 需要用pip 安装 pip install PyOpenGL PyOpenGL_accelerate 可能会报错, 是因为没有 ...
- HTML提交方式post和get区别(实验)
HTML提交方式post和get区别(实验) 一.post和get区别 get提交,提交的信息都显示在地址栏中. post提交,提交的信息不显示地址栏中,显示在消息体中. 二.客户端代码 <!D ...
- mount ntfs-3g , fstab里的配置没有效果
把ntfs-3g配置在 fstab 里,mount 时会报 No such device 网上也有在嵌入式系统里发生的类似例子. 没有解决方法,也不准备再研究了. 准备在机器启动之后,手动下面的命令 ...
- Java数组的定义和使用
如果希望保存一组有相同类型的数据,可以使用数组. 数组的定义和内存分配 Java 中定义数组的语法有两种: type arrayName[]; type[] arrayName; type 为Java ...
- Style、ControlTemplate 和 DataTemplate 触发器
本文摘要: 1:属性触发器: 2:数据触发器: 3:事件触发器: Style.ControlTemplate 和 DataTemplate 都有触发器集合. 属性触发器只检查W ...
- 记一次无法正常本地登陆Linux服务器(确定密码正确)
首先,ssh可以正常登陆使用.但是,本地可以确定密码是正确的情况还是不能登陆. 然后查看/var/log/secure文件如下提示: 然后,尝试去看了下/etc/pam.d/login 下面(有问题的 ...
- 『PyTorch』第九弹_前馈网络简化写法
『PyTorch』第四弹_通过LeNet初识pytorch神经网络_上 『PyTorch』第四弹_通过LeNet初识pytorch神经网络_下 在前面的例子中,基本上都是将每一层的输出直接作为下一层的 ...
- spring boot 基础篇 -- 自带图片服务器
我们平时在日常项目中经常会遇到图片的上传和访问的情景,平时我们可能习惯于把图片传到resource或者项项目中的某个位置,这样会有一个缺点,当我们重新项目打包时,这些图片会丢失.为了解决这一缺点,我们 ...
- zzuli1728(数学期望,组合数)
1728: 社交网络 Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 232 Solved: 64 SubmitStatusWeb Board Desc ...