HDU 1392 (计算凸包周长)

原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1392
题意: 让你找出最小的凸包周长 。
思路:凸包裸题,坑点在于注意特判n=1和n=2的情况
方法一:极角排序–Graham算法

#include <bits/stdc++.h>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)+1
#define CLR(x,y) memset((x),y,sizeof(x))
#define fuck(x) cerr << #x << "=" << x << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int seed = 131;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int n;
struct Point {
    double x, y;
    Point() {}
    Point(double x, double y): x(x), y(y) {}
};
Point po[maxn], p[maxn];
typedef Point Vector;
int dcmp(double x) { //返回x的正负
    if (fabs(x) < eps)return 0;
    return x < 0 ? -1 : 1;
}
Vector operator-(Vector A, Vector B) {
    return Vector(A.x - B.x, A.y - B.y);
}
Vector operator+(Vector A, Vector B) {
    return Vector(A.x + B.x, A.y + B.y);
}
Vector operator*(Vector A, double p) {
    return Vector(A.x * p, A.y * p);
}
Vector operator/(Vector A, double p) {
    return Vector(A.x / p, A.y / p);
}
bool operator<(const Point&a,const Point&b) {return a.x<b.x||(a.x==b.x&&a.y<b.y);}
bool operator==(const Point&a, const Point&b) {
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
double Dot(Vector A, Vector B) { //点积
    return A.x * B.x + A.y * B.y; //如果改成整形记得加LL
}
double Cross(Vector A, Vector B) { //叉积
    return A.x * B.y - A.y * B.x; //如果改成整形记得加LL
}
//向量长度
double Length(Vector A) {
    return sqrt(Dot(A, A));
}
//极角排序cmp函数
bool cmp(Point a, Point b) {
    int d = dcmp(Cross(a - po[0], b - po[0]));
    if (d > 0) return true;
    else if (d < 0) return false;
    else return Length(a - po[0]) < Length(b - po[0]);
}
int tot;
//求凸包,po存的就是结果,范围[0,tot)
void Graham(Point po[], Point p[]) {
    int k = 0;
    //找一个最小的点,详见看 <
    for (int i = 1; i < n; i++) {
        if (po[i] < po[k]) k = i;
    }
    swap(po[k], po[0]);
    sort(po + 1, po + n, cmp);//第一个元素不排序
    tot = 2;
    p[0] = po[0];
    p[1] = po[1];
    for (int i = 2; i < n; i++) {
        while (tot >0 && dcmp(Cross(p[tot-2] - p[tot - 1], po[i] - p[tot - 1])) >= 0) tot--; //旧边X新边>=0 tot--;
        p[tot++] = po[i];
    }
}
int main() {
    while (~scanf("%d", &n), n) {

        for (int i = 0; i < n; i++) {
            scanf("%lf%lf", &po[i].x, &po[i].y);
        }
        if (n == 1) printf("0.00\n");
        else if (n == 2) printf("%.2f\n", Length(po[1] - po[0]));
        else {
            Graham(po, p);
            p[tot]=p[0];
            double ans = 0.0;
            for (int i = 0; i < tot; i++) {
                ans += Length(p[i + 1] - p[i]);
            }
            printf("%.2f\n", ans);
        }
    }
    return 0;
}





方法二:水平排序–Andrew算法

#include <bits/stdc++.h>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)+1
#define CLR(x,y) memset((x),y,sizeof(x))
#define fuck(x) cerr << #x << "=" << x << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int seed = 131;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int n;
struct Point {
    double x, y;
    Point() {}
    Point(double x, double y): x(x), y(y) {}
};
Point po[maxn], p[maxn];
typedef Point Vector;
int dcmp(double x) { //返回x的正负
    if (fabs(x) < eps)return 0;
    return x < 0 ? -1 : 1;
}
Vector operator-(Vector A, Vector B) {
    return Vector(A.x - B.x, A.y - B.y);
}
Vector operator+(Vector A, Vector B) {
    return Vector(A.x + B.x, A.y + B.y);
}
Vector operator*(Vector A, double p) {
    return Vector(A.x * p, A.y * p);
}
Vector operator/(Vector A, double p) {
    return Vector(A.x / p, A.y / p);
}
bool operator<(const Point&a, const Point&b) {
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}
bool operator==(const Point&a, const Point&b) {
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
double Dot(Vector A, Vector B) { //点积
    return A.x * B.x + A.y * B.y; //如果改成整形记得加LL
}
double Cross(Vector A, Vector B) { //叉积
    return A.x * B.y - A.y * B.x; //如果改成整形记得加LL
}
//向量长度
double Length(Vector A) {
    return sqrt(Dot(A, A));
}
//求凸包,按水平坐标排序,返回值是凸包点的个数
int Andrew(Point *P, Point *R, int n) {
    int m = 0, k;
    sort(P, P + n);//内置重载<
    for (int i = 0; i < n; i++) {
        while (m > 1 && dcmp(Cross(R[m - 1] - R[m - 2], P[i] - R[m - 2])) <= 0) m--;
        R[m++] = P[i];
    }
    k = m;
    for (int i = n - 2; i >= 0; i--) {
        while (m > k && dcmp(Cross(R[m - 1] - R[m - 2], P[i] - R[m - 2])) <= 0) m--;
        R[m++] = P[i];
    }
    if (n > 1) m--;
    return m;
}

int main() {
    while (~scanf("%d", &n), n) {

        for (int i = 0; i < n; i++) {
            scanf("%lf%lf", &po[i].x, &po[i].y);
        }
        if (n == 1) printf("0.00\n");
        else if (n == 2) printf("%.2f\n", Length(po[1] - po[0]));
        else {
            int tot = Andrew(po, p, n);
            p[tot] = p[0];
            double ans = 0.0;
            for (int i = 0; i < tot; i++) {
                ans += Length(p[i + 1] - p[i]);
            }
            printf("%.2f\n", ans);
        }
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值