poj1113凸包

时间:2022-11-16 15:02:49

就是求凸包的周长加以l为半径的圆周长,证明略

由于之前写过叉积,所以graham扫描算法不是很难理解

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; struct point{
double x,y;
};
point p[N],s[N];
int top,n;
double dir(point p1,point p2,point p3)
{
return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.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 comp(point a,point b)
{
int te=dir(p[],a,b);
if(te<)return ;
if(te==&&dis(p[],a)<dis(p[],b))return ;
return ;
}
void graham()
{
int pos,minx,miny;
minx=miny=inf;
for(int i=;i<n;i++)
{
if(p[i].x<minx||(p[i].x==minx&&p[i].y<miny))
{
minx=p[i].x;
miny=p[i].y;
pos=i;
}
}
swap(p[],p[pos]);
sort(p+,p+n,comp);
p[n]=p[];
s[]=p[],s[]=p[],s[]=p[];
top=;
for(int i=;i<=n;i++)
{
while(dir(s[top-],s[top],p[i])>=&&top>=)top--;
s[++top]=p[i];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int l;
cin>>n>>l;
for(int i=;i<n;i++)cin>>p[i].x>>p[i].y;
graham();
double ans=*pi*l;
for(int i=;i<top;i++)
{
// cout<<s[i].x<<" "<<s[i].y<<endl;
if(i==top-)ans+=dis(s[i],s[]);
else ans+=dis(s[i],s[i+]);
}
cout<<(int)(ans+0.5)<<endl;
return ;
}
/*
4 3
0 0
1 0
0 1
1 1
*/