POJ 2187.Beauty Contest

题目:http://poj.org/problem?id=2187

AC代码(C++):

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <queue>
#include <math.h>
#include <string>
#include <string.h>
#include <bitset>

#define INF 0x7fffffff
#define LFINF 1e10
#define eps 1e-7
#define MAXN 100105
#define PI 3.14159265358979323846

using namespace std;

struct Point  
{  
    double x,y;
    Point(double x=0,double y=0):x(x),y(y) {}
};
typedef Point Vector;
Vector operator - (Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); }
int dcmp(double x){  
    if(fabs(x)<eps) return 0;  
    else return x<0?-1:1;  
}

int n;
Point node[50005];
Point convex[50005];
int top;

double Cross(Vector A,Vector B) { return (A.x*B.y-A.y*B.x); }
double Dot(Vector A,Vector B) { return A.x*B.x+A.y*B.y; }  
double Length(Vector A) { return sqrt(Dot(A,A)); }  

bool cmp(Point a, Point b){
    return dcmp(a.y-b.y)==-1 || (dcmp(a.y-b.y)==0 && dcmp(a.x-b.x)==-1);
}
void grahamScan(){
	sort(node,node+n,cmp);
    int tmp;
    top = 1;
    convex[0]=node[0], convex[1]=node[1];
    for(int i=2; i<n; i++){
        while(top>0 && dcmp(Cross(convex[top]-convex[top-1], node[i]-convex[top-1])) <= 0)top--;
        convex[++top] = node[i];
    }
    tmp = top;
    for(int i=n-2; i>=0; i--){
        while(top>tmp && dcmp(Cross(convex[top]-convex[top-1], node[i]-convex[top-1])) <= 0)top--;
        convex[++top] = node[i];
    }
}

double rotating_calipers(){  
    int q = 1;
    double ans = 0;
    convex[top] = convex[0];
    for(int p = 0; p < top; p++)  
    {  
        while(dcmp(Cross(convex[p+1]-convex[p],convex[q+1]-convex[p])-Cross(convex[p+1]-convex[p],convex[q]-convex[p]))==1)
            q=(q+1)%top;
        ans=max(ans,max(Length(convex[p]-convex[q]),Length(convex[p+1]-convex[q+1])));
    }  
    return ans;   
}

int main(){
	cin>>n;
	for(int i = 0; i < n; i++)cin>>node[i].x>>node[i].y;
	grahamScan();
	double ans = rotating_calipers();
	ans *= ans;
	printf("%.0lf",ans);
}
总结: 凸包. 先计算凸包, 然后用旋转卡壳法计算凸包直径.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值