zoukankan      html  css  js  c++  java
  • poj2187 Beauty Contest

    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)

    分析:凸包

    tip

    在计算TUB的时候,时刻注意精度(只要有小数比较,就一定要用dcmp)

    这道题可以用旋转卡壳,但是因为点不多,
    所以n^2枚举也是可以的

    旋转卡壳我会专门学习介绍的

    这里写代码片
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    
    using namespace std;
    
    const double eps=1e-8;
    const int N=50010;
    struct node{
        double x,y;
        node (double xx=0,double yy=0)
        {
            x=xx;y=yy;
        }
    };
    node po[N];
    int n,sta[N],top=0;
    
    node operator +(const node &a,const node &b){return node(a.x+b.x,a.y+b.y);}
    node operator -(const node &a,const node &b){return node(a.x-b.x,a.y-b.y);}
    node operator *(const node &a,const double &b){return node(a.x*b,a.y*b);}
    node operator /(const node &a,const double &b){return node(a.x/b,a.y/b);}
    
    int dcmp(double x)
    {
        if (fabs(x)<eps) return 0;
        else if (x>0) return 1;
        else return -1;
    }
    
    int cmp(const node &a,const node &b)
    {
        if (dcmp(a.x-b.x)!=0) return a.x<b.x;
        else return a.y<b.y;
    }
    
    double Dot(node x,node y){return x.x*y.x+x.y*y.y;}
    double Cross(node x,node y){return x.x*y.y-x.y*y.x;}
    
    void TuB()
    {
        top=0;
        sort(po+1,po+1+n,cmp);
        for (int i=1;i<=n;i++)
        {
            while (top>1&&dcmp(Cross(po[i]-po[sta[top-1]],po[sta[top]]-po[sta[top-1]]))<=0) top--;  //上 
            sta[++top]=i;
        }
        int k=top;
        for (int i=n-1;i>=1;i--)
        {
            while (top>k&&dcmp(Cross(po[i]-po[sta[top-1]],po[sta[top]]-po[sta[top-1]]))<=0) top--;
            sta[++top]=i;
        }
        if (n>1) top--;   //n>1
    }
    
    int ds(node x,node y)
    {
        int xx=(int)(x.x-y.x)*(x.x-y.x);
        int yy=(int)(x.y-y.y)*(x.y-y.y);
        return (int)(x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y);
    }
    
    int main()
    {
        while (scanf("%d",&n)!=EOF)
        {
            for (int i=1;i<=n;i++)
                scanf("%lf%lf",&po[i].x,&po[i].y);
            TuB();
            int ans=0;
            for (int i=1;i<top;i++)
                for (int j=i+1;j<=top;j++)
                    ans=max(ans,ds(po[sta[i]],po[sta[j]])); 
            printf("%d",ans);
        }
        return 0;
    }
  • 相关阅读:
    读Hadoop3.2源码,深入了解java调用HDFS的常用操作和HDFS原理
    AI学习笔记:人工智能与机器学习概述
    CDN百科 | 最近,你的APP崩了吗?
    CDN HTTPS安全加速基本概念、解决方案及优化实践
    CDN百科第六讲 | 怎样用CDN抵御攻击?看完这篇漫画你就懂了
    CDN百科第五讲 | CDN和游戏加速器有什么区别?
    CDN百科第四讲 | 如何优雅地在云上“摆摊”——做直播带货,你不得不关注的技术
    CDN百科第三讲 | 如果用了云服务器,还需要做CDN加速吗?
    阿里云杨敬宇:边缘计算行业通识与阿里云ENS的技术演进之路
    CDN百科 | 假如没有CDN,网络世界会变成什么样?
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673431.html
Copyright © 2011-2022 走看看