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

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define db double
using namespace std;
const int N=5005,INF=1e7;
int n,sta[N],top;db ans;
struct Node{
int x,y;
Node(int x=0,int y=0):x(x),y(y) {}
bool operator< (const Node &b)const
{return x<b.x||(x==b.x&&y<b.y);}
Node operator- (const Node &b)const
{return Node(b.x-x,b.y-y);}
}a[N];
ll Sqr(int x){return (ll)x*x;}
ll Cross(Node a,Node b){return (ll)a.x*b.y-(ll)a.y*b.x;}
db dist(Node a,Node b){return sqrt(Sqr(a.x-b.x)+Sqr(a.y-b.y));}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
while(top>1&&Cross(a[i]-a[sta[top]],a[i]-a[sta[top-1]])>=0)top--;
sta[++top]=i;
}
for(int i=2;i<=top;i++)ans+=dist(a[sta[i-1]],a[sta[i]]);
for(int i=1;i<=n;i++)a[i].y=INF-a[i].y+1;
sort(a+1,a+n+1);
top=0;
for(int i=1;i<=n;i++)
{
while(top>1&&Cross(a[i]-a[sta[top]],a[i]-a[sta[top-1]])>=0)top--;
sta[++top]=i;
}
for(int i=2;i<top;i++)ans+=dist(a[sta[i-1]],a[sta[i]]);
if(top>1&&a[sta[top]].x!=a[sta[top-1]].x)ans+=dist(a[sta[top-1]],a[sta[top]]);
if(a[1].x==a[2].x)ans+=abs(a[1].y-a[2].y);
printf("%.2f\n",ans);
return 0;
}

posted on 2018-12-19 14:56  Narh  阅读(123)  评论(0编辑  收藏  举报