zoukankan      html  css  js  c++  java
  • poj 2187:Beauty Contest(计算几何,求凸包,最远点对)

    Beauty Contest
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 26180   Accepted: 8081

    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) 

    Source


     
      简单凸包应用。
      题意简单说就是求凸包的最长直径。给出N个点,要你求这N个点最长的距离平方值。
      看到这道题我们的直接思路应该是暴力枚举每个点对,求出最长距离,但由于这道题的数据规模是5w,直接枚举会超时。所以我们应该换一个思路,既然这道题要求最长距离,那么我们应该很容易想到凸包。因为凸包上的点一定是给定点集的最外围点,所以最长距离的两个点应该在凸包上。所以解题思路应该是:先求凸包,再枚举
      凸包模板
     1 struct Point{
     2     double x,y;
     3 };
     4 double dis(Point p1,Point p2)
     5 {
     6     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
     7 }
     8 double xmulti(Point p1,Point p2,Point p0)    //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
     9 {
    10     return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
    11 }
    12 void graham(Point p[],int n)    //点集和点的个数 
    13 {
    14     int pl[10005];
    15     //找到纵坐标(y)最小的那个点,作第一个点 
    16     int t = 1;
    17     for(int i=1;i<=n;i++)
    18         if(p[i].y < p[t].y)
    19             t = i;
    20     pl[1] = t;
    21     //顺时针找到凸包点的顺序,记录在 int pl[]
    22     int num = 1;    //凸包点的数量
    23     do{    //已确定凸包上num个点 
    24         num++; //该确定第 num+1 个点了
    25         t = pl[num-1]+1;
    26         if(t>n) t = 1;
    27         for(int i=1;i<=n;i++){    //核心代码。根据叉积确定凸包下一个点。 
    28             double x = xmulti(p[i],p[t],p[pl[num-1]]);
    29             if(x<0) t = i;
    30         }
    31         pl[num] = t;
    32     } while(pl[num]!=pl[1]);
    33 }

      本题代码

     1 #include <iostream>
     2 using namespace std;
     3 struct Point{
     4     int x,y;
     5 }p[50010];
     6 int xmulti(Point p1,Point p2,Point p0)    //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
     7 {
     8     return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
     9 }
    10 int graham(Point p[],int n)    //点集和点的个数
    11 {
    12     int pl[50005];
    13     //找到纵坐标(y)最小的那个点,作第一个点
    14     int t = 1;
    15     for(int i=1;i<=n;i++)
    16         if(p[i].y < p[t].y)
    17             t = i;
    18     pl[1] = t;
    19     //顺时针找到凸包点的顺序,记录在 int pl[]
    20     int num = 1;    //凸包点的数量
    21     do{    //已确定凸包上num个点
    22         num++; //该确定第 num+1 个点了
    23         t = pl[num-1]+1;
    24         if(t>n) t = 1;
    25         for(int i=1;i<=n;i++){    //核心代码。根据叉积确定凸包下一个点。
    26             int x = xmulti(p[i],p[t],p[pl[num-1]]);
    27             if(x<0) t = i;
    28         }
    29         pl[num] = t;
    30     } while(pl[num]!=pl[1]);
    31     //计算最长距离
    32     int Max = 0;
    33     for(int i=1;i<num-1;i++)
    34         for(int j=i+1;j<=num-1;j++){
    35             int tt = (p[pl[i]].x-p[pl[j]].x)*(p[pl[i]].x-p[pl[j]].x) + (p[pl[i]].y-p[pl[j]].y)*(p[pl[i]].y-p[pl[j]].y);
    36             //注意这里要用p[pl[i]],不能是p[i],否则会WA
    37             if(tt>Max)
    38                 Max = tt;
    39         }
    40     return Max;
    41 }
    42 int main()
    43 {
    44     int n;
    45     while(cin>>n){
    46         for(int i=1;i<=n;i++)   //输入n个点
    47             cin>>p[i].x>>p[i].y;
    48         cout<<graham(p,n)<<endl;    //输出最长距离
    49     }
    50     return 0;
    51 }

    类似题目:hdu 1348:Wall(计算几何,求凸包周长)

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    养成写随笔的习惯
    脚本附加数据库
    脚本还原数据库
    C# 自定义安装包
    怕忘记了。记录一下要采购的元件1
    计划没有变化快啊
    一天三练有点累啊
    nRF24L01无线模块使用1电平转换
    好几年没参加IC公司的研讨会了
    变化
  • 原文地址:https://www.cnblogs.com/yym2013/p/3621183.html
Copyright © 2011-2022 走看看