「Luogu P2742」【模板】二维凸包 发表于 2018-10-21 | 字数统计: 637 | 阅读时长 ≈ 3 给定一个点集,求它的凸包的周长。 链接Luogu P2742 题解二维凸包的模板题,没有什么好说的。不会凸包的右转这里但是最后求周长是一定要算点的距离,而不是向量的长度。被卡了两次 代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153#include<bits/stdc++.h>using namespace std;typedef int ll;typedef double db;const ll MAXN=1e4+51;struct Point{ db x,y; Point(ll x=0,ll y=0) { this->x=x,this->y=y; } inline bool operator <(const Point &rhs)const { return y==rhs.y?x<rhs.x:y<rhs.y; } inline db polar() { return atan2(y,x); }};typedef Point Vector;Point p[MAXN];ll cnt,minn;db res;inline ll read(){ register ll num=0,neg=1; register char ch=getchar(); while(!isdigit(ch)&&ch!='-') { ch=getchar(); } if(ch=='-') { neg=-1; ch=getchar(); } while(isdigit(ch)) { num=(num<<3)+(num<<1)+(ch-'0'); ch=getchar(); } return num*neg;}inline Vector operator +(Vector x,Vector y){ return Vector(x.x+y.x,x.y+y.y);}inline Vector operator -(Vector x,Vector y){ return Vector(x.x-y.x,x.y-y.y);}inline Vector operator *(Vector x,db y){ return Vector(x.x*y,x.y*y);}inline Vector operator /(Vector x,db y){ return Vector(x.x/y,x.y/y);}inline db dot(Vector x,Vector y){ return x.x*y.x+x.y*y.y;}inline db length(Vector x){ return sqrt(dot(x,x));}inline db angle(Vector x,Vector y){ return acos(dot(x,y)/length(x)/length(y));}inline db cross(Vector x,Vector y){ return x.x*y.y-x.y*y.x;}inline db dist(Vector x,Vector y){ db xx=x.x-y.x,yy=x.y-y.y; return sqrt(xx*xx+yy*yy);}inline bool cmp(Point x,Point y){ double xx=cross(x-p[1],y-p[1]); if(xx>0) { return 1; } if(!xx&&length(x-p[1])<length(y-p[1])) { return 1; } return 0;}inline deque<Point> convexHull(Point *p,ll size){ Point top; deque<Point>vec; if(size==1) { vec.push_back(p[1]); return vec; } if(size==2) { vec.push_back(p[1]),vec.push_back(p[2]); return vec; } vec.push_back(p[1]),vec.push_back(p[2]); for(register int i=3;i<=size;i++) { top=vec.back(); while(cross(top-vec[vec.size()-2],p[i]-top)<0) { vec.pop_back(),top=vec.back(); } vec.push_back(p[i]); } return vec;}int main(){ cnt=read(); p[0].x=p[0].y=100000000000.0; if(cnt==0) { printf("0.00"); return 0; } for(register int i=1;i<=cnt;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); if(p[i]<p[minn]) { minn=i; } } swap(p[minn],p[1]),sort(p+2,p+cnt+1,cmp); deque<Point>pt=convexHull(p,cnt); if(pt.size()==1) { printf("%.2lf",0.0); } else { for(register int i=1;i<pt.size();i++) { res+=dist(pt[i],pt[i-1]); } res+=dist(pt[0],pt[pt.size()-1]); printf("%.2lf",res); }} 本文作者: Karry5307 本文链接: http://karry5307.github.io/2018/10/21/「Luogu-P2742」【模板】二维凸包/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!