Convex hull rotating jam to find the maximum triangular area
Maximum triangleTime
limit:5000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3316 Accepted Submission (s): 1119
Problem description Teacher in the computational geometry of this class to decorate a topic for Eddy, the title is this: given a two-dimensional plane on the n different points, required to find three points in these points, so that they constitute the triangle has the largest area.
Eddy to the problem of the solution, think not general what method to solve, so he found the smart you, ask you to help him solve the problem.
Input data contains multiple sets of test cases, the first line of each test case contains an integer n, indicating that there are altogether n different points, and the next n rows each row contains 2 integer xi,yi, which represents the x and Y coordinates of the first point on the plane. You can think of: 3 <= n <= 50000 and -10000 <= XI, Yi <= 10000.
Output for each set of test data, outputs the area of the largest triangle that is composed, and the result retains two decimal places.
One row for each set of outputs.
Sample Input
33 42 63 762 63 92 08 06 67 7
Sample Output
1.5027.00
Authoreddy
/* ***********************************************author:ckbosscreated time:2014 December 28 Sunday 19:28 15 seconds file Name : hdoj2202.cpp************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map>using namespace std;struct Point{double x, y; Point operator-(point a) Const{return (point) {X-A.X,Y-A.Y};} BOOL Operator < (point a) const{if (x!=a.x) return X<a.x;return Y<A.Y;} BOOL operator = = (Point a) Const{return (x==a.x) && (Y==A.Y);}; int n,m;double Cross (point A,point B) {return a.x*b.y-a.y*b.x;} Double Area2 (const point A,const point B,const point C) {return fabs (Cross (B-A,C-A));} Vector<point> vp;vector<point> ch;void covexhull (vector<point> &p) {sort (P.begin (), P.end ()); P.erase (Unique (P.begin (), P.end ()), P.end ()), int n=p.size ();m=0;ch=vector<point> (n+1), for (int i=0;i<n;i++) {while (M>1&&cross (ch[m-1]-ch[ M-2],p[i]-ch[m-2]) <0) m--;ch[m++]=p[i];} int k=m;for (int i=n-2;i>=0;i--) {while (M>k&&cross (Ch[m-1]-ch[m-2],p[i]-ch[m-2]) <0) m--;ch[m++]=p[ I];} if (n>1) m--;ch.resize (m);} Double Rotating_calipers (vector<point>& p,int N) {double ans=0;for (int i=0;i<n;i++) {int j= (i+1)%n;int k= ( j+1)%n;while (j!=i&&k!=i) {Ans=max (ANS,AREA2 (P[i],p[j],p[k])), while (Cross (p[i]-p[j],p[(k+1)%n]-p[k]) < 0) k= (k+1)%n;j= (j+1)%n;} return ans;} int main () {//freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout), while (scanf ("%d", &n)!=eof) {vp.clear (); for (int i=0;i<n;i++) {int x,y;scanf ( "%d%d", &x,&y); Vp.push_back ((point) {x, y});} Covexhull (VP);d ouble area=rotating_calipers (ch,ch.size ())/2.; printf ("%.2lf\n", area);} return 0;}
Hdoj 2202 Largest triangular convex hull rotating jam to find the maximum triangular area