poj2187凸包最远点对
暴力过了
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; struct point{
double x,y;
};
point p[N],s[N];
int n;
double dir(point p1,point p2,point p3)
{
return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.x);
}
double dis(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double powdis(point p1,point p2)
{
return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
}
bool comp(point p1,point p2)
{
double te=dir(p[],p1,p2);
if(te<)return ;
if(te==&&dis(p[],p1)<dis(p[],p2))return ;
return ;
}
double Graham()
{
int pos;
double minx,miny;
minx=miny=inf;
for(int i=;i<n;i++)
{
if(p[i].x<minx||(p[i].x==minx&&p[i].y<miny))
{
minx=p[i].x;
miny=p[i].y;
pos=i;
}
}
swap(p[],p[pos]);
sort(p+,p+n,comp);
int top=;
p[n]=p[];
s[]=p[],s[]=p[],s[]=p[];
for(int i=;i<=n;i++)
{
while(top>=&&dir(s[top-],s[top],p[i])>)top--;
s[++top]=p[i];
}
/* for(int i=0;i<top;i++)
cout<<s[i].x<<" "<<s[i].y<<endl;*/
double ans=;
for(int i=;i<top;i++)
{
for(int j=i+;j<top;j++)
{
ans=max(ans,powdis(s[i],s[j]));
}
}
return ans;
}
int main()
{
/* ios::sync_with_stdio(false);
cin.tie(0);*/
while(~scanf("%d",&n)){
for(int i=;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
printf("%.0f\n",Graham());
}
return ;
}
暴力枚举
但是旋转卡壳死活过不了,
10
0 0
10000 0
1 100
2 199
9999 100
9998 199
100 -900
200 -1799
9800 -1799
9900 -900
这组数据过不了,
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; struct point{
double x,y;
};
point p[N],s[N];
int n;
double dir(point p1,point p2,point p3)
{
return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.x);
}
double dis(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double powdis(point p1,point p2)
{
return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
}
bool comp(point p1,point p2)
{
double te=dir(p[],p1,p2);
if(te<)return ;
if(te==&&dis(p[],p1)<dis(p[],p2))return ;
return ;
}
double Graham()
{
int pos;
double minx,miny;
minx=miny=inf;
for(int i=;i<n;i++)
{
if(p[i].x<minx||(p[i].x==minx&&p[i].y<miny))
{
minx=p[i].x;
miny=p[i].y;
pos=i;
}
}
swap(p[],p[pos]);
sort(p+,p+n,comp);
int top=;
p[n]=p[];
s[]=p[],s[]=p[],s[]=p[];
for(int i=;i<=n;i++)
{
while(top>=&&dir(s[top-],s[top],p[i])>)top--;
s[++top]=p[i];
}
/* for(int i=0;i<top;i++)
cout<<s[i].x<<" "<<s[i].y<<endl;*/
//Rotating Calipers
double ans=;
int resx=,resy=;
for(int i=;i<top;i++)
{
if(s[i].x==s[].x)resx++;
if(s[i].y==s[].y)resy++;
}
if(resx==top||resy==top)return powdis(s[],s[top-]);
if(top==)ans=powdis(s[],s[]);
else
{
int j=;
for(int i=;i<top;i++)
{
while(dir(s[i],s[i+],s[j+])>=dir(s[i],s[i+],s[j]))
j=(j+)%top;
// cout<<endl;
// cout<<i<<" "<<j<<endl;
// cout<<powdis(s[i],s[j])<<" "<<powdis(s[i],s[j+1])<<endl;
ans=max(ans,max(powdis(s[j],s[i]),powdis(s[j+],s[i])));
}
}
return ans;
}
int main()
{
/* ios::sync_with_stdio(false);
cin.tie(0);*/
while(~scanf("%d",&n)){
for(int i=;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
printf("%.0f\n",Graham());
}
return ;
}
/*
4
0 0
0 1
1 1
1 0
*/
rc
不知道哪里出错了,希望有路过的大神帮忙改改错
poj2187凸包最远点对的更多相关文章
- 【poj2187】最远点对(勉强凑数)
题目简述 输入n个点,及其坐标,n<=50000,所有坐标都是不超过10000的整数组成,没有重点. 问最远点对间的距离的平方是多少 题解 这是一道旋转卡壳的裸题 我们要求这个多边形的直径,这可 ...
- [Gym-101512C] 凸包+最远点对
找最大的四边形或者三角形面积,先求凸包,然后枚举两个点,再通过旋转,找最大的另两个点 #include<bits/stdc++.h> #define fi first #define se ...
- 【POJ各种模板汇总】(写在逆风省选前)(不断更新中)
1.POJ1258 水水的prim……不过poj上硬是没过,wikioi上的原题却过了 #include<cstring> #include<algorithm> #inclu ...
- OpenCV学习(30) 轮廓defects
上一篇教程中,我们学习了如何计算轮廓的凸包,其实对一个轮廓而言,可能它的凸包和它本身是重合的,也有可能不是重合的.比如下面左边图像的轮廓本身就是凸包,而右边图像的轮廓则不是.我们可以通过函数bool ...
- POJ 2187 Beauty Contest( 凸包求最远点对 )
链接:传送门 题意:给出 n 个点,求出这 n 个点中最远的两个点距离的平方 思路:最远点对一定会在凸包的顶点上,然后直接暴力找一下凸包顶点中距离最远的两个点 /******************* ...
- POJ(2187)用凸包求最远点对
Beauty Contest http://poj.org/problem?id=2187 题目描述:输入n对整数点,求最距离远的点对,输出他们距离的平方和 算法:拿到这个题,最朴素的想法就是用2层循 ...
- [USACO2003][poj2187]Beauty Contest(凸包+旋转卡壳)
http://poj.org/problem?id=2187 题意:老题了,求平面内最远点对(让本渣默默想到了悲剧的AHOI2012……) 分析: nlogn的凸包+旋转卡壳 附:http://www ...
- poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)
链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...
- POJ2187(凸包+旋转卡壳)
这道题目的大意是给出一组二维空间的顶点,计算其中距离最远的两个顶点之间的距离. 先说明凸包的概念和求法. 定义:对于多边形P,若将P中任意的两个点(包含边上)用一条线段连接,线段都落于该多边形中(含边 ...
随机推荐
- SDUT3165:Round Robina(循环链表)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=3165 题意分析: 比赛时这题没有A真伤心 ...
- PAT 1125 Chain the Ropes[一般]
1125 Chain the Ropes (25 分) Given some segments of rope, you are supposed to chain them into one rop ...
- string与CString对比
string是标准C++库中的字符串类,CString是在Windows开发环境下常用的字符串类,CString目前已从MFC中分离出来可以单独使用,只需包含atlstr.h即可. 相比string, ...
- java通过url抓取网页数据
在很多行业中,要对行业数据进行分类汇总,及时分析行业数据,对于公司未来的发展,有很好的参照和横向对比.所以,在实际工作,我们可能要遇到数据采集这个概念,数据采集的最终目的就是要获得数据,提取有用的数据 ...
- Xcode插件开发案例教程
引言 在平时开发过程中我们使用了很多的Xcode插件,虽然官方对于插件制作没有提供任何支持,但是加载三方的插件,默认还是被允许的.第三方的插件,存放在 ~/Library/Application Su ...
- django【F和Q】
一.F 案例每人增加500工资 ORM:UserInfo.objects.filter().update(salary=500) 这不行吧 SQL: update userinfo set salar ...
- POJ - 1511 - 两次SPFA
这道题也算是一道模板题,但是第一次用优先队列迪杰斯特拉就T了.1e6的数据量,给了8s,网上其他题解中说要用SPFA. 题意:N个点的带权有向图.每次都从1出发,要到达其余没有被访问过的一个点(发传单 ...
- Jquery Easy UI Datagrid 上下移动批量保存数据
DataGrid with 上下移动批量保存数据 通过前端变量保存修改数据集合,一次性提交后台执行 本想结合easyui 自带的$('#dg').datagrid('getChanges'); 方法来 ...
- visual studio开发工具的C#主流控件属性一览表
visual studio开发工具的C#主流控件属性一览表 详细的介绍了各控制属性的详细中文介绍 C#控件及常用设计整理 1.窗体 1.常用属性 (1)Name属性:用来获取或设置窗体的名称,在应用程 ...
- Spring JdbcTemplate的queryForList(String sql , Class<T> elementType)返回非映射实体类的解决方法
Spring JdbcTemplate的queryForList(String sql , Class<T> elementType)易错使用 一直用ORM,今天用JdbcTemplate ...