【洛谷 P1452】 Beauty Contest (二维凸包,旋转卡壳)

本文深入探讨了旋转卡壳算法的实现细节,通过具体代码展示了如何处理点集以找到凸包。文章首先定义了必要的数学运算,如点乘和距离计算,然后详细解释了排序、构建上壳和下壳的过程,最后给出了完整的算法实现。

题目链接
旋转卡壳模板题把。
有时间再补总结吧。

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 100010;
struct point{
    int x, y;
}p[MAXN];
int operator * (point a, point b){    // a x b
    return a.x * b.y - b.x * a.y;
}
point operator - (point a, point b){
    return (point){ a.x - b.x, a.y - b.y };
}
int cmp1(const point a, const point b){
    return a.x == b.x ? a.y < b.y : a.x < b.x;
}
inline double k(point a, point b){
    return a.x == b.x ? 1e18 : ((double)b.y - a.y) / (b.x - a.x);
}
inline double dis(point a, point b){
    int px = b.x - a.x, py = b.y - a.y;
    return sqrt(px * px + py * py);
}
inline int dp(point a, point b){
    int px = b.x - a.x, py = b.y - a.y;
    return px * px + py * py;
}
int n, top, tp, ans;
point st[MAXN], ts[MAXN];
inline double mult(point a, point b, point c){
    return (a - c) * (b - c);
}
int SpecialJudge(){
    for(int i = 2; i <= n; ++i)
       if(p[i].y != p[i - 1].y)
         return 0;
    return 1;
}
int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
       scanf("%d%d", &p[i].x, &p[i].y);
    if(SpecialJudge()){
      int Min = 0x3f3f3f3f, Max = -0x3f3f3f3f;
      for(int i = 1; i <= n; ++i){
         Min = min(Min, p[i].x);
         Max = max(Max, p[i].x);
      }
      printf("%d\n", (Max - Min) * (Max - Min));
      return 0;
    }
    sort(p + 1, p + n + 1, cmp1);
    for(int i = 1; i <= n; ++i){
       while(top > 1 && k(st[top - 1], p[i]) < k(st[top - 1], st[top])) --top;
       st[++top] = p[i];
    }
    for(int i = 1; i <= n; ++i){
       while(tp > 1 && k(ts[tp - 1], p[i]) > k(ts[tp - 1], ts[tp])) --tp;
       ts[++tp] = p[i];
    }
    for(int i = tp - 1; i; --i) st[++top] = ts[i];
    int j = 2;
    for(int i = 1; i <= top; ++i){
       while(mult(st[i], st[i + 1], st[j]) < mult(st[i], st[i + 1], st[j + 1]))
         if(++j > top) j = 1;
       ans = max(ans, max(dp(st[i], st[j]), dp(st[i + 1], st[j])));
    }
    printf("%d\n", ans);
    return 0;
}

转载于:https://www.cnblogs.com/Qihoo360/p/10316656.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值