(Hdu step 7.1.6) Maximum triangle (convex hull Application -- 3 points are found in n points, and the triangle area is the largest), hdu7.1.6

Source: Internet
Author: User

(Hdu step 7.1.6) Maximum triangle (convex hull Application -- 3 points are found in n points, and the triangle area is the largest), hdu7.1.6

Question:

Maximum triangle
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 121 Accepted Submission (s): 61
 
In the course of computing ry, The Problem Description teacher assigned Eddy a question titled n different points on a given two-dimensional plane, you need to search for three points in these points so that the triangle they constitute has the largest area.
Eddy is puzzled by the problem and cannot figure out how to solve it. Therefore, he has found a smart one. Please help him solve the problem.
The Input data contains multiple groups of test cases. The first line of each test case contains an integer n, indicating a total of n different points, each row of the next n rows contains two integers xi, yi, indicating the x and y coordinates of the I point on the plane. 3 <= n <= 50000 and-10000 <= xi, yi <= 10000.
For each set of test data, Output outputs the area of the largest triangle and returns two decimal places.
Each group of output occupies one row.
 
Sample Input
33 42 63 762 63 92 08 06 67 7
 
Sample Output
1.5027.00
 
AuthorEddy
 
Recommendlcy


Question Analysis:

The simple application of convex hull, finding 3 points in n points, they form the largest Triangle Area. This question generally has two ideas:

1) direct violence. This will certainly be TLE, because n's data range is around 50000.

2) Calculate the convex hull first. Then we can find these three points in the convex bag. In this case, the data size will be much smaller. It turns out that the largest triangle can be formed

These three points must also be on the convex hull.


The Code is as follows:

# Include <iostream> # include <cstdio> # include <cmath> # include <algorithm> using namespace std; /*** calculate the maximum area of the triangle formed by any three points in n points. * 1) direct violence. Yes. Because n is more than 50000. ** 2) the three points of the largest Triangle must be on the convex hull. Finding these three points on the convex bag * converts the problem into: * finding the three points on the convex bag has the largest area of the triangle .. */const double epsi = 1e-8; const double pi = acos (-1.0); const int maxn = 50001; struct PPoint {// struct should not be defined as Point, it is easy to use the 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 & 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 (rows X-p1.x) + sqr (p0.y-p1.y);} inline double dis (const PPoint & 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,, 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]; // do not change the two lines of code in sequence .. otherwise, 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 & sign (mul (B [newn-1], B [newn-2], a [I])> = 0) {newn --;} B [newn ++] = a [I];} return newn ;} /*** there is A triangle with three points to calculate the area of the 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 (sca Nf ("% d", & n )! = EOF) {int I; for (I = 0; I <n; ++ I) {scanf ("% lf", & p [I]. x, & p [I]. y);} n = convex_hull (p, n, p); p [n] = p [0]; double 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 ;}





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.