zoukankan      html  css  js  c++  java
  • POJ

    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) 

    题意:给定二维平面上N个点,输出最远距离的平方。

    思路:先求出凸包,只考虑凸包上的点。利用旋转卡壳求最远距离。模板达成。

    #include<bits/stdc++.h>
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    #define ll long long
    #define RC rotating_calipers
    using namespace std;
    const int maxn=100010;
    struct point{
        double x,y;
        point(double x=0,double y=0):x(x),y(y){}
        bool operator < (const point &c) const { return x<c.x||(x==c.x&&y<c.y);}
        point operator - (const point &c) const { return point(x-c.x,y-c.y);}
        double operator * (const point &c) const { return x*c.y-y*c.x; }
        double operator | (const point &c) const { return (x-c.x)*(x-c.x)+(y-c.y)*(y-c.y); }
    };
    double det(point A,point B){ return A.x*B.y-A.y*B.x;}
    double det(point O,point A,point B){ return det(A-O,B-O);}
    point a[maxn],ch[maxn];
    void convexhull(int n,int &top)
    {
        sort(a+1,a+n+1); top=0;
        for(int i=1;i<=n;i++){
            while(top>1&&det(ch[top-1],ch[top],a[i])<=0) top--;
            ch[++top]=a[i];
        }
        int ttop=top;
        for(int i=n-1;i>=1;i--){
            while(top>ttop&&det(ch[top-1],ch[top],a[i])<=0) top--;
            ch[++top]=a[i];
        }
    }
    double rotating_calipers(point p[],int top)
    {
        double ans=0; int now=2;
        rep(i,1,top-1){
            while(det(p[i],p[i+1],p[now])<det(p[i],p[i+1],p[now+1])){
                now++; //最远距离对应了最大面积。
                if(now==top) now=1;
            }
            ans=max(ans,(p[now]|p[i]));
        }
        return ans;
    }
    int main()
    {
        int N; scanf("%d",&N);
        for(int i=1;i<=N;i++)
          scanf("%lf%lf",&a[i].x,&a[i].y);
        int top; convexhull(N,top);
        printf("%lld",(ll)RC(ch,top));
        return 0;
    }
  • 相关阅读:
    庄家试盘的K线形态
    股票基本知识入门提纲
    我与猫
    夜雨不眠时
    快速排序
    由float转std::string的方法
    BugFree + EasyPHP在Windows平台搭建步骤详解
    安装VS2008的时候Windows Mobile 5.0 SDK R2 for pocket pc错误解决方案
    收集WCF文章
    linq to ef(相当于sql中in的用法)查询语句
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9619998.html
Copyright © 2011-2022 走看看