圈奶牛<计算几何>

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct point{//x横坐标,y纵坐标.
        double x;
        double y;
}p[10000],chosen[10000];
bool cmp(point a,point b){
    if(a.y==b.y)
        return a.x<b.x;
    else
        return a.y<b.y;
}
double count(double x,double y){//计算连线长度,x为Δx,y为Δy.
    return sqrt(pow(x,2)+pow(y,2));
}
double det(point o,point a,point b){//计算叉积,判断OA是否在OB逆时针方向.
    return (a.y-o.y)*(b.x-o.x)-(a.x-o.x)*(b.y-o.y);
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>p[i].x>>p[i].y;
    sort(p,p+n,cmp);
    int choose=0;
    for(int i=0;i<n;i++){
        while(choose>=2&&det(chosen[choose-1],p[i],chosen[choose-2])<0)
            choose--;
        chosen[choose++]=p[i];
    }
    int mark=choose+1;
    for(int i=n-2;i>=0;i--){
        while(choose>=mark&&det(chosen[choose-1],p[i],chosen[choose-2])<0)
            choose--;
        chosen[choose++]=p[i];
    }
    double tot=0;
    for(int i=0;i<choose-1;i++)
        tot+=count(chosen[i].x-chosen[i+1].x,chosen[i].y-chosen[i+1].y);
    printf("%.2f",tot);
    return 0;
}

你可能感兴趣的:(C语言)