zoukankan      html  css  js  c++  java
  • poj2187

    题意:求凸包直径。

    分析:用graham按纵坐标序,再用旋转卡壳法。

    但是模板我并没有完全理解,为什么要用点到直线的距离呢

    View Code
    #include <iostream>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cstdio>
    #include
    <algorithm>
    usingnamespace std;

    #define maxn 50005

    struct Xpoint
    {
    int x, y;
    } pnt[maxn], res[maxn];

    int n;

    bool mult(Xpoint sp, Xpoint ep, Xpoint op)
    {
    return (sp.x - op.x) * (ep. y - op.y) >= (ep.x - op.x) * (sp.y - op.y);
    }

    booloperator<(const Xpoint &l, const Xpoint &r)
    {
    return l.y < r. y || (l.y == r.y && l.x < r.x);
    }

    int cross(Xpoint a, Xpoint b, Xpoint o)
    {
    return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);
    }

    int graham()
    {
    int i, len, top =1;
    sort(pnt, pnt
    + n);
    if (n ==0)
    return0;
    res[
    0] = pnt[0];
    if (n ==1)
    return1;
    res[
    1] = pnt[1];
    if (n ==2)
    return2;
    res[
    2] = pnt[2];
    for (i =2; i < n; i++)
    {
    while (top && mult(pnt[i], res[top], res[top -1]))
    top
    --;
    res[
    ++top] = pnt[i];
    }
    len
    = top;
    res[
    ++top] = pnt[n -2];
    for (i = n -3; i >=0; i--)
    {
    while (top != len && mult(pnt[i], res[top], res[top -1]))
    top
    --;
    res[
    ++top] = pnt[i];
    }
    return top;
    }

    int dist2(Xpoint a, Xpoint b)
    {
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
    }

    int rotating_calipers(Xpoint *ch, int n)
    {
    int q =1, ans =0;
    ch[n]
    = ch[0];
    for (int p =0; p < n; p++)
    {
    while (cross(ch[p +1], ch[q +1], ch[p]) > cross(ch[p +1], ch[q], ch[p]))
    q
    = (q +1) % n;
    ans
    = max(ans, max(dist2(ch[p], ch[q]), dist2(ch[p +1], ch[q +1])));
    }
    return ans;
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    scanf("%d", &n);
    for (int i =0; i < n; i++)
    scanf(
    "%d%d", &pnt[i].x, &pnt[i].y);
    int count = graham();
    int ans = rotating_calipers(res, count);
    printf(
    "%d\n", ans);
    return0;
    }
  • 相关阅读:
    <mvc:default-servlet-handler />说明
    sql server 数据库创建链接服务器访问另外一个sql server 数据库
    WebSocket实现简易聊天室
    WebSocket在建立连接时通过@PathParam获取页面传值
    Shiro密码处理
    Java enum应用小结
    Java8 Optional类使用小结
    Java中使用Jedis操作Redis
    前台图片Canvas压缩上传小结
    剪邮票
  • 原文地址:https://www.cnblogs.com/rainydays/p/2073170.html
Copyright © 2011-2022 走看看