HDU1392 Surround the Trees

Convex hull

Water it. . Find the circumference convex hull configuration. .

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int ret = 0, w = 0; char ch = 0;
    while(!isdigit(ch)){
        w |= ch == '-', ch = getchar();
    }
    while(isdigit(ch)){
        ret = (ret << 3) + (ret << 1) + (ch ^ 48);
        ch = getchar();
    }
    return w ? -ret : ret;
}
inline int lcm(int a, int b){ return a / __gcd(a, b) * b; }
template <typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
#define eps 1e-6;
struct Point{
    double x, y;
    Point(double x, double y): x(x), y(y){}
    Point operator + (Point &a){
        return Point(x + a.x, y + a.y);
    }
    Point operator - (Point &a){
        return Point(x - a.x, y - a.y);
    }
    bool operator < (const Point &a) const {
        if(x == a.x) return y < a.y;
        return x < a.x;
    }
    bool operator == (Point &a){
        if(x == a.x && y == a.y) return true;
        return false;
    }
    double abs(){
        return sqrt(x * x + y * y);
    }
};

typedef Point Vector;
vector<Point> p, u, l;
int n;

double cross(Vector a, Vector b){
    return a.x * b.y - a.y * b.x;
}

bool isClock(Point a, Point b, Point c){
    Vector x = b - a;
    Vector y = c - a;
    return cross(x, y) < eps;
}

double dis(Point a, Point b){
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));;
}

int main(){

    while(~scanf("%d", &n) && n){
        p.clear(), u.clear(), l.clear();
        for(int i = 1; i <= n; i ++){
            double x, y;
            scanf("%lf%lf", &x, &y);
            p.push_back(Point(x, y));
        }
        if(n == 1){
            printf("0.00\n");
            continue;
        }
        if(n == 2){
            printf("%.2lf\n", dis(p[0], p[1]));
            continue;
        }
        sort(p.begin(), p.end());
        u.push_back(p[0]), u.push_back(p[1]);
        for(int i = 2; i < p.size(); i ++){
            for(int n = u.size(); n >= 2 && (!isClock(u[n - 2], u[n - 1], p[i])); n --)
                u.pop_back();
            u.push_back(p[i]);
        }
        l.push_back(p[p.size() - 1]), l.push_back(p[p.size() - 2]);
        for(int i = p.size() - 3; i >= 0; i --){
            for(int n = l.size(); n >= 2 && (!isClock(l[n - 2], l[n - 1], p[i])); n --)
                l.pop_back();
            l.push_back(p[i]);
        }
        for(int i = 1; i < u.size() - 1; i ++){
            l.push_back(u[i]);
        }
        double ans = 0;
        for(int i = 1; i < l.size(); i ++){
            ans += dis(l[i], l[i - 1]);
        }
        ans += dis(l[0], l[l.size() - 1]);
        printf("%.2lf\n", ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/onionQAQ/p/11242619.html