uvalive 4728(旋转卡壳求凸包最长直径)

题意:有n个正方形,给出了左下角坐标和边长,问在他们的顶点中距离最大的两个点的距离的平方。
题解:首先想到用凸包可以减少点的数量,然后两两枚举比较长度,这种方法也是O(n^2),所以有了一种新的方法叫做旋转卡(qia)壳,http://www.cnblogs.com/Booble/archive/2011/04/03/2004865.html,这里讲的比较好理解,就不总结了,其实是不会作图:-( 。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = acos(-1);
const int N = 400005;
const double INF = 1e9;
struct Point {
    double x, y;
    Point(double x = 0, double y = 0): x(x), y(y) {}
}P[N], res[N];
int n, m;
double Sqr(double x) {
    return x * x;
}
Point operator + (Point A, Point B) {
    return Point(A.x + B.x, A.y + B.y);
}
Point operator - (Point A, Point B) {
    return Point(A.x - B.x, A.y - B.y);
}
Point operator * (Point A, double p) {
    return Point(A.x * p, A.y * p);
}
Point operator / (Point A, double p) {
    return Point(A.x / p, A.y / p);
}
//计算点积的正负  负值夹角为钝角
int dcmp(double x) {
    if (fabs(x) < 1e-9)
        return 0;
    return x < 0 ? -1 : 1;
}
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(Point A, Point B) {
    return A.x * B.x + A.y * B.y;
}
//计算叉积,也就是数量积
double Cross(Point A, Point B) {
    return A.x * B.y - A.y * B.x;
}
//计算向量长度
double Length(Point A) {
    return sqrt(Dot(A, A));
}
//向量A旋转rad弧度,rad负值为顺时针旋转
Point Rotate(Point A, double rad) {
    return Point(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
//角度转化弧度
double torad(double deg) {
    return deg / 180.0 * PI;
}
//求凸包
int ConvexHull(Point* P, int cnt, Point* res) {
    sort(P, P + cnt);
    cnt = unique(P, P + cnt) - P;
    int m = 0;
    for (int i = 0; i < cnt; i++) {
        while (m > 1 && Cross(res[m - 1] - res[m - 2], P[i] - res[m - 2]) <= 0)
            m--;
        res[m++] = P[i];
    }
    int k = m;
    for (int i = cnt - 2; i >= 0; i--) {
        while (m > k && Cross(res[m - 1] - res[m - 2], P[i] - res[m - 2]) <= 0)
            m--;
        res[m++] = P[i];
    }
    if (cnt > 1)
        m--;
    return m;
}
//旋转卡壳求凸包最长直径
double Rotating_Calipers(Point* res, int cnt) {
    int q = 1;
    double ans = 0;
    res[cnt] = res[0];
    //枚举每一条边p[i] - p[i + 1],然后移动顶点q判断是否距离在增长,如果是就继续移动,否则就更新最大值并换下一条边,q可能会转好几圈所以求余cnt,判断q点到边的两个端点的距离用叉积替代,也就是看边与点围成的面积是否增大。
    for (int i = 0; i < cnt; i++) {
        while (Cross(res[q + 1] - res[i + 1], res[i] - res[i + 1]) > Cross(res[q] - res[i + 1], res[i] - res[i + 1]))
            q = (q + 1) % cnt;
        ans = max(ans, max(Length(res[i] - res[q]), Length(res[i + 1] - res[q + 1])));
    }
    return ans;
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        int temp = 0, cnt;
        double a, b, d;
        for (int i = 0; i < n; i++) {
            scanf("%lf%lf%lf", &a, &b, &d);
            P[temp++] = Point(a, b);
            P[temp++] = Point(a + d, b);
            P[temp++] = Point(a, b + d);
            P[temp++] = Point(a + d, b + d);
        }
        n = temp;
        cnt = ConvexHull(P, n, res);
        double ans = Rotating_Calipers(res, cnt);
        printf("%.0lf\n", ans * ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值