首页 > 代码库 > 凸包模板(codevs1298)(洛谷2742)

凸包模板(codevs1298)(洛谷2742)

就是给你若干个点,求凸包的周长。

#include<bits/stdc++.h>#define maxn 2000000using namespace std;struct Point {	double x,y;	Point(double x=0,double y=0):x(x),y(y){}}p[maxn],ch[maxn];int n;typedef Point Vector;//将点和向量区分开来 double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}//点积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));}Vector operator -(Point A,Point B) {return Vector(A.x-B.x,A.y-B.y);}//点-点等于向量 bool operator < (Point a,Point b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}double CovexHull(Point* p,int n,Point* ch){	sort(p,p+n);	int m=0;	for(int i=0;i<n;i++)	{		while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;		ch[m++]=p[i];	}	int k=m;	for(int i=n-2;i>=0;i--)	{		while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;		ch[m++]=p[i];	}	if(n>1) m--;	double ans=0;	for(int i=0;i<m;i++)		ans+=dis(ch[i],ch[i+1]);	return ans;} int main(){    cin>>n;    for(int i=0;i<n;i++)    	cin>>p[i].x>>p[i].y;    printf("%.1f\n",CovexHull(p,n,ch));    return 0;}

 

凸包模板(codevs1298)(洛谷2742)