zoukankan      html  css  js  c++  java
  • POJ 2187 Beauty Contest(凸包,旋转卡壳)

    #include <iostream>
    #include <cstdio>
    #include"cmath"
    #include <algorithm>
    using namespace std;
    const int N=5e4+10;
    struct point{
        int x,y;
    }p[N];
    int dis(point a,point b){
        return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    }
    int cross(point p0,point p1,point p2){
        return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
    }
    int cmp1(point p1,point p2){
        return p1.x<p2.x||(p1.x==p2.x&&p1.y<p2.y);
    }
    int cmp2(point p1,point p2){
    
    
        int c=cross(p[0],p1,p2);
        if(c==0) return p1.x<p2.x;
        return c>0;
    }
    point sta[N];
    int top;
    void convex(int n){
        top=0;
        sort(p,p+n,cmp1);
        sort(p+1,p+n,cmp2);
        sta[top++]=p[0];
        sta[top++]=p[1];
    
        for(int i=2;i<n;){
    
            if(top>1&&cross(sta[top-2],p[i],sta[top-1])>=0)top--;
            else {
                sta[top++]=p[i++];
            }
    
        }
    }
    
    int rotating()
    {
        int q=1,ans=0;
        sta[top]=sta[0];
        for(int i=0;i<top;i++)
        {
            while(cross(sta[i],sta[i+1],sta[(q+1)%top])>cross(sta[i],sta[i+1],sta[q]))
                q=(q+1)%top;
    
            ans=max(ans,max(dis(sta[i],sta[q]),dis(sta[i+1],sta[q+1])));
        }
        return ans;
    }
    
    int main()
    {
        //freopen("cin.txt","r",stdin);
        int n;
        while(cin>>n){
            for(int i=0;i<n;i++){
                scanf("%d%d",&p[i].x,&p[i].y);
            }
            convex(n);
            printf("%d
    ",rotating());
        }
        return 0;
    }
    

      

    题面
    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)

    题解

    SOLUTION:


    题目大意:给出若干个农场的坐标,求出相距最远的农场的直线距离的平方。

    首先扫描法求出凸包,旋转卡壳求出最大值即可

    CODE:

  • 相关阅读:
    最大子串和
    [USACO1.5]数字金字塔 Number Triangles
    数字金字塔
    台阶问题
    取余运算
    数列分段pascal程序
    Java 集合-Collection接口和迭代器的实现
    Java 集合-集合介绍
    Java IO流-File类
    Git学习记录
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11430636.html
Copyright © 2011-2022 走看看