您的位置:首页 > Web前端

[USACO5.1]Fencing the Cows

2017-08-23 15:03 176 查看
裸的二维凸包

namespace point
{
struct point
{
double x,y;
point()
{
x=y=0;
}
point makepoint(double x,double y)
{
point k;
k.x=x;
k.y=y;
return k;
}
point operator - (point a)
{
return makepoint(x-a.x,y-a.y);
}
}g[10010];
int len,s[10010];
bool cmp1(point a,point b)
{
return a.y<b.y;
}
bool cmp2(point a,point b)
{
return a.x*b.y<a.y*b.x;
}
bool cmp3(point c,point b,point a)
{
return !cmp2(a-c,b-c);
}
void insert(int x)
{
s[++len]=x;
}
void popfrom(int x)
{
while(len>2&&cmp3(g[x],g[s[len]],g[s[len-1]]))
len--;
}
double sqr(double x)
{
return x*x;
}
double dis(point a,point b)
{
return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}
double calc()
{
double ans=dis(g[s[1]],g[s[len]]);
fr(i,2,len)
ans+=dis(g[s[i]],g[s[i-1]]);
return ans;
}
}
int n;
int main()
{
n=read();
point::point *a=point::g;
fr(i,1,n)
scanf("%lf%lf",&(a+i)->x,&(a+i)->y);
sort(a+1,a+n+1,point::cmp1);
fd(i,n,1)
a[i]=a[i]-a[1];
sort(a+2,a+n+1,point::cmp2);
fr(i,1,2)
point::insert(i);
fr(i,3,n)
{
point::popfrom(i);
point::insert(i);
}
printf("%.2lf\n",point::calc());
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: