裸的凸包..很好写,废话不说,直接贴代码。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #define rep(i,r) for(int i=0;i<r;i++) using namespace std; const int maxn=10000+5; struct P { double x,y; P(double x=0,double y=0):x(x),y(y) {} P operator + (P o) { return P(x+o.x,y+o.y); } P operator - (P o) { return P(x-o.x,y-o.y); } bool operator == (const P &o) const { return x==o.x && y==o.y; } bool operator < (const P &o) const { return x<o.x || (x==o.x && y<o.y); } }; P ans[maxn],p[maxn]; inline double cross(P a,P b) { return a.x*b.y-a.y*b.x; } inline double dist(P o) { return sqrt(o.x*o.x+o.y*o.y); } double convexHull(int n) { sort(p,p+n); int m=0; rep(i,n) { while(m>1 && cross(ans[m-1]-ans[m-2],p[i]-ans[m-2])<=0) m--; ans[m++]=p[i]; } int k=m; for(int i=n-2;i>=0;i--) { while(m>k && cross(ans[m-1]-ans[m-2],p[i]-ans[m-2])<=0) m--; ans[m++]=p[i]; } if(n>1) m--; double length=0; rep(i,m) length+=dist(ans[i]-ans[i+1]); return length; } int main() { freopen("fc.in","r",stdin); freopen("fc.out","w",stdout); int n; cin>>n; rep(i,n) scanf("%lf%lf",&p[i].x,&p[i].y); printf("%.2lf\n",convexHull(n)); return 0; }
时间: 2024-11-02 01:23:14