zoukankan      html  css  js  c++  java
  • POJ 2187: Beauty Contest(旋转卡)

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 27218   Accepted: 8410

    Description

    Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.

    Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms.

    Input

    * Line 1: A single integer, N

    * Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm

    Output

    * Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.

    Sample Input

    4
    0 0
    0 1
    1 1
    1 0
    

    Sample Output

    2
    

    Hint

    Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)


    题意如Hint。。这道题我是用凸包加枚举做的。。这竟然还是235ms....Orz。

    。。

    看别人是用旋转卡壳做的。。。

    也就找了些资料看看。。。。


    凸包+枚举:


    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<sstream>
    #include<cmath>
    
    using namespace std;
    
    #define f1(i, n) for(int i=0; i<n; i++)
    #define f2(i, m) for(int i=1; i<=m; i++)
    #define f3(i, n) for(int i=n; i>=0; i--)
    #define f4(i, n) for(int i=2; i<=n; i++)
    #define M 50050
    double ans;
    
    struct Point
    {
        double x, y;
    };
    
    bool cmp (Point a1, Point a2)
    {
        return ((a1.x > a2.x) || (a1.x==a2.x && a1.y>a2.y) );
    
    }
    
    int cross(int x1, int y1, int x2, int y2)
    {
        if(x1*y2-x2*y1<=0)
            return 0;
        else
            return 1;
    }
    
    double dis(Point a, Point b)
    {
        return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
    }
    
    int convexhull(Point *p, Point *c, int n)
    {
        int m = 0;
        f1(i, n)
        {
            while( m>1 && !cross(c[m-2].x-c[m-1].x, c[m-2].y-c[m-1].y, c[m-2].x-p[i].x, c[m-2].y-p[i].y) )
                m--;
            c[m++] = p[i];
        }
        int k = m;
        f3(i, n-2)
        {
            while( m>k && !cross(c[m-2].x-c[m-1].x, c[m-2].y-c[m-1].y, c[m-2].x-p[i].x, c[m-2].y-p[i].y) )
                m--;
            c[m++] = p[i];
        }
        if(n>1)
            m--;
        return m;
    }
    
    int main()
    {
        Point a[M], p[M];
        double sum;
        int n, r;
        while( cin>>n)
        {
            ans = -1.0;
            f1(i, n)
            scanf("%lf %lf", &a[i].x, &a[i].y);
            if(n==2)
                printf("%.lf
    ", dis( a[1], a[0] ));
            else
            {
                sort (a, a+n, cmp);
                int m = convexhull(a, p, n);
                f4(i, m)   //枚举全部的两点距离。。
                f2(j, m)
                ans = max(ans, dis( p[i], p[j] ) );
                    printf("%.lf
    ", ans);
            }
    
        }
        return 0;
    }
    

    由于我凸包用的不是扫描法。

    Orz。。。

    所以。

    。还是在研究研究。。。

    旋转卡壳的方法以及学习资料:

    传送门:

    点击打开链接

    感谢这位博主。。资料弄的非常好。。





    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    【每天学习一点点】Tensorflow 版本与CUDA版本
    【每天学习一点点】使用plot_model绘制网络模式失败
    【每天学习一点点】keras cifar10.load_data()自己下载数据
    【每天学习一点点】Tensorflow2.X 运行问题:Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED
    【每天学习一点点】mxnet 版本运行失败问题
    【每天学习一点点】Tensorflow GPU与CPU版本
    【每天学习一点点】不再显示I信息(Tensorflow GPU)
    C# Dynamic特性
    豆瓣,你的前端开发有点幽默了
    配置SHH集群
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4834420.html
Copyright © 2011-2022 走看看