POJ 2187 Beauty Contest (凸包 旋转卡壳 平面最远点对 凸包直径)

题目: 传送门

题意: 给你 n 个点, 问你这些点中, 最远的两个点的距离的平方是多少。

 

题解: 挑战 261 有细讲, 求凸包 + 旋转卡(qia)壳。

       旋转卡壳详解

 

码一:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 5e4 + 5;

struct Point {
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) { } /// 构造函数
};

typedef Point Vector;
/// 向量+向量=向量, 点+向量=向量
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
///点-点=向量
Vector operator - (Point A, Point 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); }

const double eps = 1e-8;
int dcmp(double x) {
    if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}

bool operator < (const Point& a, const Point& b) {
    return a.x == b.x ? a.y < b.y : a.x < b.x;
}

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; } /// 点积
double Length(Vector A) { return sqrt(Dot(A, A)); } /// 计算向量长度
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积


Point P[N], Q[N];

int ConvexHull(Point* p, int n, Point* ch) {
    sort(p, p + n);
    int m = 0;
    rep(i, 0, n - 1) {
        while(m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
        ch[m++] = p[i];
    }
    int k = m;
    dep(i, 0, n - 2) {
        while(m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
        ch[m++] = p[i];
    }
    if(n > 1) m--;
    return m;
}

void GetMax(int n) {
    if(n == 2) {
        printf("%.0f\n", Dot(Q[1] - Q[0], Q[1] - Q[0]));
        return ;
    }

    double res = 0;

    int cur = 1;
    rep(i, 0, n - 1) {
        while(dcmp(Cross(Q[(i + 1) % n] - Q[i], Q[cur] - Q[i]) - Cross(Q[(i + 1) % n] - Q[i], Q[(cur +1) % n] - Q[i])) < 0) cur = (cur + 1) % n;
        res = max(res, max(Dot(Q[cur] - Q[i], Q[cur] - Q[i]), Dot(Q[cur] - Q[(i + 1) % n], Q[cur] - Q[(i + 1) % n])));
    }

    printf("%.0f\n", res);
}

int main() {
    int n;
    scanf("%d", &n);
    rep(i, 0, n - 1) scanf("%lf %lf", &P[i].x, &P[i].y);
    if(n == 2) {
        printf("%.0f\n", Dot(P[0] - P[1], P[0] - P[1]));
    }
    else {
        n = ConvexHull(P, n, Q);
        GetMax(n);
    }
    return 0;
}

 

 

码二:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 5e4 + 5;

struct Point {
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) { } /// 构造函数
};

typedef Point Vector;
/// 向量+向量=向量, 点+向量=向量
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
///点-点=向量
Vector operator - (Point A, Point 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); }

const double eps = 1e-10;
int dcmp(double x) {
    if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}

bool operator < (const Point& a, const Point& b) {
    return a.x == b.x ? a.y < b.y : a.x < b.x;
}

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; } /// 点积
double Length(Vector A) { return sqrt(Dot(A, A)); } /// 计算向量长度
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积


Point P[N], Q[N];

int ConvexHull(Point* p, int n, Point* ch) {
    sort(p, p + n);
    int m = 0;
    rep(i, 0, n - 1) {
        while(m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
        ch[m++] = p[i];
    }
    int k = m;
    dep(i, 0, n - 2) {
        while(m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
        ch[m++] = p[i];
    }
    if(n > 1) m--;
    return m;
}

void GetMax(int n) {
    if(n == 2) {
        printf("%.0f\n", Dot(Q[1] - Q[0], Q[1] - Q[0]));
        return ;
    }
    int i = 0, j = 0;
    rep(k, 0, n - 1) {
        if(!(Q[i] < Q[k])) i = k;
        if(Q[j] < Q[k]) j = k;
    }
    double res = 0;
    int si = i, sj = j;
    while(i != sj || j != si) {
        res = max(res, Dot(Q[i] - Q[j], Q[i] - Q[j]));
        if(Cross(Q[(i + 1) % n] - Q[i], Q[(j + 1) % n] - Q[j]) < 0) {
            i = (i + 1) % n;
        }
        else {
            j = (j + 1) % n;
        }
    }
    printf("%.0f\n", res);
}

int main() {
    int n;
    scanf("%d", &n);
    rep(i, 0, n - 1) scanf("%lf %lf", &P[i].x, &P[i].y);
    if(n == 2) {
        printf("%.0f\n", Dot(P[0] - P[1], P[0] - P[1]));
    }
    else {
        n = ConvexHull(P, n, Q);
        GetMax(n);
    }
    return 0;
}

 

posted on 2020-02-22 13:28  Willems  阅读(145)  评论(0编辑  收藏  举报

导航