POJ 2187 Beauty Contest (凸包+旋转卡壳)

解题思路:很显然需要求一个凸包,距离最远的点显然凸包上,因为这道题的数据范围比较小,所以可以平方枚举,当然更好的做法是旋转卡壳法。

AC代码:

/*
* @Author: wchhlbt
* @Last Modified time: 2017-09-07
*/

//#include <bits/stdc++.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <limits>
#include <climits>
#include <cstdio>

#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 50007
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
#define _  << "  " <<
using namespace std;

typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
const double PI = acos(-1.0);
int dx[5] = {0,0,1,-1,0};
int dy[5] = {1,-1,0,0,0};
inline int read(){ int cnt;    scanf("%d",&cnt);   return cnt;}


typedef struct  Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
}Vector;

Vector operator + (const Vector & A,const Vector & B) { return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (const Point & A,const Point & B) { return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (const Vector & A,double p) { return Vector(A.x*p,A.y*p);}
Vector operator / (const Vector & A,double p) { return Vector(A.x/p,A.y/p);}

bool operator < (const Point &A, const Point &B)
{
    return A.x<B.x||(A.x==B.x&&A.y<B.y);
}

int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return  x<0? -1 : 1 ;
}

bool operator == (const Point &A,const Point &B)
{
    return dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)==0;
}

//向量点乘
double Dot(const Vector &A, const Vector &B) {  return A.x*B.x+A.y*B.y;  }

//求向量的长度或两点距离
double Length(const Vector &A){ return (Dot(A,A)); }

//返回两个向量夹角的弧度值
double Angle(const Vector &A, const Vector &B)
{
    return  acos(Dot(A,B)/Length(A)/Length(B));
}

//向量叉积。共起点的两个向量A,B,若A叉乘B大于0,则B在A的左边。
double Cross(const Vector &A, const Vector &B){ return A.x*B.y-A.y*B.x ;}

//用叉积求三个点组成的三角形的面积。面积为0表示三点共线。
double Area2(const Point &A,const Point &B,const Point &C ){return Cross(B-A,C-A) ;}

//向量绕起点旋转,rad为逆时针旋转的弧度。
//Rotate(A,-rad)表示将A顺时针旋转rad
Vector Rotate(const Vector &A,double rad)
{
    return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}

//计算A向量的单位法向量,即左转90度,将单位归一化
//调用前确保A向量不是零向量
Vector Normal(Vector A)
{
    double L=Length(A);
    return Vector(-A.y/L,A.x/L);
}


int ConvexHull (Point *p,int n ,Point *ch)
{
    sort(p,p+n);  //先比较x坐标,再比较y坐标
    //上凸包
	int m=0;
    for(int i=0;i<n;i++)
    {
        while(m>1&&dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) m--;
        ch[m++]=p[i];
	}
	//下凸包
    int k=m;
    for(int i=n-2;i>=0;i--)
    {
        while(m>k&&dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) m--;
        ch[m++]=p[i];
    }
    if(n>1) m--;
    return m;
}
//旋转卡壳法 求凸包直径p 凸包点集 
//利用三角形面积法来判断到一条边最远的点是哪个点
//如果想要求出对踵点,可以开一个结构体维护到每个点最远的那个点的编号和两者距离
double RotatingCaliper(int m, Point *ch)
{
	int q = 1;
	ch[m] = ch[0];
	double ans = 0.0;
	for(int p = 0; p<m; p++){
		while(fabs( Area2(ch[p],ch[p+1],ch[q+1]) ) > fabs( Area2(ch[p],ch[p+1],ch[q]) ))	q = (q+1)%m;
		ans = max(ans,max(Length(ch[q+1]-ch[p]) , Length(ch[q+1]-ch[p+1])));
		ans = max(ans,max(Length(ch[q]-ch[p]) , Length(ch[q]-ch[p+1])));
	}
	return ans;
}
Point p[maxn];
Point ans[maxn];

int main()
{
	int n;
	while(~scanf("%d",&n)){
		for(int i = 0; i<n; i++){
			int x = read();
			int y = read();
			p[i] = Point(x,y);
		}
		int cnt = ConvexHull(p,n,ans);
		double res = RotatingCaliper(cnt,ans);
		printf("%.0f\n",res);
	}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值