poj1113--Convex bag (Andrew)

Source: Internet
Author: User

Main topic:

Given the coordinates of several points on a plane, your task is to build a circular fence, enclose all the dots, and be no less than L from all points. Find the minimum length of the fence.

It is easy to conclude that the girth of the convex hull + the circumference of the circle with the radius of L.

Let's talk about the Andrew algorithm here.

Andrew is a variant of the Graham algorithm, and Andrew is faster and more stable.

Andrew algorithm idea is to first n points according to the x-coordinate from small to large sort (x same as Y from small to large), get a sequence a1,a2,... an, will A1,A2 into the CH array, starting from A3, judging whether the point in the convex hull current direction of the left, if it is, The point is added to the CH array, otherwise the point in CH is deleted until the left side, repeating the above operation. It is worth noting that Andrew needs to do 2 of these processes, the first time to find the "convex hull", the second time to find "convex hull", the merger is the complete convex hull.

See the code specifically.

#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>using namespacestd;#definePi ACOs (-1.0)structpoint{intx, y; Point (intx=0,inty=0): X (x), Y (y) {}}a[1001],ch[1001];typedef Point Vector;pointoperator-(Point A,point b) {returnPoint (a.x-b.x,a.y-b.y);}intCross (Vector A,vector b) {return(a.x*b.y-a.y*b.x);}DoubleLength (Vector a) {return(SQRT (Double) a.x*a.x+a.y*a.y));}BOOLCMP (point A,point b) {return(a.x<b.x| | (a.x==b.x&&a.y<b.y));}intn,i,j,k,l,x,y;Doublesum=0;intMain () {scanf ("%d%d",&n,&l);  for(i=1; i<=n;++i) scanf ("%d%d",&a[i].x,&a[i].y); Sort (a+1, a+n+1, CMP); intm=0;  for(i=1; i<=n;++i) {         while(m>1&&cross (ch[m]-ch[m-1],a[i]-ch[m-1]) <0) m--; ch[++m]=A[i]; } k=m;  for(i=n-1; i>=1;--i) {         while(M>k&&cross (ch[m]-ch[m-1],a[i]-ch[m-1]) <=0) m--; ch[++m]=A[i]; }     for(i=1; i<m;++i) Sum+=length (ch[i+1]-Ch[i]); printf ("%.0f\n", sum+2*pi*l); return 0;}
poj1113

poj1113--Convex bag (Andrew)

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.