• POJ 2187 Beauty Contest( 凸包求最远点对 )



    **链接:****传送门 **

    题意:给出 n 个点,求出这 n 个点中最远的两个点距离的平方

    思路:最远点对一定会在凸包的顶点上,然后直接暴力找一下凸包顶点中距离最远的两个点


    /*************************************************************************
        > File Name: poj2187.cpp
        > Author:    WArobot 
        > Blog:      http://www.cnblogs.com/WArobot/ 
        > Created Time: 2017年05月08日 星期一 14时30分43秒
     ************************************************************************/
    
    #include<iostream>
    #include<cmath>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    #define eps 1e-10
    const int maxn = 50010;
    struct point{ double x,y; };
    
    double multi(point sp,point ep,point op){
    	return (sp.x-op.x)*(ep.y-op.y) >= (sp.y-op.y)*(ep.x-op.x);
    }
    bool operator < (const point &a,const point &b){
    	return a.y < b.y || ( a.y == b.y && a.x < b.x );
    }
    int Graham(point pnt[] , int n , point res[]){
    	int i , len , k = 0 , top = 1;
    	sort(pnt,pnt+n);
    	if( n == 0 ) return 0;	res[0] = pnt[0];
    	if( n == 1 ) return 1;	res[1] = pnt[1];
    	if( n == 2 ) return 2;	res[2] = pnt[2];
    	for(int i = 2 ; i < n ; i++){
    		while( top && multi( pnt[i] , res[top] , res[top-1] ))
    			top--;
    		res[++top] = pnt[i];
    	}
    	len = top;	res[++top] = pnt[n-2];
    	for(int i = n - 3 ; i >= 0 ; i--){
    		while( top!=len && multi( pnt[i] , res[top] , res[top-1] ))
    			top--;
    		res[++top] = pnt[i];
    	}
    	return top;
    }
    int Dist(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;
    	while(~scanf("%d",&n)){
    		point pnt[maxn] , res[maxn];
    		for(int i = 0 ; i < n ; i++)	scanf("%lf%lf",&pnt[i].x,&pnt[i].y);
    		int num = Graham( pnt , n , res );
    		int dis = 0;
    		for(int i = 0 ; i < num ; i++){
    			for(int j = i+1 ; j < num ; j++){
    				dis = max( dis , Dist( res[i] , res[j] ) );
    			}
    		}
    		printf("%d
    ",dis);
    	}
    	return 0;
    }
  • 相关阅读:
    python 软件目录规范
    生成器与迭代器
    第四周-第08章节-Python3.5-装饰器
    第三周-第18章节-Python3.5-函数式编程与函数不同
    第三周-第17章节-Python3.5-递归
    第三周-第16章节-Python3.5-局部变量与全局变量作用域
    第三周-第14章节-Python3.5-函数式编程
    JAVA发红包案例
    JAVA字符串
    JAVA关于字符串&&字符数组处理的小题目
  • 原文地址:https://www.cnblogs.com/WArobot/p/6825094.html
Copyright © 2020-2023  润新知