zoukankan      html  css  js  c++  java
  • [POJ 2187] Beauty Contest

    Beauty Contest
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 28659   Accepted: 8898

    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

    涨知识了、旋转卡(qia)壳
    前面写凸包都是从1开始的、这里改为从0开始、方便一点
    附暴力程序、或许是因为数据弱了、所以旋转卡壳算法再本题的时间上没有太大优化。

    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    using namespace std;
    #define N 100010
    
    struct Point
    {
        int x,y;
        Point(){}
        Point (int x,int y):x(x),y(y){
        }
        Point operator -(Point p){
            return Point(x-p.x,y-p.y);
        }
        int operator * (Point p){
            return x*p.x+y*p.y;
        }
        int operator ^ (Point p){
            return x*p.y-y*p.x;
        }
        bool operator < (const Point &p)const
        {
            if(x!=p.x) return x<p.x;
            return y<p.y;
        }
    };
    
    int n;
    int top;
    Point p[N],q[N];
    
    void Graham()
    {
        int i;
        sort(p,p+n);
        top=0;
        for(i=0;i<n;i++)
        {
            while(top>1 && ((q[top-1]-q[top-2])^(p[i]-q[top-2]))<=0)
                top--;
            q[top++]=p[i];
        }
        int t=top;
        for(i=n-1;i>=0;i--)
        {
            while(top>t && ((q[top-1]-q[top-2])^(p[i]-q[top-2]))<=0)
                top--;
            q[top++]=p[i];
        }
        top-=1;
    }
    int dis(Point a,Point b)
    {
        return (a-b)*(a-b);
    }
    void solve()
    {
        int i,j,ans=-1;
        for(i=0;i<top-1;i++)
        {
            for(j=i+1;j<top;j++)
            {
                ans=max(ans,dis(q[i],q[j]));
            }
        }
        cout<<ans<<endl;
    }
    void Rotate_Caliper()
    {
        Point v;
        int ans=0,cur=1;
        for(int i=0;i<top;i++)
        {
            v=q[i]-q[(i+1)%top];
            while((v^(q[(cur+1)%top]-q[cur]))<0)
                cur=(cur+1)%top;
            ans=max(ans,max(dis(q[i],q[cur]),dis(q[(i+1)%top],q[(cur+1)%top])));
        }
        cout<<ans<<endl;
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=0;i<n;i++)
            {
                scanf("%d%d",&p[i].x,&p[i].y);
            }
            Graham();
            //暴力:solve();
            Rotate_Caliper();
        }
        return 0;
    }
    趁着还有梦想、将AC进行到底~~~by 452181625
  • 相关阅读:
    C# 实现简单打印(二)打印一个文本文档,打印的内容是多行的
    用户管理:登录窗体通过ShowDialog()方法实现切换
    SQL 定义与使用数据库及表 实例_(学生,课程表,选修表)
    temp0305
    计算机硬件通用功能类:硬件信息控制器(主机名,cpu编号,网卡地址,MAC地址,主硬盘编号,ip地址,获取最大线程数,验证服务IP)
    socket编程:简单的TCP服务器
    从输入的邮箱地址中提取用户名
    C#基础:helloWord book 实例小集合
    怎么样datatable表中增加一行合计行?
    C#基础:多态:基类可以定义并实现虚(virtual)方法,派生类可以重写(override)这些方法
  • 原文地址:https://www.cnblogs.com/hate13/p/4149055.html
Copyright © 2011-2022 走看看