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。。。

    所以。

    。还是在研究研究。。。

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

    传送门:

    点击打开链接

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





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

  • 相关阅读:
    windows2012 永激活及配置
    Fiddler2 英文版翻译
    你知道using的用法吗?
    你会利用css写下拉列表框吗?
    完美解决.net2.0和.net4.0在同一个iis中共同运行
    深入剖析new override和virtual关键字
    思科防火墙,h3c三层交换机配置笔记
    c# 笔记 数据类型转换 数组 函数
    Silverlight 完美征程 笔记1 (控件模型)
    C#笔记(流程控制)
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4834420.html
Copyright © 2011-2022 走看看