题面
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.
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
4
0 0 0 1 1 1 1 0Sample Output
2
Hint
Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)
题解
题目大意:给出若干个农场的坐标,求出相距最远的农场的直线距离的平方。
题解: 首先扫描法求出凸包,旋转卡壳求出最大值即可
#include#include #include #include #include #include #include using namespace std;#define MAX 50010#define INF 1000000000#define rg registerinline int read(){ int x=0,t=1;char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-')ch=getchar(); if(ch=='-'){t=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();} return x*t;}struct Node{ int x,y;}p[MAX],p0,S[MAX];int n,top,T;inline bool cmp(Node a,Node b){ rg double A=atan2(a.y-p0.y,a.x-p0.x); rg double B=atan2(b.y-p0.y,b.x-p0.x); if(A!=B)return A p[i].y||(p0.y==p[i].y&&p0.x>p[i].x)) p0=p[i],k=i; swap(p[k],p[0]); sort(&p[1],&p[n],cmp);//关于最下方的点排序 S[0]=p[0];S[1]=p[1]; top=1;//栈顶 for(rg int i=2;i =0) top--; else S[++top]=p[i++]; }}inline long long Dis(Node a,Node b)//计算两点的距离的平方和 { return 1LL*(a.x-b.x)*(a.x-b.x)+1LL*(a.y-b.y)*(a.y-b.y);}long long GetMax()//求出直径 { rg long long re=0; if(top==1)//仅有两个点 return Dis(S[0],S[1]); S[++top]=S[0];//把第一个点放到最后 int j=2; for(int i=0;i