Beauty Contest
Time limit:3000 Ms |
|
Memory limit:65536 K |
Total submissions:24124 |
|
Accepted:7364 |
Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss cow world '. as a result, Bessie will make a tour of N (2 <= n <= 50,000) farms around the world order to spread goodwill between farmers and their Cows. for simplicity, the world will be represented as a two-dimen1_plane, where each farm is located at a pair of integer coordinates (x, y ), each having a value in the range-10,000... 10,000. no two farms share the same pair of coordinates.Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. since Bessie refills Her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring. help Bessie by computing the maximum distance among all pairs of farms. Input * Line 1: A single integer, n* Lines 2. n + 1: two space-separated integers x and y specifying coordinate of each farm Output * Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.Sample Input 40 00 11 11 0 Sample output 2 Hint Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)Source Usaco 2003 fall |
Find the vertices on the convex hull, and then perform brute force enumeration.
If the number of vertices is less than 100, the system will launch brute-force attacks!
# Include <iostream> # include <stdio. h> # include <string. h >#include <algorithm> # include <cmath> using namespace STD; # define EPS 1e-8 # define PI 3.14159265 struct point {int X; int y;} po [55500], temp; int N, Pos; bool zero (double A) {return FABS (a) <EPS;} int DIS (point & A, point & B) // returns the square {return (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y);} int struct SS (point & A, point & B, point & C) // calculate the X product of a B and A c {return (B. x- A. x) * (C. y-a.y)-(B. y-a.y) * (C. x-a.x);} int CMP (const void * a, const void * B) {return writable SS (po [0], * (point *) A, * (point *) b)> 0? -1: 1;} int select () {int I, j, k = 1; for (I = 2; I <n; I ++) {If (po [0], po [K], po [I]) = 0) {If (DIS (po [0], po [k]) <DIS (po [0], po [I]) po [k] = po [I];} elsepo [++ K] = po [I];} return k + 1;} int Graham (INT num) {int I, j, k = 2; //////////////////////////////////////// // Po [num] = po [0]; // fangbian num ++; int ans = 0; int t; If (Num <20) {for (I = 0; I <num; I ++) for (j = 0; j <num; j ++) {T = DIS (po [I], po [J]); If (T> ans) ans = T;} printf ("% d \ n", ANS); Return 0;} For (I = 3; I <num; I ++) {While (Primary SS (po [k-1], po [K], po [I]) <-EPS) {k --;} po [++ K] = po [I]; // This loop ends, no more !} /* For (I = 0; I <K; I ++) printf ("% lf \ n", po [I]. x, po [I]. y); */for (I = 0; I <K; I ++) for (j = 0; j <K; j ++) {T = DIS (po [I], po [J]); If (T> ans) ans = T;} printf ("% d \ n", ANS ); return 0;} int main () {int I, j, k; point my_temp; int ans; int t; while (scanf ("% d", & N )! = EOF) {ans = 0; scanf ("% d", & po [0]. x, & po [0]. y); temp = po [0]; Pos = 0; for (I = 1; I <n; I ++) {scanf ("% d ", & po [I]. x, & po [I]. y); If (po [I]. Y <temp. y) temp = po [I], Pos = I;} If (n <100) {for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) {T = DIS (po [I], po [J]); If (T> ans) ans = T;} printf ("% d \ n", ANS); continue;} my_temp = po [0]; po [0] = po [POS]; po [POS] = my_temp; qsort (PO + 1, n-1, sizeof (po [0]), CMP); Graham (select ();} return 0 ;}