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;
}

最新文章

  1. Swift学习笔记-ARC
  2. js指定分隔符连接数组元素join()
  3. C++ 画星号图形——空心梯形(核心代码记录)
  4. 【Hibernate】Hibernate系列1之概述
  5. Java多线程中的进程,线程,并行,并发
  6. c#实现数据的左补右补功能
  7. 野指针及c++指针使用注意点
  8. HDU5653 Bomber Man wants to bomb an Array 简单DP
  9. Microsoft Windows Sharepoint Services V3.0 安装图示
  10. IOC容器在框架中的应用
  11. 用django搭建一个简易blog系统(翻译)(一)
  12. 【干货分享】dos命令大全
  13. Linux系统下为何病毒少?原因竟是这个?
  14. Top sort 双队列
  15. 三星sm865
  16. elasticsearch-java异常
  17. Django Forms实例
  18. 学习电脑编码utf-8,ansi编码的基础知识等
  19. soapUi在调用过程中日期参数
  20. GitHub 中国区前 100 名到底是什么样的人?

热门文章

  1. POJ 3070 - 快速矩阵幂求斐波纳契数列
  2. HDU_1063_Exponentiation_大数
  3. linux调试环境时常用的命令 及 常识
  4. iview Table表格单选框互斥
  5. 水图(牛客练习赛(DFS搜索))
  6. 转:Windows Phone 7 设计简介
  7. Emacs的undo与redo
  8. hdu2013 蟠桃记【C++】
  9. SSL常用专业缩略语汇总
  10. Debug 集子