[hoj 2742]The Shape of Donggua[凸包][Graham Scan][旋转卡壳]

所谓凸包(这里说的是平面点集的凸包),就是一个最小的凸多边形,把所有给定点包含在内。显然,凸包一定是由给定的点中的某一些点构成。


主要有两种算法.

Graham Scan

首先是将点有序化, 有两种方式:

极角序 Vs 水平序

极角序是选定一个点,计算其它点所确定的射线的极角, 按极角从小到大排序(逆时针). 极角相同时, 按照距离由小到大排列. 重合点直接去除.

缺点是精度问题.

选择原点和第一个点入栈.

遍历剩下的每一个点 P. 设栈顶点为A, 次顶点为B, 则:

1. 若 PA 在 AB 的左侧, 则将 P 入栈.

2. 若 PA 在 AB 的右侧, 则将 B 出栈, 跳回 1 继续判断.

3. 若 PA 与 AB 共线, 则视题意而定, B点在凸多边形的边上.


水平序是将所有顶点按照 y 坐标优先 x 坐标次之的标准排序(可以避免精度问题).

将第一个点和第二个点入栈.

遍历剩下的每一个点 P. 设栈顶点为A, 次顶点为B, 则:

1. 若 PA 在 AB 的左侧, 则将 P 入栈.

2. 若 PA 在 AB 的右侧, 则将 B 出栈, 跳回 1 继续判断.

从0到maxn点扫描一遍,再从最高点倒序进行一次(不包括右链上的点)扫描.


题意:

算出一些点的凸包并求长轴,短轴长.(题中具体定义了)

思路:

凸包+旋转卡壳.

旋转卡壳是在凸包的基础上, 在O(n)时间模拟两直线夹着凸包旋转一圈的过程。
可以解决凸包上的最远点对等问题。

for(int i=0;i<n-1;i++)
      while(cross(c[p],c[i],c[i+1])<cross(c[(p+1)%n],c[i],c[i+1]))
            p=(p+1)%top;
整个过程就是枚举凸包上的边,找到离它最远的点(用叉积判断)。

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-8;
const double L = 0.618 - 0.1;
const double R = 0.618 + 0.1;
const int MAXN = 10005;
struct point
{
    double x,y;
}res[MAXN],p[MAXN];
int top,n;
/*cross product*/
double cross(point a, point b, point o)//OA X OB
{
    return (a.x - o.x)*(b.y - o.y) - (b.x - o.x)*(a.y - o.y);
}
/*square sum*/
double sqsm(point a, point b)
{
    return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
}
bool cmp(point a, point b)
{
    if(a.x - b.x > EPS || b.x - a.x > EPS)  return a.x < b.x;
    return a.y < b.y;
}
void Graham()
{
    sort(p, p+n, cmp);
    res[0] = p[0];
    res[1] = p[1];
    top = 1;
    for(int i=2;i<n;i++)
    {
        while(top && cross(p[i], res[top-1], res[top])<=EPS)
            top--;
        res[++top] = p[i];
    }
    int mark = top;
    res[++top] = p[n-2];
    for(int i=n-3;i>=0;i--)
    {
        while(top>mark && cross(p[i], res[top-1], res[top])<=EPS)
            top--;
        res[++top] = p[i];
    }
}

int main()
{
    while(scanf("%d",&n)==1)
    {
        for(int i=0;i<n;i++)
            scanf("%lf %lf",&p[i].x,&p[i].y);
        Graham();
        /*querying point*/
        int q = 1;
        /*longest(product) shortest(product)*/
        double l = 0, s = 0;
        /*radius        cross product*/
        double r = INF, product = 0;
        for(int i=0;i<top;i++)//旋转卡壳
        {
            while(fabs(cross(res[i], res[i+1], res[q])) < fabs(cross(res[i], res[i+1], res[q+1])))
                q = q+1==top?0:q+1;
            l = max(l, max(sqsm(res[i], res[q]),sqsm(res[i+1], res[q+1])));
            product = fabs(cross(res[i], res[i+1], res[q]));
            s = product / sqrt(sqsm(res[i], res[i+1]));
            if(s < r)  r = s;
        }
        l = sqrt(l);
        /* quotient*/
        double t = r / l;
        if(L <= t && t <= R)    printf("Proper\n");
        else if(t < L)          printf("Thin\n");
        else                    printf("Fat\n");
    }
}

Melkman

To be continued...





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值