计算几何 _ 凸包
作者:互联网
代码
#include<bits/stdc++.h>
#define int long long
#define pdd pair<double ,double >
using namespace std;
const int N=1e5+10;
const int mod=1000000007;
int n;
pdd q[N];
bool used[N];
int stk[N];
pdd operator-(pdd a,pdd b){
return {a.first-b.first,a.second-b.second};
}
double cross(pdd a,pdd b){
return a.first*b.second-a.second*b.first;
}
double area(pdd a,pdd b,pdd c){
return cross(b-a,c-a);
}
double get_dist(pdd a,pdd b){
double dx=a.first-b.first;
double dy=a.second-b.second;
return sqrt(dx*dx+dy*dy);
}
double andrew(){
sort(q,q+n);
int top=0;
for(int i=0;i<n;i++){
while(top>=2 && area(q[stk[top-1]],q[stk[top]],q[i])<=0){
// 凸包边界上的点即使被从栈中删掉,也不能删掉used上的标记
if(area(q[stk[top-1]],q[stk[top]],q[i])<0)
used[stk[top--]]=false;
else top--;
}
stk[++top]=i;
used[i]=true;
}
used[0]=false;
for(int i=n-1;i>=0;i--){
if(used[i])continue;
while(top>=2 && area(q[stk[top-1]],q[stk[top]],q[i])<=0)
top--;
stk[++top]=i;
}
double ans=0;
for(int i=2;i<=top;i++){
ans+= get_dist(q[stk[i-1]],q[stk[i]]);
}
return ans;
}
signed main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>q[i].first >> q[i].second;
}
double ans=andrew();
printf("%.2lf\n", ans);
return 0;
}
标签:int,double,top,凸包,second,计算,几何,pdd,first 来源: https://www.cnblogs.com/kingwz/p/16676327.html