The coordinates of n points are given, and the square of the longest distance between points is obtained.
Solution: Build a convex hull. The longest distance point must be on the convex hull. The point on the violent enumeration convex hull can pass through without timeout.
[Csharp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
Struct aa
{
Int x, y;
Double angle;
} Point [1, 51000], stack [2, 51000];
Double count (int x1, int y1, int x2, int y2)
{
Return atan2 (double) (y1-y2), (double) (x1-x2 ));
}
Void angle (int n, int m)
{
Int I;
Point [m]. angle =-1;
For (I = 1; I <= n; I ++)
If (I! = M)
Point [I]. angle = count (point [I]. x, point [I]. y, point [m]. x, point [m]. y );
}
Int cmp (const void * a, const void * B)
{
Struct aa * c = (struct aa *);
Struct aa * d = (struct aa *) B;
If (c-> angle! = D-> angle)
Return (c-> angle> d-> angle )? 1:-1;
Else
If (c-> x! = D-> x)
Return c-> x-d-> x;
Return c-> y-d-> y;
}
Int dist (int x1, int y1, int x2, int y2)
{
Return (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2 );
}
Int cross (int x1, int y1, int x2, int y2)
{
Return (x1 * y2)-(x2 * y1)> 0 )? 1:0;
}
Int Gansan (int n, int top)
{
Int I, j;
For (I = 4; I <= n; I ++)
{
While (top> = 2)
{
If (cross (stack [top]. x-stack [top-1]. x, stack [top]. y-stack [top-1]. y, point [I]. x-stack [top]. x, point [I]. y-stack [top]. y ))
{
Stack [++ top] = point [I];
Break;
}
Else
Top --;
}
If (top = 1)
Stack [++ top] = point [I];
}
Return top;
}
Int main ()
{
Int I, j, n, m, top, num;
Scanf ("% d", & n );
For (I = 1, m = 1; I <= n; I ++)
{
Scanf ("% d", & point [I]. x, & point [I]. y );
If (point [I]. y <point [m]. y | (point [I]. y = point [m]. y & point [I]. x <point [m]. x ))
M = I;
}
// Calculate the Polar Angle
Angle (n, m );
// Polar sorting
Qsort (point + 1, n, sizeof (point [0]), cmp );
If (n = 2)
{
Printf ("% d \ n", dist (point [1]. x, point [1]. y, point [2]. x, point [2]. y ));
Return 0;
}
Stack [1] = point [1];
Stack [2] = point [2];
Stack [3] = point [3];
Top = 3;
Top = Gansan (n, top );
For (I = 1, m =-1; I <top; I ++)
{
For (j = I + 1; j <= top; j ++)
{
Num = dist (stack [I]. x, stack [I]. y, stack [j]. x, stack [j]. y );
If (m <num)
M = num;
}
}
Printf ("% d \ n", m );
Return 0;
}