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 ...
随机推荐
- VR定制开发、AR定制开发(长年承接虚拟现实、增强现实应用、VR游戏定制开发,北京公司,可签合同)
Cardboard SDK for Unity的使用 上一篇文章作为系列的开篇,主要是讲了一些虚拟现实的技术和原理,本篇就会带领大家去看一看谷歌的Cardboard SDK for Unity,虽然目 ...
- chmod、chown、chgrp的意思
文件权限管理chown->change owner 改变文件所有者chown test install.log -将install.log这个文件的所有者改为test用户 chgrp->c ...
- CSS中的绝对定位与相对定位
层级关系为:<div ----------- position:relative; 不是最近的祖先定位元素,不是参照物<div----------没有设置为定位元素,不是参照物<di ...
- mfc 控件字体设置
将以下代码加入至 OnInitDialog() 中 // TODO: Add extra initialization here CFont * m_font= new CFont; m_font-& ...
- Mac 配置Charles抓https的包
安装Charles 这个简单,略过... 打开Charles,在Menu选择Help > Install Charles CA SSL Certificate Keychain Access(钥 ...
- 使用Dapper读取Oracle多个结果集
Dapper对SQL Server支持很好,但对于Oracle有些用法不一样,需要自己进行特殊处理. 1.首先要自定义一个Oracle参数类 public class OracleDynamicPar ...
- spring JPA 动态查询
没什么好说的,记住就行. 下面是在Service中的方法 Page<TStaff> staffs=dao.findAll(new Specification<TStaff>() ...
- 关于Oracle GoldenGate中Extract的checkpoint的理解 转载
什么是checkpoint? 在Oracle 数据库中checkpoint的意思是将内存中的脏数据强制写入到磁盘的事件,其作用是保持内存中的数据与磁盘上的数据一致.SCN是用来描述该事件发生的准确的时 ...
- springmvc 接受特殊类型字段的处理方法
springmvc接受前台传入的数据时如果该字段类型无法被封装(如Date),则会出现400 Bad Request错误,解决方法如下. 1.在需要处理的字段前加上注解: @DateTimeForma ...
- 一起学习KenDo
这几年用Telerik做WEB,积累了一些感觉.因为打算涉足移动APP开发,打算接下来学习KenDo.