欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[POJ2187]Beauty Contest

程序员文章站 2022-03-30 09:03:38
...
Beauty Contest
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 40972   Accepted: 12678

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

题解:

其实这就是我之前一篇博客题目的英文版...不说了直接粘代码...原博客

AC代码:

#include<iostream>  
#include<cstdio>  
#include<algorithm>  
#include<cmath>  
#define ll long long
using namespace std;  
const int Maxn=100005;  
struct node{  
    ll x,y;  
}a[Maxn],ans[Maxn];  
ll n,k,lim,ansn;  
node operator -(const node &a,const node &b){  
    node c;  
    c.x=a.x-b.x;c.y=a.y-b.y;  
    return c;  
}  
inline ll chaji(node a,node b){
    return a.x*b.y-a.y*b.x;  
}
inline ll mx(ll x,ll y){
	return x>y?x:y;
}
inline ll dianji(node a,node b){
	return a.x*b.x+a.y*b.y;
}
inline ll dis(node a,node b){  
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);  
}   
bool cmp(node x,node y){  
    if(x.y!=y.y)return x.y<y.y;  
    return x.x<y.x;
}  
bool check(node o,node x,node y){  
    return chaji((x-o),(y-o))>0?0:1;  
}  
void convex(){  
    k=0;
    sort(a+1,a+1+n,cmp);  
    for(ll i=1;i<=n;i++){  
        while(k>=2&&check(ans[k-1],ans[k],a[i]))k--;
        ans[++k]=a[i];  
    }  
    lim=k;  
    for(ll i=n;i>=1;i--){
        while(k>=lim+1&&check(ans[k-1],ans[k],a[i]))k--;
        ans[++k]=a[i];  
    }  
    if(n>1)k--; 
}  
inline ll step(ll x){
	x=x+1;
	if(x>k)x=1;
	return x;
}
void rotate(){
	ll now=2,ansn=0;
	scanf("%lld",&n);
	for(ll i=1;i<=n;i++){
		scanf("%lld%lld",&a[i].x,&a[i].y);
	}
	convex();
	ans[k+1]=ans[1];
	for(ll i=1;i<=k;i++){
		while(chaji((ans[i+1]-ans[i]),(ans[now+1]-ans[i]))>chaji((ans[i+1]-ans[i]),(ans[now]-ans[i]))){now=step(now);}
		ansn=mx(ansn,mx(dis(ans[i],ans[now]),dis(ans[i+1],ans[now+1])));
	}
	printf("%lld\n",ansn);
}
int main(){  
	rotate();
    return 0;  
}