题目:http://poj.org/problem?id=2187
View Code
#include <iostream> #include <cstdio> #include <math.h> #include <cstring> #include <algorithm> #define sqr(x) ((x)*(x)) using namespace std; const double eps = 1e-8; const int pi = acos(-1.0); const int N = 500000+10; int dcmp(double x) { if (x < -eps) return -1; else return x > eps; } struct cpoint { double x, y; cpoint() {} cpoint(double x, double y):x(x),y(y) {} cpoint operator + (const cpoint &u) const { return cpoint(x + u.x, y + u.y); } cpoint operator - (const cpoint &u) const { return cpoint(x - u.x, y - u.y); } double operator * (const cpoint &u) const { return x * u.y - y * u.x; } double operator ^ (const cpoint &u) const { return x * u.x + y * u.y; } bool operator == (const cpoint& u) const { return dcmp(x - u.x) == 0 && dcmp(y - u.y) == 0; } bool operator < (const cpoint& u) const { if (dcmp(x - u.x)) return x < u.x; else return dcmp(y - u.y) < 0; } double len() { return sqrt(x * x + y * y); } void get() { scanf("%lf%lf", &x, &y); } }; double cross(cpoint o, cpoint p, cpoint q) { return (p - o) * (q - o); } double dot(cpoint o, cpoint p, cpoint q) { return (p - o) ^ (q - o); } double dissqr(cpoint p, cpoint q) { return sqr(p.x - q.x) + sqr(p.y - q.y); } cpoint bp; int PolarCmp(const cpoint &p1, const cpoint &p2) { int u = dcmp(cross(bp, p1, p2)); return u > 0 || (u == 0 && dcmp(dissqr(bp,p1)-dissqr(bp,p2))<0); } void graham(cpoint pin[], int n, cpoint ch[], int &m) { int i, j, k, u, v; memcpy(ch, pin, n * sizeof(cpoint)); for (i = k = 0; i < n; i++) { u = dcmp(ch[i].x - ch[k].x); v = dcmp(ch[i].y - ch[k].y); if (v < 0 || (v == 0 && u < 0)) k = i; } bp = ch[k]; sort(ch, ch + n, PolarCmp); n = unique(ch, ch + n) - ch; if (n <= 1) { m = n; return ; } if (dcmp(cross(ch[0], ch[1], ch[n-1])) == 0) { m = 2, ch[1] = ch[n-1]; return ; } ch[n++] = ch[0]; for (i = 1, j = 2; j < n; j++){ while (i > 0 && dcmp(cross(ch[i-1], ch[i], ch[j])) <= 0) i--; ch[++i] = ch[j]; } m = i; } double rotating(cpoint cp[], int n){ int i = 1; double res = 0.0; cp[n] = cp[0]; for (int j = 0; j < n; j++){ while (dcmp(fabs(cross(cp[i+1], cp[j], cp[j+1]))- fabs(cross(cp[i], cp[j], cp[j+1])) ) > 0) i = (i + 1) % n; res = max(res, max(dissqr(cp[i], cp[j]), dissqr(cp[i+1], cp[j+1]))); } return res; } cpoint fp[N], ch[N]; int main() { //freopen("D:/a.txt", "r", stdin); int n, m; while (~scanf("%d", &n)) { for (int i=0; i<n; i++) fp[i].get(); graham(fp, n, ch, m); double ans = rotating(ch, m); printf("%d\n", (int)(ans+0.5)); } return 0; }