Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22893 Accepted: 6994 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. 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 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 簡單模版的運用 [cpp] #include <stdio.h> #include <string.h> #include <math.h> struct map { int x,y; }a[50010]; struct num { int x,y; }statck[50010]; int chacheng(int x1,int y1,int x2, int y2) { int t=x1*y2-y1*x2; if(t>0) { return 1; }else if(t<0) { return -1; }else { return 0; } } int cmp(const void *e,const void *f) { struct map *p1=(struct map *)e; struct map* p2=(struct map *)f; int x1,y1,x2,y2,t; x1=p1->x-a[0].x; y1=p1->y-a[0].y; x2=p2->x-a[0].x; y2=p2->y-a[0].y; t=chacheng(x1,y1,x2,y2); if(t==1) { return -1; }else if(t==-1) { return 1; }else { return (x1*x1+y1*y1-(x2*x2+y2*y2)); } } int main() { int i,j,n,m,s,t,pos1,top,dis; int x1,y1,x2,y2; while(scanf("%d",&n)!=EOF) { for(i=0;i<=n-1;i++) { scanf("%d %d",&a[i].x,&a[i].y); if(i==0) { pos1=i; }else if((a[i].y<a[pos1].y)||(a[i].y==a[pos1].y&&a[i].x<a[pos1].x)) { pos1=i; } } t=a[0].x; a[0].x=a[pos1].x ;a[pos1].x=t; t=a[0].y; a[0].y=a[pos1].y; a[pos1].y=t; qsort(a+1,n-1,sizeof(a[0]),cmp); top=0; statck[top].x=a[0].x; statck[top++].y=a[0].y; statck[top].x=a[1].x; statck[top++].y=a[1].y; for(i=2;i<=n-1;) { if(top>=2) { x1=statck[top-1].x-statck[top-2].x; y1=statck[top-1].y-statck[top-2].y; x2=a[i].x-statck[top-1].x; y2=a[i].y-statck[top-1].y; t=chacheng(x1,y1,x2,y2); if(t==1||t==0) { statck[top].x=a[i].x; statck[top++].y=a[i].y; i++; }else { top--; } }else { statck[top].x=a[i].x; statck[top++].y=a[i].y; i++; } } for(i=0,dis=0;i<=top-1;i++) { for(j=i+1;j<=top-1;j++) { x1=statck[j].x-statck[i].x; y1=statck[j].y-statck[i].y; if((x1*x1+y1*y1)>dis) { dis=x1*x1+y1*y1; } } } printf("%d\n",dis); } return 0; }