(Hdu step 7.1.6) Maximum triangle (application of convex hull--3 points found in n points, they form the largest triangular area)

Source: Internet
Author: User
Tags mul

Topic:

Maximum triangle
Time limit:5000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 121 Accepted Submission (s): 61
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
Recommendlcy


Topic Analysis:

The simple application of convex hull, find 3 points in n points, they form the largest triangle area. There are generally two ways of thinking about this problem:

1) Direct violence. This will definitely be tle, because the data range of n is around 50000.

2) The convex hull is obtained first. Then look for these three points in the convex hull, so the data size will be much smaller. It turns out that the largest triangles can be formed

These three points must also be on a convex hull.


The code is as follows:

#include <iostream> #include <cstdio> #include <cmath> #include <algorithm>using namespace std /** * To find the maximum area of a triangle formed by any three points in n points. * 1) Direct violence. Sure will be tle. Because N is more than 50,000. * * 2) The three points of the largest triangle formed must be on the convex hull. Find these three points on the convex hull * turn the problem into: * The largest area of the triangle formed by finding three points on a convex hull. */const double epsi = 1e-8;const double pi = ACOs ( -1.0); const int MAXN = 50001;struct ppoint{//struct Try not to define it as a point, easy and C + + The variable in the body has the same name double x;double y;  PPoint (Double _x = 0,double _y = 0): X (_x), Y (_y) {}ppoint operator-(const ppoint& OP2) Const{return ppoint (x-op2.x,y -OP2.Y);} Double operator^ (const ppoint &AMP;OP2) Const{return x*op2.y-y*op2.x;}; inline int sign (const double &x) {if (x > Epsi) {return 1;} if (x <-epsi) {return-1;} return 0;} Inline double sqr (const double &x) {return x*x;} Inline double mul (const ppoint& p0,const ppoint& p1,const ppoint& p2) {return (p1-p0) ^ (p2-p0);} Inline double dis2 (const ppoint &p0,const ppoint &p1) {return Sqr (p0.x-p1.x) + SQR (P0.Y-P1.Y);} inline double dis (const ppoint&Amp P0,const ppoint& p1) {return sqrt (Dis2 (P0,P1));} int n; PPoint P[MAXN]; PPoint convex_hull_p0;inline bool convex_hull_cmp (const ppoint& a,const ppoint& b) {return sign (Mul (convex_hull _p0,a,b) >0) | | (Sign (Mul (convex_hull_p0,a,b)) = = 0 && dis2 (convex_hull_p0,a) < Dis2 (convex_hull_p0,b));} int Convex_hull (ppoint* a,int n,ppoint* b) {int i;for (i = 1; i < n; ++i) {if (sign (a[i].x-a[0].x) < 0 | | (sign (a[i].x-a[0].x) = = 0 && sign (A[I].Y-A[0].Y) < 0)) {swap (a[i],a[0]);}} Convex_hull_p0 = a[0];//These two lines of code do not change sequentially. Otherwise it will Wasort (A,A+N,CONVEX_HULL_CMP), b[0] = a[0];b[1] = A[1];int newn = 2;for (i = 2; i < n; ++i) {while (Newn > 1 && Amp Sign (Mul (b[newn-1],b[newn-2],a[i)) >= 0) {newn--;} b[newn++] = A[i];} return newn;} /** * There is a triangle of three points to calculate the area of this triangle */double Crossprod (ppoint A, PPoint B, PPoint C) {return (b.x-a.x) * (C.Y-A.Y)-(B.Y-A.Y) * ( c.x-a.x);} int main () {while (scanf ("%d", &n)!=eof) {int i;for (i = 0; i < n; ++i) {scanf ("%lf%lf", &p[i].x,&amP;P[I].Y);} n = convex_hull (p,n,p);p [n] = p[0];d ouble max_ans = -1;int J;int k;for (i = 0; i < n; ++i) {for (j = i+1; J < N; + +) j) {for (k = j+1; k <= N; ++k) {Double ans = fabs (Crossprod (P[i],p[j],p[k]))/2;if (Max_ans < ans) {Max_ans = ans;}}} printf ("%.2lf\n", Max_ans);} return 0;}





(Hdu step 7.1.6) Maximum triangle (application of convex hull--3 points found in n points, they form the largest triangular area)

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.