"POJ 2187" Beauty Contest (convex packet-graham scanning algorithm)
Find the plane farthest point to the data very large with violence will t. I feel......
Scan out a convex hull and then enumerate the points on the convex hull. Int can also pay attention to the heavy edge and Collinear line code with a few sets of data
The code is as follows:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <vector > #include <stack> #include <algorithm> #define LL long longusing namespace std;typedef struct line line; typedef struct POINT Point;ll mx,my;struct point{ll x, y; LL operator-(const point a) const {return (x-a.x) * (x-a.x) + (Y-A.Y) * (Y-A.Y); } BOOL operator < (const point a) const {if (x = = mx && y = = my) return false; if (a.x = = mx && a.y = = my) return true; if ((X-MX) * (a.y-my)-(y-my) * (a.x-mx) = = 0) return (X-MX) * (X-MX) + (y-my) * (Y-my) < (A.X-MX) * (A.X-MX) + (a.y-my) * (a.y-my) ; Return (X-MX) * (a.y-my)-(y-my) * (A.X-MX) > 0; }};struct line{ll x, y; BOOL operator > (const line a) const//clockwise {return x*a.y-y*a.x > 0; }}; Point Pt[50000];stack <Point> s;vector <Point> tmp;void Read (int &n)//input and perform a polar sort {scanf ("%d", &n); int mm = 0; for (int i =0; I < n; ++i) {scanf ("%lld%lld", &pt[i].x,&pt[i].y); if (pt[i].x < pt[mm].x | | (pt[i].x = = pt[mm].x && pt[i].y < PT[MM].Y)) MM = i; } mx = pt[mm].x; my = Pt[mm].y; Sort (pt,pt+n);} void Settb (int n)//build convex package {point p1,p2; Line L1,L2; S.push (Point{mx,my}); S.push (Pt[0]); for (int i = 1; i < n; ++i) {if (pt[i].x = = mx && pt[i].y = = my) break; P2 = S.top (); S.pop (); while (!s.empty ()) {P1 = S.top (); S.pop (); l1.x = p2.x-p1.x; L1.y = P2.Y-P1.Y; l2.x = pt[i].x-p1.x; L2.y = PT[I].Y-P1.Y; if (L1 > L2) {s.push (P1); Break } P2 = p1; } s.push (p2); S.push (Pt[i]); }}ll MaxLength ()//from the stack constantly out of the stack to find the farthest point distance {int i; ll maxlen = 0; while (!s.empty ()) {point TP; TP = S.top (); S.pop (); for (i = 0; i < tmp.size (); ++i) {maxlen = max (Maxlen,tp-tmp[i]); } tmp.push_back (TP); } return maxlen;} int main () {int n; Read (n); SETTB (n); printf ("%lld\n", MaxLength ()); return 0;} /*input:9200 400300 400300 300400 300400 400500 400500 200350 200200 20040 xx 020
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"POJ 2187" Beauty Contest (convex packet-graham scanning algorithm)