计算几何?学了不会?会了不对?
1 #include<bits/stdc++.h> 2 using namespace std; 3 const double EPS=1e-6; 4 inline int dcmp(double x){ 5 if(fabs(x)<EPS)return 0; 6 else if(x<0)return -1; 7 else return 1; 8 } 9 #define Point Vector 10 struct Vector{ 11 double x,y; 12 Vector(double X=0.0,double Y=0.0):x(X),y(Y){} 13 Vector operator+(const Vector &A)const{ 14 return Vector(x+A.x,y+A.y); 15 } 16 Vector operator-(const Vector &A)const{ 17 return Vector(x-A.x,y-A.y); 18 } 19 double operator*(const Vector &A)const{//点乘· 20 return x*A.x+y*A.y; 21 } 22 Vector operator*(const double &X)const{//数乘 23 return Vector(x*X,y*X); 24 } 25 bool operator==(const Vector&A)const{ 26 return ( !dcmp(x-A.x) && !dcmp(y-A.y) ); 27 } 28 bool operator<(const Point &A)const{ 29 return dcmp(x-A.x) ? x<A.x : y<A.y ; 30 } 31 }; 32 inline double polarangle(Vector &A){//向量极角 33 return atan2(A.y,A.x); //?什么函数? 34 } 35 inline double cross(const Vector &A,const Vector &B){//叉乘* (只取数值大小) Arg(A)<Arg(B)时取正值 36 return A.x*B.y-A.y*B.x; 37 } 38 inline double length(const Vector &A){ 39 return sqrt(A.x*A.x+A.y*A.y); 40 } 41 inline Point meet(const Point &P1,const Vector &u,const Point &P2,const Vector &v){ 42 return P1+u*(cross(P1-P2,v)/cross(v,u)); 43 } 44 inline double dis(const Point &A,const Point &P,const Vector &u){ 45 return fabs(cross(A-P,u)/length(u)); 46 } 47 /*inline double dis(const Point &A,const Point &P,const Point &Q){ 48 return fabs(cross(A-P,Q-P)/length(Q-P)); 49 }*/ 50 const int maxn=1e5+5; 51 Point p[maxn],sta[maxn]; 52 int n,k; 53 54 inline void Andrew(){ 55 sort(p+1,p+n+1); 56 sta[0]=sta[k=1]=p[1];//放1 or 放2? 57 for(int i=2;i<=n;i++){ 58 if(p[i]==p[i-1])continue;//去重 59 while(cross(p[i]-sta[k],sta[k]-sta[k-1])<0)k--; 60 sta[++k]=p[i]; 61 } 62 for(int i=n-1;i;i--){//必须循环到1:保证下凸包的正确性 63 if(p[i]==p[i+1])continue;//去重 64 while(cross(p[i]-sta[k],sta[k]-sta[k-1])<0)k--;//凸包边上无点:<= 65 sta[++k]=p[i]; 66 } 67 } 68 int main(){ 69 scanf("%d",&n); 70 for(int i=1;i<=n;i++){ 71 scanf("%lf %lf",&p[i].x,&p[i].y); 72 } 73 Andrew(); 74 double ans=0.0; 75 for(int i=1;i<k;i++)ans+=length(sta[i+1]-sta[i]); 76 printf("%.2lf",ans); 77 return 0; 78 }