题意:题目的意思就是说给你N个点,然后让你求出最远的两个点的距离的平方(注意:是平方);
分析:对于50000个点暴力肯定是不行的,那么我们可以联系到凸包,因为最远的点肯定是在凸包上,那么要求最远的
距离,我们就可以先对这些点求一次凸包(复习凸包的写法)。然后对于凸包上面的点可以枚举,因为这时候要枚举的
点的数目已经减少很多了,这时候,就是普通的暴搞了。主要是前面的凸包的写法。
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdio>
#define N 50005
using namespace std;
struct node{
int x, y;
bool friend operator < (const node &a, const node &b){
if(a.y == b.y)
return a.x < b.x;
return a.y < b.y;
}
}point[N];
double calcurateDis(node a, node b){
return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
}
bool judge(node a, node b, node c){
return (a.x - c.x)*(b.y - c.y) >= (b.x - c.x)*(a.y - c.y);
}
double solve(int n){
vector<node> tempPoint(n*2);
sort(point, point + n);
if(n == 1){
return 0;
}
if(n == 2){
return calcurateDis(point[0], point[1]);
}
tempPoint[0] = point[0];
tempPoint[1] = point[1];
tempPoint[2] = point[2];
int top = 1;
for(int i = 2; i < n; i++){
while(top != 0 && judge(point[i], tempPoint[top], tempPoint[top - 1])) top--;
tempPoint[++top] = point[i];
}
int len = top;
tempPoint[++top] = point[n - 2];
for(int i = n - 3; i >= 0; i--){
while(top != len && judge(point[i], tempPoint[top], tempPoint[top - 1])) top--;
tempPoint[++top] = point[i];
}
double result = 0.0;
for(int i = 0; i < top; ++i)
for(int j = i + 1; j < top; ++j)
result = max(result, calcurateDis(tempPoint[i], tempPoint[j]));
return result;
}
int main(){
int n;
while(~scanf("%d", &n)){
for(int i = 0; i < n; i++)
scanf("%d %d", &point[i].x, &point[i].y);
printf("%.0lf\n", solve(n));
}
return 0;
}