# Bzoj1670 [usaco2006 oct]building the moat护城河的挖掘

## 凸包模板题

Posted by yjjr's blog on January 19, 2018 1 minutes to read

# 题目

Description

Input

• 第1行: 一个整数，N * 第2..N+1行: 每行包含2个用空格隔开的整数，x[i]和y[i]，即第i股泉水的位 置坐标 Output

• 第1行: 输出一个数字，表示满足条件的护城河的最短长度。保留两位小数 Sample Input 20

2 10

3 7

22 15

12 11

20 3

28 9

1 12

9 3

14 14

25 6

8 1

25 1

28 4

24 12

4 15

13 5

26 5

21 11

24 4

1 8

Sample Output 70.87 HINT

Source

# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
{
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,top;double ans;
const int maxn=5006;
struct P{int x,y;}p[maxn],s[maxn];
inline P operator-(P a,P b){P t;t.x=a.x-b.x;t.y=a.y-b.y;return t;}
inline ll operator*(P a,P b){return a.x*b.y-a.y*b.x;}
inline ll dis(P a,P b){return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}
inline bool operator<(P a,P b){
ll t=(a-p[1])*(b-p[1]);
if(t==0)return dis(p[1],a)<dis(p[1],b);else return t>0;
}
void graham(){
int t=1;
rep(i,2,n)
if(p[i].y<p[t].y||(p[i].y==p[t].y&&p[i].x<p[t].x))t=i;//找纵坐标最小的点
swap(p[1],p[t]);
sort(p+2,p+n+1);//将剩下的点排序
s[++top]=p[1];s[++top]=p[2];
rep(i,3,n){
while((s[top]-s[top-1])*(p[i]-s[top-1])<=0)top--;
s[++top]=p[i];
}
s[top+1]=p[1];
rep(i,1,top)ans+=sqrt(dis(s[i],s[i+1]));
}

int main()
{