Convex hull: The smallest polygon that can surround all given points (personal understanding)
Graham: Select the point with the lowest Y value, sort the other point angles, then add the 1~n points to the stack, judging the current point, the top of the stack, and the bottom of the stack.
To judge with a cross product, i.e. to be sentenced (top--)
In fact, it is not right oh, you have not reacted to it ~ by the angle of the order will not have this situation, oh, it must first even a[i], and then even S[top]
Specific implementation look at the Code bar ~
#include <cstdio>#include<cmath>#include<algorithm>#defineSQR (x) (x) * (x)#defineRep (i,x,y) for (int i= (x); i<= (y); i++)#definePer (i,x,y) for (int i= (x); i>= (y); i--)typedefDoubleDBD;using namespacestd;Const intn=100010;structpoint{intx, y;} A[n],s[n],b[n];intN,top;intRead () {intD=0, f=1;CharC=getchar (); while(c<'0'|| C>'9') {if(c=='-') f=-1; C=getchar ();} while(c>='0'&&c<='9') d= (d<<3) + (d<<1) +c- -, C=getchar ();returnd*F;} DBD Dis (intXintXxintYintYY) {returnsqrt (SQR (x-xx) +sqr (yyy));}BOOLCross (point x,point y,point z) {return(x.x-z.x) * (Y.Y-Z.Y)-(y.x-z.x) * (X.Y-Z.Y) >=0;}BOOLCMP (Point A,point B) {return(a.x-a[1].x) * (b.y-a[1].Y)-(b.x-a[1].x) * (a.y-a[1].Y) >=0;}intMain () {//judge ();n=read (); intp=0; a[0].y=1e9; Rep (I,1, N) {a[i].x=read (); a[i].y=read (); if(a[i].y<a[p].y| | a[i].y==a[p].y&&a[i].x<a[p].x) p=i; } Swap (a[1],a[p]); Sort (a+2, A +1+n,cmp); s[1]=a[1]; s[2]=a[2]; top=2; Rep (I,3, N) { while(top>=2&&cross (a[i],s[top],s[top-1])) top--; s[++top]=A[i]; } DBD ans=0; for(intI=1; i<top;i++) Ans+=dis (s[i].x,s[i+1].x,s[i].y,s[i+1].y); Ans+=dis (s[1].x,s[top].x,s[1].y,s[top].y); printf ("%.1LF", ans); return 0;}
Graham
Andrew:
It's said to be faster and more stable than Graham, but I don't know much.
The specific process (where specific ...) ): All points with X as the first keyword, y for the second keyword sort, respectively, to find out the upper and lower convex shells, the same judgment when the same method, good
#include <cstdio>#include<cmath>#include<algorithm>#defineSQR (x) (x) * (x)#defineRep (i,x,y) for (int i= (x); i<= (y); i++)#definePer (i,x,y) for (int i= (x); i>= (y); i--)typedefDoubleDBD;using namespacestd;Const intn=100010;structpoint{intx, y;} A[n],s[n];intN,top;intRead () {intD=0, f=1;CharC=getchar (); while(c<'0'|| C>'9') {if(c=='-') f=-1; C=getchar ();} while(c>='0'&&c<='9') d= (d<<3) + (d<<1) +c- -, C=getchar ();returnd*F;} DBD Dis (intXintXxintYintYY) {returnsqrt (SQR (x-xx) +sqr (yyy));} DBD Cross (point x,point y,point z) {return(x.x-z.x) * (Y.Y-Z.Y)-(y.x-z.x) * (x.y-z.y);}BOOLCMP (point A,point b) {returna.x<b.x| | a.x==b.x&&a.y<b.y;}intMain () {n=read (); Rep (I,1, n) a[i].x=read (), a[i].y=read (); Sort (a+1, A +1+n,cmp); Rep (I,1, N) { while(top>1&&cross (a[i],s[top],s[top-1]) >=0) top--; s[++top]=A[i]; } intk=top; Per (I,n-1,1) { while(Top>k&&cross (a[i],s[top],s[top-1]) >=0) top--; s[++top]=A[i]; } DBD ans=0; Rep (I,1, top-1) Ans+=dis (s[i].x,s[i+1].x,s[i].y,s[i+1].y); Ans+=dis (s[1].x,s[top].x,s[1].y,s[top].y); printf ("%.1LF", ans); return 0;}
Andrew
You may wonder why the nth point must be on a convex hull. Of course, because its x is the largest, even if it is the same as its x, its y is larger, yy a bit of brain repair out
Convex hull--graham scanning method and Andrew algorithm