栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 2187 Beauty Contest

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 2187 Beauty Contest

#include <cstdio>#include <cmath>#include <algorithm>#define vector pointusing std::sort;using std::swap;using std::max;const double PI = acos(-1.00);struct point{    int x,y;    point(int xx = 0,int yy = 0)    {        x = xx;        y = yy;    }    point operator - (point s)    {        return point(x - s.x, y - s.y);    }    double len()    {        return sqrt(x * x + y * y + 0.0);    }    int len2()    {        return x * x + y * y;    }};point p[50010];int n,l,hull_list[50010];int cross_product(vector v1,vector v2){    return v1.x * v2.y - v1.y * v2.x;}bool cmp(point& a,point& b){    int cp = cross_product(vector(a - p[0]),vector(b - p[0]));    if(cp > 0 || (cp == 0 && vector((a - p[0])).len2() < vector((b - p[0])).len2() ) )        return true;    else        return false;}//基点取p[0],逆时针扫描,返回凸包上的点数int Graham_scan(){    int top = 1;    sort(p + 1,p + n,cmp);    hull_list[0] = 0;    hull_list[1] = 1;    for(int i = 2;i < n;i++)    {        while(top >= 1 && cross_product(vector(p[hull_list[top]] - p[hull_list[top - 1]]),vector(p[i] - p[hull_list[top]])) <= 0)//<=为一条边上只留两个顶点,<则为一条边上可以留多个顶点 top--;        hull_list[++top] = i;    }    return top + 1;}//旋转卡壳求最远点对的距离的平方,p[]的大小要比n的最大值多2,凸包点集的顺序为逆时针int rotating_caliper(point p[],int& n){    int counter = 0,dis = 0;    p[n] = p[0];    p[n + 1] = p[1];    int j = 2;    for(int i = 0;i < n;i++)    {        while(cross_product(p[i + 1] - p[i],p[j] - p[i]) < cross_product(p[i + 1] - p[i],p[j + 1] - p[i])) j = (j + 1) % n;        dis = max(max((p[i] - p[j]).len2(),(p[i + 1] - p[j]).len2()),dis);    }    return dis;}int main(){    while(~scanf("%d",&n))    {        int lowest = 0;        for(int i = 0;i < n;i++)        { scanf("%d%d",&p[i].x,&p[i].y); if(p[lowest].y > p[i].y || (p[lowest].y == p[i].y && p[lowest].x > p[i].x))     lowest = i;        }        swap(p[lowest],p[0]);        l = Graham_scan();        for(int i = 0;i < l;i++) p[i] = p[hull_list[i]];        printf("%dn",rotating_caliper(p,l));    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/370278.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号