zoukankan      html  css  js  c++  java
  • POJ 2187 Beauty Contest 凸包

    Beauty Contest
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 27276   Accepted: 8432

    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
    


    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    
    #define M 50005
    struct node
    {
    	double x,y;
    }A[M],B[M];
    
    double cmp(node a,node b)      //先按X排序,其次按Y排序
    {
    	if(a.x != b.x)
    		return a.x < b.x;
    	else
    		return a.y < b.y;
    }
    
    double dis(node a,node b)    //计算两点之间的距离
    {
    	return (b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y);
    }
    
    double chaji(node a,node b,node c)      //叉积,推断方向
    {
    	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
    }
    
    int tubao(int n)
    {
    	sort(A,A+n,cmp);
    	int m=0,i;
    	for(i=0;i<n;i++)      //构建下凸包边
    	{
    		while(m>1 && chaji(B[m-2],B[m-1],A[i]) < 0)
    			m--;
    		B[m++]=A[i];
    	}
    
    	int k=m;
    	for(i=n-2;i>=0;i--)     //构建上凸包边
    	{
    		while(m>k && chaji(B[m-2],B[m-1],A[i]) < 0)
    			m--;
    		B[m++]=A[i];
    	}
    
    	if(n>1)  m--;
    	return m;
    }
    
    int main()
    {
    	int n;
    	cin>>n;
    	int i,j;
    	for(i=0;i<n;i++)
    		cin>>A[i].x>>A[i].y;
    
    	double p=tubao(n);
    	__int64 max=0,q;
    	for(i=0;i<p;i++)
    		for(j=i+1;j<p;j++)
    		{
    			q=dis(B[i],B[j]);
    			if(max<q)   max=q;
    		}
    	printf("%I64d
    ",max);
    
    	return 0;
    }
    


  • 相关阅读:
    Web开发人员必备工具-Emmet (Zen Coding)
    Zerver是一个C#开发的Nginx+PHP+Mysql+memcached+redis绿色集成开发环境
    jquery 插件站
    数据挖掘项目之---通过对web日志的挖掘来实现内容推荐系统
    类似wordpress的网站模板
    浅谈 SOAP
    在Livemedia的基础上开发自己的流媒体客户端 V 0.01
    模式识别之分类器knn---c语言实现带训练数据---反余弦匹配
    多项式相乘快速算法原理及相应C代码实现---用到fft
    模式识别之基础---常用分类算法特性归纳
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5185644.html
Copyright © 2011-2022 走看看