Description
给定很多个正方形的定点,求这些点中两两之间距离的最大值。
Solution
直接求出凸包后旋转卡壳即可。。
Source
/************************************************
* Au: Hany01
* Date: Feb 13th, 2018
* Prob: [LA4728] Square, Seoul 2009
* Email: hany01@foxmail.com
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
inline void File()
{
#ifdef hany01
freopen("la4728.in", "r", stdin);
freopen("la4728.out", "w", stdout);
#endif
}
const double eps = 1e-9;
int dcmp(double x) { if (fabs(x) < eps) return 0; return x < 0 ? -1 : 1; }
struct Point
{
double x, y;
Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); }
Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); }
bool operator < (Point A, Point B) { return A.x < B.x || (A.x == B.x && A.y < B.y); }
double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }
double dis2(Vector A) { return Dot(A, A); }
const int maxn = 400005;
int n;
Point p[maxn];
inline int ConvexHull(Point* p, int n, Point* q)
{
register int m = 0, tmp;
sort(p + 1, p + 1 + n);
For(i, 1, n) {
while (m > 1 && Cross(q[m] - q[m - 1], p[i] - q[m - 1]) >= 0) -- m;
q[++ m] = p[i];
}
tmp = m;
Fordown(i, n - 1, 1) {
while (m > tmp && Cross(q[m] - q[m - 1], p[i] - q[m - 1]) >= 0) -- m;
q[++ m] = p[i];
}
if (n > 1) -- m;
return m;
}
inline double RotatingCaliper2(Point* q, int m)
{
static Point p[maxn];
int n = ConvexHull(q, m, p);
double Ans = 0;
p[n + 1] = p[1];
for (register int u = 1, v = 2; u <= n; ++ u) {
for (;;) {
int dif = dcmp(Cross(p[u + 1] - p[u], p[v + 1] - p[v]));
if (dif >= 0) {
chkmax(Ans, dis2(p[u] - p[v]));
if (!dif) chkmax(Ans, dis2(p[u] - p[v + 1]));
break;
}
++ (v %= n);
}
}
return Ans;
}
int main()
{
File();
for (register int T = read(); T --; ) {
register double x, y, w; register int cnt = 0;
n = read();
For(i, 1, n)
scanf("%lf%lf%lf", &x, &y, &w),
p[++ cnt] = Point(x, y), p[++ cnt] = Point(x + w, y),
p[++ cnt] = Point(x, y + w), p[++ cnt] = Point(x + w, y + w);
printf("%.0lf\n", RotatingCaliper2(p, cnt));
}
return 0;
}
//寻常一样窗前月,才有梅花便不同。
// -- 杜耒《寒夜》