hdu:surrounding the trees(凸包)

发布时间 2023-05-03 03:24:42作者: ruoye123456

Problem Description
There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.

There are no more than 100 trees.

Input
The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.

Zero at line for number of trees terminates the input for your program.

Output
The minimal length of the rope. The precision should be 10^-2.

Sample Input
9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0

Sample Output
243.06

计算几何-凸包问题(andrewScan)

注意两个点的凸包是连接其的线段长

点击查看代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps=1e-10;
const int COUNTER_CLOCKWISE=1;
const int CLOCKWISE=-1;
const int ONLINE_BACK=2;
const int ONLINE_FRONT=-2;
const int ON_SEGMENT=0;
class Point {
    public :
    double x,y;
    Point(double x=0,double y=0) :x(x),y(y) {}
    
    double abs() {return sqrt(norm());}
    double norm() {return x*x+y*y;}
    
    Point operator + (const Point &p) const
    {
        return Point(x+p.x,y+p.y);
    }
    Point operator - (const Point &p) const
    {
        return Point(x-p.x,y-p.y);
    }
    
    bool operator < (const Point &p) const
    {
        return x!=p.x?x<p.x:y<p.y;
    }
    
    bool operator == (const Point &p) const
    {
        return fabs(x-p.x)<eps&&fabs(y-p.y)<eps;
    }
};

typedef Point Vector;
typedef vector<Point> Polygon;
double cross(Vector a,Vector b)
{
    return a.x*b.y-a.y*b.x;
}

double dot(Vector a,Vector b)
{
    return a.x*b.x+a.y*b.y;
}

int ccw(Point p0,Point p1,Point p2)
{
    Vector a=p1-p0;
    Vector b=p2-p0;
    if(cross(a,b)>eps) return COUNTER_CLOCKWISE;
    if(cross(a,b)<-eps) return CLOCKWISE;
    if(dot(a,b)<-eps) return ONLINE_BACK;
    if(a.norm()<b.norm()) return ONLINE_FRONT;
    return ON_SEGMENT;
}

Polygon andrewScan(Polygon a)
{
    Polygon u,l;
    if(a.size()<3) return a;
    sort(a.begin(),a.end());
    u.push_back(a[0]);
    u.push_back(a[1]);
    l.push_back(a[a.size()-1]);
    l.push_back(a[a.size()-2]);
     
    for(int i=2;i<a.size();++i)
    {
        for(int n=u.size();n>=2&&ccw(u[n-2],u[n-1],a[i])!=CLOCKWISE;n--)
        u.pop_back();
        u.push_back(a[i]);
    }
    
    for(int i=a.size()-3;i>=0;--i)
    {
        for(int n=l.size();n>=2&&ccw(l[n-2],l[n-1],a[i])!=CLOCKWISE;n--)
        l.pop_back();
        l.push_back(a[i]);
    }
    
    reverse(l.begin(),l.end());
    for(int i=u.size()-2;i>=0;--i) l.push_back(u[i]);
    return l;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    while(cin>>n,n)
    {
        Polygon P;
        P.resize(n);
        for(int i=0;i<n;++i)
        {
            int x,y;
            cin>>x>>y;
            P[i]=Point(x,y);
        }
        auto ans=andrewScan(P);
        double res=0;
        int len=ans.size();
        for(int i=0;i<ans.size();++i)
        {
            Vector t=ans[(i+1)%len]-ans[i];
            res+=t.abs();
        }
        if(n==2)
        {
            Vector v=P[0]-P[1];
            res=v.abs();
        }
        res=round(res*100)/100;
        cout<<fixed<<setprecision(2)<<res<<'\n';
    }
}