POJ 2079 Triangle (rotating jam)

Source: Internet
Author: User

Triangle
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 8917 Accepted: 2650

Description

Given n distinct points on a plane, your task was to find the triangle that has the maximum area, whose vertices was from The given points.

Input

The input consists of several test cases. The first line of all test case contains a integer n, indicating the number of points on the plane. Each of the following n lines contains is an integer xi and Yi, indicating the ith points. The last line of the input was an integer−1, indicating the end of input, which should wasn't be processed. Assume that 1 <= n <= 50000 and−104 <= XI, Yi <= 104 for all i = 1 ... n.

Output

For each test case, print a line containing the maximum area, which contains, and then the decimal point. You may assume this there is always a answer which is greater than zero.

Sample Input

33 42 62 752 63 92 08 06 5-1

Sample Output

0.5027.00

Source

Shanghai 2004 Preliminary

Ideas

Convex hull + rotating jam

Rotation Jam: First determine two points, cross product to find the largest 3rd, and then change the second point to continue.

Quote: The maximum triangular area of the point set, O (n) rotation jam, convex package, and then select the beginning of three points p,q,r start rotation, note r No more than the first point, q not more than R,p Q. Do three times each time to advance, first push R, so that the PQ fixed area of the largest, and then push Q, and then push P, if three times have not been pushed, R propulsion a lattice. Update the area maximum at one point after each push.

Code

1#include <cstdio>2#include <vector>3#include <iostream>4#include <algorithm>5 #definefor (A,B,C) for (int a= (b); a<= (c); a++)6 using namespacestd;7 8 structPt {9     intx, y;TenPt (intx=0,inty=0): X (x), Y (y) {}; One }; A typedef Pt VEC; -  -Vecoperator-(Pt a,pt B) {returnVEC (a.x-b.x,a.y-b.y); } the BOOL operator< (Constpt& A,Constpt&b) { -     returna.x<b.x | | (a.x==b.x && a.y<b.y); - } - BOOL operator== (Constpt& A,Constpt&b) { +     returna.x==b.x && a.y==b.y; - } + intCross (VEC A,vec B) {returna.x*b.y-a.y*b.x;} A intDist (Pt a,pt B) { at     return(a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b.y); - } -Vector<pt> Convexhull (vector<pt>p) { - sort (P.begin (), P.end ()); - p.erase (Unique (P.begin (), P.end ()), P.end ()); -     intN=p.size (), m=0; inVector<pt> CH (n+1); -      for(intI=0; i<n;i++) { to          while(m>1&& Cross (ch[m-1]-ch[m-2],p[i]-ch[m-2]) <=0) m--; +ch[m++]=P[i]; -     } the     intk=m; *      for(inti=n-2; i>=0; i--) { $          while(M>k && Cross (ch[m-1]-ch[m-2],p[i]-ch[m-2]) <=0) m--;Panax Notoginsengch[m++]=P[i]; -     } the     if(n>1) m--; +Ch.resize (m);returnch; A } the  + intN; -Vector<pt>P,ch; $  $ intRC () { -     intn=ch.size (); -     intans=0, cur=1, j,k; the Pt v; -for (I,0, N-1) {WuyiJ= (i+1)%n, k= (j+1)%N; the          while(J!=i && k!=i) { -Ans=max (Ans,abs (ch[j]-ch[i],ch[k]-Ch[i] )); Wu              while(Cross (ch[i]-ch[j],ch[(k +1)%n]-ch[k]) <0) k= (k +1)%N; -J= (j+1)%N; About         } $     } -     returnans; - } -  A intMain () { +Freopen ("in.in","R", stdin); theFreopen ("Out.out","W", stdout); -      while(SCANF ("%d", &n) = =1&& n!=-1) { $ p.clear (), Ch.clear (); the         intx, y; thefor (I,1, N) { thescanf"%d%d",&x,&y); the P.push_back (Pt (x, y)); -         } inCh=Convexhull (p); theprintf"%.2lf\n", RC ()/2.0); the     } About     return 0; the}

POJ 2079 Triangle (rotating jam)

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.