题解
好难解释必然性感觉像模拟??
code
#include<bits/stdc++.h>
using namespace std;
int q[100005]={0};
struct node
{
double x,y;
}a[100005];
double dis(int b,int c)
{
node i=a[b],j=a[c];
return sqrt((i.x-j.x)*(i.x-j.x)+(i.y-j.y)*(i.y-j.y));
}
bool cmp(node b,node c)
{
if(b.x!=c.x) return b.x<c.x;
return b.y>c.y;//贪心地往小的圈
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].x>>a[i].y;
}
sort(a+1,a+n+1,cmp);
q[1]=1;
q[2]=2;
int top=2;
for(int i=3;i<=n;i++)//扫下边
{
while(top>1)
{
double x1=a[q[top]].x-a[q[top-1]].x,y1=a[q[top]].y-a[q[top-1]].y;
double x2=a[i].x-a[q[top]].x,y2=a[i].y-a[q[top]].y;
if(x1*y2-x2*y1<=0)top--;
else break;
}
q[++top]=i;
}
int top1=top;
q[++top]=n-1;
for(int i=n-2;i>=1;i--)//扫上边
{
while(top>top1)
{
double x1=a[q[top]].x-a[q[top-1]].x,y1=a[q[top]].y-a[q[top-1]].y;
double x2=a[i].x-a[q[top]].x,y2=a[i].y-a[q[top]].y;
if(x1*y2-x2*y1<=0)top--;
else break;
}
q[++top]=i;
}
double ans=0;
for(int i=1;i<=top;i++)
{
//printf("%d:%d\n",i,q[i]);
ans+=dis(q[i],q[i%top+1]);
}
printf("%.2lf",ans);
return 0;
}
标签:node,P2742,int,Fencing,top,凸包,double,y1,y2
From: https://www.cnblogs.com/pure4knowledge/p/18085929