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 ...
随机推荐
- Django Form and Modelform Admin定义 高级查询)
Django的form表单一般具有两种功能 1. 验证输入 2.输入HTML ---------模板----------- from django import forms class BookFor ...
- Shiro启用注解方式
shiro验证权限方式一种是基于url配置文件: 例如: <bean id="shiroFilter" class="org.apache.shiro.spring ...
- spring @value 注入static
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Com ...
- Windows 10 LTSB
没有商店,没有EDGE,没有助手. 有平板模式,但没有通用应用,内置便笺也没有.可以“固定到开始屏幕”,但没有动态磁贴.有虚拟键盘,但没有Windows Ink. 只有安全补丁更新,据说是10年. 安 ...
- Use Visual studio 2010 build Python2.7.10
http://p-nand-q.com/python/building-python-27-with-vs2010.html
- ajax批删
- 关于ios使用jquery的on,委托事件失效
$('.parents').on("click",'.child',function(){}); 类似上面这种,在ios上点击"child"元素不会起作用,解决 ...
- EventBus--介绍
注意: 1,post()方法里面的类型和onEvent()中的类型要一致., 2,订阅者对象中 必须有 onEvent 的 public 方法 ---public void onEvent(O ...
- 数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree
题目如下:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, fin ...
- List接口
1.List接口实现的方式有两种ArrayList 和 LinkedList ArrayList实现了可变的数组,允许保存所有元素包括null,缺点是删除的比较慢 LinkedList删除对象比较快, ...