Beauty Contest(凸包求最远点)
2024-08-31 01:43:38
http://poj.org/problem?id=2187
题意:求凸包上最远点距离的平方
思路:开始用旋转卡壳求的最远点,WA,改了好久。。后来又改成枚举凸包上的点。。AC了。。
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N=;
int n;
struct Point
{
int x,y;
Point(int x = ,int y = ):x(x),y(y) {}
bool friend operator < (const Point &a,const Point &b)
{
return a.x < b.x||(a.x==b.x&&a.y < b.y);
} } p[N],ch[N];
typedef Point Vector;
Vector operator-(Point a,Point b)
{
return Vector(a.x-b.x,a.y-b.y);
}
int Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
int Distance(Point a,Point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int Graham()//判断凸包
{
sort(p,p+n);
int m = ;
for (int i = ; i < n; i++)
{
while(m > &&Cross(ch[m-]-ch[m-],p[i]-ch[m-]) <= )
m--;
ch[m++] = p[i];
}
int k = m;
for (int i = n-; i >= ; i--)
{
while(m > k && Cross(ch[m-]-ch[m-],p[i]-ch[m-]) <= )
m--;
ch[m++] = p[i];
}
if (n > )
m--;
return m;
}
int main()
{
scanf("%d",&n);
for (int i = ; i < n; i++)
scanf("%d %d",&p[i].x,&p[i].y);
int cnt = Graham();
int ans = ;
for (int i = ; i < cnt ; i++)
{
for (int j = ; j < i; j++)
{
int dis = Distance(ch[i],ch[j]);
ans = max(ans,dis);
}
}
printf("%d\n",ans);
return ;
}
旋转卡壳的那段代码,不知道哪错了,先保留着吧。。后期再改。。
int dia_rotating_calipers(int cnt)//旋转卡壳
{
int dia = ;
int q = ;
for (int i = ; i < cnt; i++)
{
while(Cross(ch[i+]-ch[i],ch[q+]-ch[i]) > Cross(ch[i+]-ch[i],ch[q]-ch[i]))
q = (q+)%n;
dia = max(dia,max(Distance(ch[i],ch[q]),Distance(ch[i+],ch[q+])));
}
return dia;
}
最新文章
- Swift学习笔记-ARC
- js指定分隔符连接数组元素join()
- C++ 画星号图形——空心梯形(核心代码记录)
- 【Hibernate】Hibernate系列1之概述
- Java多线程中的进程,线程,并行,并发
- c#实现数据的左补右补功能
- 野指针及c++指针使用注意点
- HDU5653 Bomber Man wants to bomb an Array 简单DP
- Microsoft Windows Sharepoint Services V3.0 安装图示
- IOC容器在框架中的应用
- 用django搭建一个简易blog系统(翻译)(一)
- 【干货分享】dos命令大全
- Linux系统下为何病毒少?原因竟是这个?
- Top sort 双队列
- 三星sm865
- elasticsearch-java异常
- Django Forms实例
- 学习电脑编码utf-8,ansi编码的基础知识等
- soapUi在调用过程中日期参数
- GitHub 中国区前 100 名到底是什么样的人?