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 in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional 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.
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
* 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
4
0 0
0 1
1 1
1 0
Sample Output
2
Hint
Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)
题解
可以参考 YYB的博客 。
1 //It is made by Awson on 2018.1.29 2 #include <set> 3 #include <map> 4 #include <cmath> 5 #include <ctime> 6 #include <queue> 7 #include <stack> 8 #include <cstdio> 9 #include <string> 10 #include <vector> 11 #include <complex> 12 #include <cstdlib> 13 #include <cstring> 14 #include <iostream> 15 #include <algorithm> 16 #define LL long long 17 #define dob complex<double> 18 #define Abs(a) ((a) < 0 ? (-(a)) : (a)) 19 #define Max(a, b) ((a) > (b) ? (a) : (b)) 20 #define Min(a, b) ((a) < (b) ? (a) : (b)) 21 #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b)) 22 #define writeln(x) (write(x), putchar(' ')) 23 #define lowbit(x) ((x)&(-(x))) 24 using namespace std; 25 const int N = 50000; 26 27 int n, loc = 1, S[N+5], tot; 28 struct point { 29 int x, y; 30 point() {} 31 point(int _x, int _y) {x = _x, y = _y; } 32 int operator *(const point &b) const {return x*b.y-y*b.x; } 33 int operator ^(const point &b) const {return (x-b.x)*(x-b.x)+(y-b.y)*(y-b.y); } 34 point operator -(const point &b) const {return point(x-b.x, y-b.y); } 35 }a[N+5]; 36 bool comp(const point &p, const point &q) { 37 int x = (p-a[1])*(q-a[1]); 38 return x > 0 || (x == 0 && (p^a[1])>=(q^a[1])); 39 } 40 bool judge(const point &a1, const point &a2, const point &a3) { 41 int x = (a2-a1)*(a3-a1); 42 return x > 0 || (x == 0 && (a2^a1)>=(a3^a1)); 43 } 44 45 void work() { 46 scanf("%d", &n); 47 for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].x, &a[i].y); 48 for (int i = 2; i <= n; i++) if (a[i].y < a[loc].y || (a[i].y == a[loc].y && a[i].x < a[loc].x)) loc = i; 49 swap(a[1], a[loc]); sort(a+2, a+n+1, comp); a[n+1] = a[1]; 50 S[1] = 1, S[2] = 2, tot = 2; 51 for (int i = 3; i <= n+1; i++) { 52 while (tot > 1 && judge(a[S[tot-1]], a[i], a[S[tot]])) --tot; 53 S[++tot] = i; 54 } 55 --tot; int ans = 0; 56 for (int i = 1, now = 2; i <= tot; i++) { 57 while ((a[S[i+1]]-a[S[i]])*(a[S[now]]-a[S[i]])<(a[S[i+1]]-a[S[i]])*(a[S[now+1]]-a[S[i]])) { 58 ++now; if (now > tot) now = 1; 59 } 60 ans = Max(ans, a[S[now]]^a[S[i]]); 61 } 62 printf("%d ", ans); 63 } 64 int main() { 65 work(); 66 return 0; 67 }