思路:用旋转卡壳求最远点对。
#include <iostream> #include <cmath> #include <algorithm> #include <cstdio> #include <cstring> #include <vector> #define next(i) ((i+1)%n) using namespace std; const double maxn = 401000; const double eps = 1e-8; int dcmp(double x) { if(fabs(x) < eps) return 0; if(x > 0) return 1; return -1; } struct point { double x, y; point() {} point(double a, double b) : x(a), y(b) {} friend point operator - (const point &a, const point &b) { return point(a.x-b.x, a.y-b.y); } friend point operator + (const point &a, const point &b) { return point(a.x+b.x, a.y+b.y); } }; double det(const point &a, const point &b) { return a.x * b.y - a.y * b.x; } bool comp_less(const point &a, const point &b) { return dcmp(a.x-b.x)<0 || (dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)<0); } bool cmp(point &a, point &b) { if(a.x==b.x && a.y==b.y) return true; else return false; } double dist(const point &a, const point &b) { return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y); } struct polygon_convex { vector <point> P; polygon_convex(int Size = 0) { P.resize(Size); } }; polygon_convex convex_hull(vector<point> a) { polygon_convex res(2*a.size()+5); sort(a.begin(), a.end(), comp_less); ///a.erase(unique(a.begin(), a.end()), a.end()); a.erase(unique(a.begin(), a.end(), cmp), a.end()); int m = 0; for(int i = 0; i < (int)a.size(); ++i) { while(m > 1 && dcmp(det(res.P[m-1]-res.P[m-2], a[i]-res.P[m-2]))<=0) --m; res.P[m++] = a[i]; } int k = m; for(int i = int(a.size())-2; i >= 0; --i) { while(m > k && dcmp(det(res.P[m-1]-res.P[m-2], a[i]-res.P[m-2]))<=0) --m; res.P[m++] = a[i]; } res.P.resize(m); if(a.size()>1) res.P.resize(m-1); return res; } double convex_diameter(polygon_convex &a) { vector<point> &p = a.P; int n = p.size(); double maxd = 0.0; if(n==1) { return maxd; } int j = 1; for(int i = 0; i < n; ++i) { while(dcmp(det(p[next(i)]-p[i], p[j]-p[i])-det(p[next(i)]-p[i], p[next(j)]-p[i]))<0) { j = next(j); } double d = dist(p[i], p[j]); if(d > maxd) maxd = d; d = dist(p[next(i)], p[next(j)]); if(d>maxd) maxd = d; } return maxd; } int main() { int T; int n; point A, B, C, D;//正方形四个点。 double x, y, w; vector <point> v; scanf("%d", &T); while(T--) { scanf("%d", &n); v.clear(); for(int i = 0; i < n; i++) { scanf("%lf%lf%lf", &x, &y, &w); A = point(x, y); B = point(x+w, y); C = point(x, y+w); D = point(x+w, y+w); v.push_back(A); v.push_back(B); v.push_back(C); v.push_back(D); } double res = 0.0; polygon_convex result; result = convex_hull(v); res = convex_diameter(result); printf("%d\n", int(res)); } return 0; } /** input: 2 3 0 0 1 1 0 2 0 0 1 6 2 1 2 1 4 2 3 2 3 4 4 4 6 5 1 5 1 3 output: 13 85 **/