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 ...
随机推荐
- xcode8让真机测试支持ios8.0以下版本
xcode8支持ios8以下真机测试方法: 1.应用程序-xcode 显示包内容-Contents-Developer-Platforms-iPhoneOS.platform-DeviceSuppor ...
- SQL:over partition by与group by 的区别
group by是对检索结果的保留行进行单纯分组,一般总爱和聚合函数一块用例如AVG(),COUNT(),max(),main()等一块用. partition by虽然也具有分组功能,但同时也具有其 ...
- MyBatis入门学习教程-Mybatis3.x与Spring4.x整合
一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:create -DgroupId=me.gacl -DartifactId=spring4-myba ...
- easyUI datagrid中 checkbox 各属性和事件
DataGrid其中与选择,勾选相关 DataGrid属性:singleSelect boolean 如果为true,则只允许选择一行. false ctrlSelect boolean 在启用多行 ...
- Emergency(山东省第一届ACM省赛)
Emergency Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Kudo’s real name is not Kudo. H ...
- CentOS开机无法进入系统,如何查错
开机时按e/F5按钮,进入选择系统界面 会出现 CentOS(2.6.32-...类似的选择列表,选择默认的系统然后按e: 这时会出现 root kernel ... initd... 三个选项,选择 ...
- php源码之遍历目录下的所有的文件
<?php //遍历目录下的所有的文件 -- 递归调用 // http://www.manongjc.com/article/1495.html function get_all_file1($ ...
- variably modified 'dist' at file scope|
转自:http://blog.csdn.net/wusuopubupt/article/details/18408227 错误原因: The reason for this warning is th ...
- Autoit 在word中绘图
没有时间整理,直接看参考网址: http://www.autoitx.com/thread-257-1-1.html
- asp.net忘记密码功能
//调用接口 post public string GetResponseByPost(string mobile, string messcode, string values, string ut ...