旋转卡壳 x Beauty Contest

旋转卡qia壳ke好难理解啊。。其实是蒟蒻太菜了
本题不难看出是求凸包直径
旋转卡壳算法最基本的用法也是求凸包直径
我们可以把它当作用两根平行的小棍夹紧凸包
然后旋转凸包一周 小棍间最大的距离
对于一个凸包 按一般求法排序很容易能得到最左边的点和最右边的点 它们之间的距离就是让小棍和y轴平行时的间距
对于任意方向上使间距最大的点对叫对踵点对
现在考虑用这两个点推出下一组点
借用一个经典的图
旋转卡壳

很直观地能看出这个图中 点到底边的距离是一个单峰函数
而已求出点对的下一组点对 不是一端的点顺时针转一个 就是另一端的点转

另外这里要应用到一个小技巧
就是两向量叉积的模长 等于这两个向量坐标的点与原点围成的三角形面积
附上代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include <cmath>
using namespace std;
const int N = 5e4 + 5;
/*
凸包直径 
*/
struct Node{
    long long x, y;
}node[N], stk[N];
int top, n, target;
long long ans;
inline Node operator -(const Node& x, const Node& y){
    return (Node){x.x - y.x, x.y - y.y};
}
inline bool rule(const Node& x, const Node& y){
    return x.x == y.x ? x.y < y.y : x.x < y.x; 
} 
inline long long dis(const Node& x){
    return x.x * x.x + x.y * x.y;
}
inline long long cross(const Node& x, const Node& y){
    return x.x * y.y - x.y * y.x;
} 
inline void print(Node x){
 printf("%lld %lld\n", x.x, x.y);
}
inline void Andrew(){
    top = 0;
    for(int i = 1; i <= n; ++i){
        while(top > 1 && cross(stk[top] - stk[top - 1], node[i] - stk[top - 1]) >= 0) --top;
        stk[++top] = node[i];
    } 
    target = top;
    for(int i = n - 1; i >= 1; --i){
        while(top > target && cross(stk[top] - stk[top - 1], node[i] - stk[top - 1]) >= 0) --top;
        stk[++top] = node[i];
    }
    if(top > 1) --top;
    //非常非常重要!回来的时候会多算一个起点!
}
inline void rotating_caliper(){
 if(n == 2){
  ans = dis(node[1] - node[2]);
  return ;
 }
 if(n == 3){
  ans = max(max(dis(node[1] - node[2]), dis(node[2] - node[3])), dis(node[1] - node[3]));
  return ;
 }
    ans = -1;
    stk[0] = stk[top];
    stk[top + 1] = stk[1];
    target = 3;
    for(int i = 1; i <= top; ++i){
        while(abs(cross(stk[i] - stk[i + 1], stk[target] - stk[i + 1]))
         < abs(cross(stk[i] - stk[i + 1], stk[target + 1] - stk[i + 1]))){//按照小棒离开该点的时候算 
         //当然这里按小棒开始接触这点的时候开始算也可以 
         //就是把i + 1换成i - 1 一开始target从3换成2
            ++target; if(target >= top) target -= top;
   }
   if(dis(stk[i] - stk[target]) > ans){
    ans = dis(stk[i] - stk[target]);
   } 
    }
}
int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i){
        scanf("%lld%lld", &node[i].x, &node[i].y);
    }
    sort(node + 1, node + n + 1, rule);
    Andrew();
    rotating_caliper();
    printf("%lld", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/CH_Vaniteux/article/details/85881556
今日推荐