Description
约翰农夫的母牛贝西,刚刚在牛选美比赛赢得第一名,赢得了“世界牛小姐”称号。为了传播善意,贝西将参观世界各地的农场 N(2 < = N < = 50000)。为简单起见,世界将被表示成一个二维平面,其中每个农场位于一对整数坐标(x,y),其值在-2000000~2000000 范围内。没有两个农场共享相同的坐标。
尽管贝西旅行中走农场之间的直线,但一些农场之间的距离可能很大,所以她想带着一个手提箱,足够她装食物,即使最远的距离。她想确定旅行中最大可能的距离,这样她才能决定箱子的大小。帮助贝西计算所有成对的农场间最大距离。(最远点对)
Input
第一行一个整数 N
以下 N 行,每行两个整数,表示一个农场的坐标。
Output
一行,一个整数表示最远一对农场距离的平方。
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output
2
题解
裸的最远点对。
旋xuan2转zhuan3卡qia3壳ke2即可。
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
inline int read(){
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
const int N = 50000 + 10;
struct P{
ll x, y;
P(){};
P(ll a, ll b){x = a, y = b;}
}p[N], s[N];
int n, top;
ll ans;
ll sqr(ll x){return x*x;}
ll dis(P a, P b){return sqr(a.x - b.x) + sqr(a.y - b.y);}
P operator - (P a, P b){
return P(a.x - b.x, a.y - b.y);
}
ll operator * (P a, P b){
return a.x * b.y - a.y * b.x;
}
bool operator < (P a, P b){
ll t = (a - p[1]) * (b - p[1]);
return t > 0 || (t == 0 && dis(p[1], a) < dis(p[1], b));
}
void init(){
n = read();
int k = 1;
for(int i = 1; i <= n; i++){
p[i].x = read(), p[i].y = read();
if(p[i].x < p[k].x || (p[i].x == p[k].x && p[i].y < p[k].y))
k = i;
}
swap(p[1], p[k]);
sort(p + 2, p + n + 1);
}
void graham(){
s[++top] = p[1]; s[++top] = p[2];
for(int i = 3; i <= n; i++){
while(top > 1 && (s[top] - s[top-1]) * (p[i] - s[top-1]) <= 0)
top--;
s[++top] = p[i];
}
}
void RC(){
s[top+1] = s[1];
int now = 2;
for(int i = 1; i <= top; i++){
while((s[i+1] - s[i]) * (s[now] - s[i]) < (s[i+1] - s[i]) * (s[now+1] - s[i])){
now++;
if(now == top + 1) now = 1;
}
ans = max(ans, dis(s[now], s[i]));
}
}
void work(){
graham();
RC();
cout<<ans<<endl;
}
int main(){
freopen("contest.in", "r", stdin);
freopen("contest.out", "w", stdout);
init();
work();
return 0;
}