Gym - 101635K:Blowing Candles (旋转卡壳,求凸包宽度)

题目:传送门

题意:给 n 个点,用矩阵将所有点覆盖,要求矩形宽度最小,输出宽度。

 

 

 

 

 

思路: 

参考自 ->

旋转卡壳 + 点到直线最短距离

最远距离是点到点;宽度是点到边。

 

#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 = 5e5 + 5;
 
struct Point {
    LL x, y;
    Point(LL x = 0, LL 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;
}
 
LL Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } /// 点积
double Length(Vector A) { return sqrt(1.0 * Dot(A, A)); } /// 计算向量长度
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角
LL 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("%.16f\n", Length(Q[1] - Q[0]));
        return ;
    }
//    cout << n << endl;
    double ans=123456789009876; int now = 1;
    rep(i, 0, n - 2){
        while(abs(Cross(Q[i + 1] - Q[i], Q[now] - Q[i])) < abs(Cross(Q[i + 1] - Q[i], Q[now + 1] - Q[i]))){
            now++; if(now == n - 1) now = 0;
        }
        double tmp=fabs(1.0 * Cross(Q[i + 1] - Q[i], Q[now] - Q[i]));
        tmp /= Length(Q[i] - Q[i + 1]);
        ans=min(ans,tmp);
    }
    printf("%.16f\n", ans);
}
 
int main() {
    int n;
    double r;
    scanf("%d %lf", &n, &r);
    rep(i, 0, n - 1) scanf("%lld %lld", &P[i].x, &P[i].y);
    if(n == 2) {
        printf("%.16f\n", Length(P[0] - P[1]));
    }
    else {
        n = ConvexHull(P, n, Q);
        Q[n++] = Q[0]; /// 免得取模
        GetMax(n);
    }
    return 0;
}

 

posted on 2020-03-14 15:30  Willems  阅读(206)  评论(0编辑  收藏  举报

导航