POJ 2187 Beauty Contest(凸包)
阿新 • • 發佈:2018-11-03
題意:題目的意思就是說給你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; }