- P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
-
#include<bits/stdc++.h> using namespace std; #define maxn 10005 struct node { double x,y; } s[maxn],p[maxn]; int n,tot; double ans,mid; double cj(node a1,node a2,node b1,node b2) { return (a2.x-a1.x)*(b2.y-b1.y)-(b2.x-b1.x)*(a2.y-a1.y); } double dis(node p1,node p2) { return sqrt(((double)(p2.y-p1.y)*(p2.y-p1.y)*1.0)+((double)(p2.x-p1.x)*(p2.x-p1.x))*1.0); } bool cmp(node p1,node p2) { double temp=cj(p[1],p1,p[1],p2); if(temp>0)return 1; if(temp==0&&dis(p[0],p1)<dis(p[0],p2))return 1; return 0; } int main() { scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%lf%lf",&p[i].x,&p[i].y); if(p[i].y<p[1].y) swap(p[1],p[i]); } sort(p+2,p+1+n,cmp); s[1]=p[1]; tot=1; for(int i=2; i<=n; i++) { while(tot>1&&cj(s[tot-1],s[tot],s[tot],p[i])<=0)tot--; s[++tot]=p[i]; } ans=dis(s[1],s[tot]); for(int i=1; i<tot; i++) ans+=dis(s[i],s[i+1]); printf("%.2lf\n",ans); return 0; }
P2742 圈奶牛Fencing the Cows-凸包-模板
最新推荐文章于 2019-11-11 19:30:57 发布