POJ训练计划2187_Beauty Contest(几何/凸包)

来源:互联网 发布:centos无法挂载ntfs 编辑:程序博客网 时间:2024/05/22 22:36

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 26846 Accepted: 8283

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

40 00 11 11 0

Sample Output

2

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 

Source

USACO 2003 Fall


解题报告

刚学凸包,看着大白书理解。。。

凸包就是把给定包围在内部的,面积最小的凸多边形。

把所有的点按照x从小到大排序(如果x相同,按照y从小到大排序),删除重复点得到序列p1,p2...然后把p1,p2放进凸包中。从p3开始,当新的点在凸包“前进”方向左边时继续加入凸包,否则依次删除最近加入的点,直到新点在左边。

先做“下凸包”,然后反过来从pn开始在做一次,求“上凸包”,合并起来就是完整的凸包。

至于判断点在线段的左右边就利用叉积判断,两向量OA和OB的叉积等于XaYb-XbYa。

题目要求凸包的最长直径平方。。。

我是用凸包加n2暴力做的,看网上还有更快的方法,没学呢。。。

#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;struct point{    double x,y;}p[50000+10],s[50000+10];int cmp(point a,point b){    if(a.x!=b.x)        return a.x<b.x;    else        return a.y<b.y;}double Cross(point a,point b,point c){    return (b.y-a.y)*(c.x-a.x)-(b.x-a.x)*(c.y-a.y);}double dis(point a,point b){    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}int main(){    int n,i,m;    while(cin>>n)    {        for(i=0;i<n;i++)        {            cin>>p[i].x>>p[i].y;        }        sort(p,p+n,cmp);        m=0;        for(i=1;i<n;i++)        {            while(m>1&&Cross(s[m-2],s[m-1],p[i])>=0)m--;            s[m++]=p[i];        }        int k=m;        for(i=n-2;i>=0;i--)        {            while(m>k&&Cross(s[m-2],s[m-1],p[i])>=0)m--;            s[m++]=p[i];        }        double ans=0;        for(i=0;i<m;i++)        {            for(int j=0;j<m;j++)            {                double t=dis(s[i],s[j]);                if(t>ans)                ans=t;            }        }        printf("%.lf\n",ans);    }    return 0;}


0 0