加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

BZOJ 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘 凸包

发布时间:2020-12-14 03:16:59 所属栏目:大数据 来源:网络整理
导读:题目大意:给出n个点,求包围所有点的多边形的最小周长。 裸凸包,练手题。 注意极角排序从下标为2的位置开始排… #include cstdio #include cmath #include cstring #include algorithm #define N 5005 using namespace std;inline int s q(int x) { return

题目大意:给出n个点,求包围所有点的多边形的最小周长。

裸凸包,练手题。

注意极角排序从下标为2的位置开始排…

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define N 5005
using namespace std;
inline int sq(int x) { return x*x; }
struct Point {
    int x,y;
    Point(int _x=0,int _y=0):x(_x),y(_y) {}
    void scan() {scanf("%d%d",&x,&y);}
    Point operator + (const Point& rhs) const { return Point(x+rhs.x,y+rhs.y); }
    Point operator - (const Point& rhs) const { return Point(x-rhs.x,y-rhs.y); }
    int operator * (const Point& rhs) const { return x*rhs.y-y*rhs.x; }
    int operator ^ (const Point& rhs) const { return x*rhs.x+y*rhs.y; }
    int operator | (const Point& rhs) const { return sq(x-rhs.x)+sq(y-rhs.y); }
    bool operator < (const Point& rhs) const { return x<rhs.x || x==rhs.x && y<rhs.y; }
}p[N],q[N];
int n,top;
bool cmp(const Point& lhs,const Point& rhs) {
    int tmp=(lhs-p[1])*(rhs-p[1]);
    if(tmp) return tmp>0;
    return (lhs|p[1])<(rhs|p[1]);
}
void Graham() {
    int tmp=1;
    for(int i=2;i<=n;i++)
        if(p[i]<p[tmp])
            tmp=i;
    if(tmp!=1) swap(p[tmp],p[1]);
    sort(p+2,p+n+1,cmp);
    q[++top]=p[1];
    for(int i=2;i<=n;i++) {
        while(top>1 && (q[top]-q[top-1])*(p[i]-q[top-1])<=0) top--;
        q[++top]=p[i];
    }
    return ;
}
double calc_ans() {
    q[top+1]=q[1];
    double ans=0;
    for(int i=1;i<=top;i++) ans+=sqrt(q[i]|q[i+1]);
    return ans;
}
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;i++) p[i].scan();
    Graham();
    printf("%.2fn",calc_ans());
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读