poj2187 the farthest point of plane, Garham_scan algorithm to find convex packet

Source: Internet
Author: User

poj2187 the farthest point of the plane, Garham_scan algorithm to find convex hull beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 29666 Accepted: 9180

Description

Bessie, Farmer John's Prize cow, had just won first place in a bovine beauty contest, earning the title ' Miss Cow World '. As a result, Bessie would make a tour of N (2 <= n <= 50,000) farms around the world in order to spread goodwill BETW Een farmers and their cows. For simplicity, the world would be represented as a two-dimensional plane, where each farm was located at a pair of integers Coordinates (x, y), each has a value in the range-10,000 ... No. 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 quit e large, so she wants-bring a suitcase full of hay with her so she she had 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, which is the squared distance between the pair of farms, is 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) test instructions: The farthest point of the plane to the idea: the first convex hull, in the point of the convex polygon of violence to solve the answer, slot point: Double super , int is only over.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<math.h>#include<ctype.h>using namespacestd;Const intmaxn=1000100;Const intInf= (1<< -);Const inteps=0.00000001;intN;structpoint{intx, y; Friendint operator*(Point A,point B) {returna.x*b.y-a.y*b.x; } Friend Pointoperator-(Point A,point B) {return{a.x-b.x,a.y-B.Y}; }}; Point FARM[MAXN]; Point ANS_FARM[MAXN];intAns_top;intDist2 (Point a,point B) {inttx=a.x-b.x,ty=a.y-b.y; returntx*tx+ty*Ty;}BOOLCmpyx (ConstPoint A,ConstPoint B) {    if(A.Y&LT;B.Y)return true; if(a.y==b.y) {        returna.x<=b.x; }    return false;}BOOLcmpConstPoint A,ConstPoint B) {    intTmp= (a-farm[0]) * (b-farm[0]); returnTmp>0;}voidGraham_scan () {sort (farm,farm+N,cmpyx); Sort (Farm+1, farm+n,cmp); Ans_top=0; Ans_farm[ans_top++]=farm[0]; Ans_farm[ans_top++]=farm[1];  for(intI=2; i<n;i++){        if((ans_farm[ans_top-1]-ans_farm[ans_top-2]) * (farm[i]-ans_farm[ans_top-1]) <0) {Ans_top--; Ans_farm[ans_top++]=Farm[i]; }        Elseans_farm[ans_top++]=Farm[i]; }}intMain () { while(cin>>N) {         for(intI=0; i<n;i++) {scanf ("%d%d",& (farm[i].x),&(FARM[I].Y));        } graham_scan (); intans=0;  for(intI=0; i<ans_top-1; i++){             for(intj=i+1; j<ans_top;j++){                inttmp=Dist2 (Ans_farm[i],ans_farm[j]); if(Tmp>ans) ans=tmp; }} printf ("%d\n",(int) ans); }    return 0;}
View Code

poj2187 the farthest point of plane, Garham_scan algorithm to find convex packet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.