zoukankan      html  css  js  c++  java
  • 求凸包上最大距离 poj2187

    Beauty Contest
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 38389   Accepted: 11866

    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

    USACO 2003 Fall

        就是求最大距离。。先求出凸包,这里的排序方法和我写的上一篇不太一样,这里是根据最左下角已确定的点与之叉乘是否大于零若共线则优先排距离小的,留个坑,我的再想想为啥

       之后求每一对对踵点间最大距离,(其实是每个点都找一遍,利用三角形面积,等底,求最大高)稍优化的暴力。。

      

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<vector>
    #include<iostream>
    #define N 50005
    using namespace std;
    struct node
    {
    	int x,y;
    }a[N],point;
    int cheng(node a,node b,node c)
    {
    	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
    }
    int dis(node a,node b)
    {
    	return (b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y);
    }
    int cmp(node b,node c)
    {
    	int t=cheng(a[1],b,c);
    	if(t==0)
    	   return dis(a[1],b)<dis(a[1],c);
    	return t>0;
    }
    int n,vis[N],head=-1,zhan[N],ans;
    int main()
    {
    	scanf("%d",&n);
    	point.x=10005,point.y=10005;int j;
    	for(int i=1;i<=n;i++)
    	{
    		scanf("%d%d",&a[i].x,&a[i].y);
    	    if(a[i].y<point.y)
    	         point=a[i],j=i;
    	    else
    	       if(a[i].y==point.y&&a[i].x<point.x)
    	            point=a[i],j=i;
    	}
    	if(n==2)
    	{
    		printf("%d
    ",dis(a[1],a[2]));
    		return 0;
    	}
    	a[j]=a[1];a[1]=point;
    	sort(a+2,a+n+1,cmp);
    	zhan[++head]=1;zhan[++head]=2;
    	for(int i=3;i<=n;i++)
    	{
    	    while(head>0&&cheng(a[zhan[head-1]],a[zhan[head]],a[i])<=0)head--;
    	    zhan[++head]=i;	
    	}
    	j=1;zhan[++head]=1;
    	for(int i=0;i<head;i++)
    	{
    		while(cheng(a[zhan[i]],a[zhan[i+1]],a[zhan[j+1]])>cheng(a[zhan[i]],a[zhan[i+1]],a[zhan[j]]))
    		   j=(j+1)%head;
    		ans=max(ans,dis(a[zhan[i]],a[zhan[j]]));
    	}
    	printf("%d
    ",ans);
    }

  • 相关阅读:
    【华为云技术分享】使用keil5打开GD32F450i的MDK项目出现的问题以及J-Link无法烧录程序对应的解决方案
    【华为云技术分享】不为人知的稠密特征加入CTR预估模型的方法
    205. 判断两个字符串的模式是否相同 Isomorphic Strings
    541. 反转字符串2 Reverse String II
    插入排序,二分查找插入排序,使用二叉树的插入排序
    二分查找,求mid值的类型溢出问题
    二叉搜索树类的C#实现
    二叉搜索树,删除节点
    二叉搜索树的前驱节点和后继节点
    438. 匹配字符串(顺序不同但个数相同的字符串) Find All Anagrams in a String
  • 原文地址:https://www.cnblogs.com/QTY2001/p/7632742.html
Copyright © 2011-2022 走看看