DESCRIPTION
農夫約翰想要建造一個圍欄用來圍住他的奶牛,可是他資金匱乏。他建造的圍欄必須包括他的奶牛喜歡喫草的所有地點。對於給出的這些地點的座標,計算最短的能夠圍住這些點的圍欄的長度。
INPUT FORMAT
輸入數據的第一行包括一個整數 N。N(0 <= N <= 10,000)表示農夫約翰想要圍住的放牧點的數目。接下來 N 行,每行由兩個實數組成,Xi 和 Yi,對應平面上的放牧點座標(-1,000,000 <= Xi,Yi <= 1,000,000)。數字用小數表示。
OUTPUT FORMAT
輸出必須包括一個實數,表示必須的圍欄的長度。答案保留兩位小數。
SAMPLE INPUT
4
4 8
4 12
5 9.3
7 8
SAMPLE OUTPUT
12.00
SOLUTION
一道模板題
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 10e-6
using namespace std;
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
Point operator + (const Point p)
{
return Point(x+p.x,y+p.y);
}
Point operator - (const Point p)
{
return Point(x-p.x,y-p.y);
}
}p[10005],ch[10005];
typedef Point Vector;
int n;
int dcmp(double x)
{
if(fabs(x)<eps)return 0;
return x>0?1:-1;
}
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
double Dis(Point A,Point B)
{
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
bool operator < (const Point& A,const Point& B)
{
if(A.x!=B.x)return A.x<B.x;
return A.y<B.y;
}
int ConvexHull()
{
sort(p+1,p+1+n);
int top=0;
for(int i=1;i<=n;i++)
{
while(top>1&&dcmp(Cross(p[i]-ch[top],ch[top]-ch[top-1]))<=0)top--;
ch[++top]=p[i];
}
int k=top;
for(int i=n-1;i>=1;i--)
{
while(top>k&&dcmp(Cross(p[i]-ch[top],ch[top]-ch[top-1]))<=0)top--;
ch[++top]=p[i];
}
return top;
}
double PolyCirc(int m)
{
double c=0;
for(int i=1;i<m;i++)
{
c+=Dis(ch[i+1],ch[i]);
}
return c;
}
int main()
{
//freopen("fc.in","rb",stdin);
//freopen("fc.out","wb",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
printf("%.2lf",PolyCirc(ConvexHull()));
return 0;
}