POJ(2187)用凸包求最远点对
Beauty Contest
http://poj.org/problem?id=2187
题目描述:输入n对整数点,求最距离远的点对,输出他们距离的平方和
算法:拿到这个题,最朴素的想法就是用2层循环遍历所有的点对,但这样可能会超时。由于距离最远的点对必定在点集的凸包的顶点上,所以只用遍历凸包上的点对就行。这样就把可能存在的大量的点给排除。哈哈~~~还是凸包。
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <cmath>
using namespace std;
struct Node
{
int x,y;
Node operator-(Node &node)
{
Node new_node;
new_node.x=x-node.x;
new_node.y=y-node.y;
return new_node;
}
};
vector<Node> p,s; //p存放所有顶点,s存放凸包顶点,s模拟栈
const double eps=10e-;
int squared_distance(Node &a,Node &b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
} double cross(Node vec1,Node vec2)
{
return (double)(vec1.x*vec2.y-vec1.y*vec2.x);
}
bool cmp(Node &a,Node &b)
{
Node vec1=a-p[];
Node vec2=b-p[];
double temp=cross(vec1,vec2);
if(temp>eps)
return true;
else if(temp<eps)
return false;
else
{
if(squared_distance(a,p[])<squared_distance(b,p[]))
return true;
else
return false;
}
}
void swap(Node &a,Node &b)
{
Node temp;
temp=a;
a=b;
b=temp;
}
int lowleft(vector<Node> &p)
{
int len=p.size();
int min_x=p[].x,min_y=p[].y;
int k=;
for(int i=;i<len;i++)
{
if(p[i].y<min_y)
{
min_x=p[i].x;
min_y=p[i].y;
k=i;
}
else if(p[i].y==min_y&&p[i].x<min_x)
{
min_x=p[i].x;
k=i;
}
}
return k;
}
void graham(vector<Node> &p)
{
int k=lowleft(p);
swap(p[],p[k]);
sort(p.begin()+,p.end(),cmp);
p.push_back(p[]); s.push_back(p[]);
s.push_back(p[]); int top=;
int len=p.size();
for(int i=;i<len;i++)
{
while(top>=&&cross(s[top]-s[top-],p[i]-s[top])<)
{
s.pop_back();
top--;
}
s.push_back(p[i]);
top++;
}
}
int main()
{
int n;
while(cin>>n)
{
Node temp;
p.clear();
s.clear();
for(int i=;i<n;i++)
{
cin>>temp.x>>temp.y;
p.push_back(temp);
}
graham(p);
int max=;
int len=s.size()-;//p的最后一个元素的值是p[0]遍历
for(int i=;i<len-;i++)
{
for(int j=i+;j<len;j++)
{
int d=squared_distance(s[i],s[j]);
if(d>max)
max=d;
}
}
cout<<max<<endl;
}
return ;
}
POJ(2187)用凸包求最远点对的更多相关文章
- Beauty Contest(凸包求最远点)
http://poj.org/problem?id=2187 题意:求凸包上最远点距离的平方 思路:开始用旋转卡壳求的最远点,WA,改了好久..后来又改成枚举凸包上的点..AC了.. #include ...
- POJ - 2187 Beauty Contest(最远点对)
http://poj.org/problem?id=2187 题意 给n个坐标,求最远点对的距离平方值. 分析 模板题,旋转卡壳求求两点间距离平方的最大值. #include<iostream& ...
- poj 2187 Beauty Contest(平面最远点)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 24431 Accepted: 7459 D ...
- POJ 2187 Beauty Contest( 凸包求最远点对 )
链接:传送门 题意:给出 n 个点,求出这 n 个点中最远的两个点距离的平方 思路:最远点对一定会在凸包的顶点上,然后直接暴力找一下凸包顶点中距离最远的两个点 /******************* ...
- POJ 3348 Cows 凸包 求面积
LINK 题意:给出点集,求凸包的面积 思路:主要是求面积的考察,固定一个点顺序枚举两个点叉积求三角形面积和除2即可 /** @Date : 2017-07-19 16:07:11 * @FileNa ...
- POJ 1113 Wall 凸包求周长
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26286 Accepted: 8760 Description ...
- poj 3348 Cows 凸包 求多边形面积 计算几何 难度:0 Source:CCC207
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7038 Accepted: 3242 Description ...
- poj 2187 Beauty Contest 凸包模板+求最远点对
题目链接 题意:给你n个点的坐标,n<=50000,求最远点对 #include <iostream> #include <cstdio> #include <cs ...
- POJ 2187 Beauty Contest【旋转卡壳求凸包直径】
链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
随机推荐
- android模拟器没法通过localhost访问本地服务器的解决
当android项目访问在一台服务器上的WEB服务时,没法通过localhost或者127.0.0.1来访问.模拟器把它自己作为了localhost,代码中使用localhost或者127.0.0.1 ...
- Spring 事务注解 错误问题
碰到个问题,在一个springmvc项目中,设置好事务,然后在service上添加@Transactional注解,只要一添加这个注解,该service就无法被spring创建成功, error cr ...
- C# 字符串 数据类型 判断 与特定规则验证
验证字符串格式 1)判断字符串是否是常见数据类型,decimal,foalt,double,datetime,int等等 2)验证字符串符合特定规则 (1)邮箱地址,IP地址 (2)纯数 ...
- left join 取一条数据
产品生产过程中要记录产品在制作过程中的不良历史,即使在一个工序,也可以产生多条不良历史记录,所有的不良信息保存在B表中.假设产品在打包工序,存放打包工序成品参数记录的表A,数据是唯一的.但在打包过程中 ...
- 初探jquery之强大丰富的选择器
---恢复内容开始--- 1.基本选择器 常用的有id选择器:#id, 类选择器:.class , 元素选择器. 2.层次选择器 $(ancestor descendant):选取ancestor ...
- maven添加仓库地址
mvn install时,好多包从mvn中央库下载不下来,搜索到一个maven库地址:http://conjars.org/repo/,将其添加到maven仓库中 编辑 conf/setting.xm ...
- 利用Oracle自带的impdp和expdp进行简单备份
目前有个这样的需求,每天晚上23点将生产库的数据备份到备份机器上,第二天备份机器的数据库可以直接使用,数据是昨天生产库的数据.(生产数据目前不多,全部数据不超过3000w条). 由于没有接触过高深的O ...
- NPOI Excel 单元格背景颜色对照表
NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillP ...
- Java的常用对象①②
Java的常用对象① Java的常用对象有很多,这里只对lang包中常用的Object,Date(Calendar,DateFormat,Math),System,Runtime进行介绍.㈠Objec ...
- 一致性 hash 算法( consistent hashing )a
一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and rando ...