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

题目大意:给出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("%.2f\n",calc_ans());
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值