HDU 3934 summer holiday rotate the maximum Triangle Area of the card Shell

Source: Internet
Author: User

// The question is very simple, that is, the area of the maximum triangle of N points .. Think of convex bag .. A look at the data, 1000000. If you use enumeration, it will certainly be TLE ..
// It took two days to see the rotating jamming case .. I still don't know much about it...
// The vertices on the convex hull may be 3 points in line... A problem that is the same as shit. It took me two days .. Dizzy ..
// I want to learn it again next time .. Dizzy ..
# Include <iostream>
# Include <stdio. h>
# Include <algorithm>
# Include <string. h>
# Include <math. h>
# Define PI ACOs (-1.0)
Using namespace STD;

Typedef double pointper; // coordinate type

# Define pointnum 100005 // maximum number of vertices
# Define prex 1e-11 // used when the coordinate of a vertex is of the real number type

Struct Node
{
Pointper X, Y;
} Point [2, 100001];

Class Polygon
{
Public:
Int sta [pointnum]; // coordinate of the point on the convex hull
Node point [pointnum];
Bool flag [pointnum];
Int top, N, stab; // n indicates the number of reading points, top-1 indicates the number of points on the convex hull, (0 ~~ Top-2) is the coordinates of the vertices on the convex hull. Top-1 and 0 are the first vertices;
Pointper X1, Y1, X2, Y2;
Polygon ()
{
Top = n = 0;
}
Static bool CMP (const node & A, const node & B)
{
Return A. x <B. x | A. X = B. X & A. Y <B. Y; // sort by X first, and then by Y.
}
Bool X (double X1, double Y1, double X2, double Y2, bool f) // if the value is true, the vertex on the edge is included.
{
If (f)
Return X1 * y2-x2 * Y1> = 0.0 | FABS (x1 * y2-x2 * Y1) <prex;
Return X1 * y2-x2 * Y1> 0.0;
}
Double DIS (node A, Node B)
{
Return SQRT (double) (A. x-b.x) * (A. x-b.x) + (A. y-b.y) * (A. y-b.y )));
}
Double max (double A, double B)
{
Return A> B? A: B;
}
Double xmult (node o, node A, Node B)
{
Return (A. x-o.x) * (B. y-o.y)-(B. x-o.x) * (A. y-o.y );
}
Void pointselect (bool F); // calculate the vertex on the convex hull. If F is true, the vertex on the edge is included;
Void getpoint (int I, bool F );
Void XY (int I); // auxiliary X () for cross Multiplication
Double Length (); // calculates the perimeter of the convex hull.
Double area (); // calculates the area of the convex hull;
Bool isinpoly (Double X, Double Y, bool F );
};

Polygon tubao;

Void polygon: XY (int I)
{
X1 = point [I]. X-point [sta [Top-2]. X;
Y1 = point [I]. Y-point [sta [Top-2]. Y;
X2 = point [sta [Top-1]. X-point [sta [Top-2]. X;
Y2 = point [sta [Top-1]. Y-point [sta [Top-2]. Y;
}

void polygon: getpoint (int I, bool f)
{< br> XY (I );
If (Top = stab | x (x1, Y1, X2, Y2, f)
{< br> sta [top ++] = I;
flag [I] = false;
}< br> else
{< br> top --;
flag [sta [Top] = true;
XY (I);
while (top> stab &&! X (x1, Y1, X2, Y2, f)
{< br> top --;
flag [sta [Top] = true;
XY (I);
}< br> sta [top ++] = I;
flag [I] = false;
}< BR >}< br> void polygon: pointselect (bool f)
{< br> int I;
memset (flag, true, n + 1);
sort (point, point + N, CMP);
sta [0] = 0;
sta [1] = 1;
Top = 2;
flag [1] = false;
stab = 1;
for (I = 2; I getpoint (I, f);
stab = top;
for (I = n-2; I> = 0; I --)
If (flag [I])
getpoint (I, f);
}

Double polygon: length ()
{
Double S = 0.0;
Int I;
For (I = 1; I <top; I ++)
S + = SQRT (1.0 * (point [sta [I]. x-point [sta [I-1]. x) * (point [sta [I]. x-point [sta [I-1]. x) + (point [sta [I]. y-point [sta [I-1]. y) * (point [sta [I]. y-point [sta [I-1]. Y ));
Return S;
}
Double polygon: Area ()
{
Double S = 0.0;
Int I;
For (I = 1; I <top; I ++)
S + = point [sta [I-1]. x * point [sta [I]. y-point [sta [I]. x * point [sta [I-1]. y;
Return FABS (S/2 );
}

Bool polygon: isinpoly (Double X, Double Y, bool f) // Double Type
{
Int I;
For (I = 1; I <top; I ++)
If (! X (x-point [sta [I-1]. x, Y-point [sta [I-1]. y, point [sta [I]. x-point [sta [I-1]. x, point [sta [I]. y-point [sta [I-1]. y, f ))
Return false;
Return true;
}

Int main ()
{
Int I, J, K;
While (scanf ("% d", & tubao. N )! = EOF)
{
For (I = 0; I <tubao. N; I ++)
{
Scanf ("% lf", & tubao. Point [I]. X, & tubao. Point [I]. y );
}
If (tubao. n = 3) // I don't know why my convex hull template cannot handle three points ..
{
Double G;
Double X1 = tubao. Point [0]. x-Tubao.point [1]. X;
Double Y1 = tubao. Point [0]. y-Tubao.point [1]. Y;
Double X2 = tubao. Point [0]. x-Tubao.point [2]. X;
Double y2 = tubao. Point [0]. y-Tubao.point [2]. Y;
G = FABS (x1 * y2-x2 * Y1)/2.0 );
Printf ("%. 2lf \ n", G );
}
Else
{
Tubao. pointselect (0); // The vertices on the convex hull may be three points in the same line... A problem that is the same as shit. It took me two days .. Dizzy ..
Double ans = 0, ans1 = 0;
Int n = 2;
For (I = 0; I <tubao. Top; I ++)
{
Ans =-1;
N = (I + 2) % tubao. Top;
For (j = I + 1; j <tubao. Top; j ++)
{
While (tubao. xmult (tubao. point [tubao. sta [I], tubao. point [tubao. sta [J], tubao. point [tubao. sta [n + 1])> tubao. xmult (tubao. point [tubao. sta [I], tubao. point [tubao. sta [J], tubao. point [tubao. sta [N])
N = (n + 1) % tubao. Top;
Ans = tubao. max (ANS, tubao. xmult (tubao. point [tubao. sta [I], tubao. point [tubao. sta [J], tubao. point [tubao. sta [N]);
}
If (ans1 <ans)
Ans1 = ans;
}
Printf ("%. 2lf \ n", ans1 * 0.5 );

}
}
Return 0;
}

/*
Enter the coordinates of N points to calculate the maximum Triangle Area.

Obviously, the triangle with the largest area must be on the convex hull of the Point Set. Therefore, the convex hull is obtained first. If the enumeration of a convex hull directly times out, you can use a method similar to the diameter of the convex hull to find the triangle with the maximum area and use the rotary jamming method.

Enumerate the first vertex I of a triangle;

Then the second vertex J = I + 1 and the third vertex K = J + 1;

Loop k + 1 until area (I, j, k)> area (I, j, k + 1), update the maximum area

Rotate J and K points

(1) If area (I, j, k) <area (I, j, k + 1) and k! = I, then K = k + 1, otherwise 2

(2) update area J = J + 1. If J = I, skip the cycle

In this way, the area obtained by rotating for one week is the maximum Triangle Area with I as the vertex.

# Define n 50005

# Define max (A, B) A> B? A: B

# Include <iostream>

# Include <algorithm>

Using namespace STD;

Struct Node

{

Int X, Y;

} Dd [N];

Int N, stak [N], top, top1;

Bool CMP (node A, Node B)

{

Return A. x <B. x | (A. X = B. X & A. Y <B. Y );

}

Bool judge_right (int o, int A, int B)

{

Int AX = dd [A]. X-dd [O]. X;

Int BX = dd [B]. X-dd [O]. X;

Int ay = dd [A]. Y-dd [O]. Y;

Int by = dd [B]. Y-dd [O]. Y;

Return (_ int64) BX * Ay> (_ int64) ax *;

}

Double area (int o, int A, int B)

{

Int AX = dd [A]. X-dd [O]. X;

Int BX = dd [B]. X-dd [O]. X;

Int ay = dd [A]. Y-dd [O]. Y;

Int by = dd [B]. Y-dd [O]. Y;

Return ABS (BX * Ay-Ax * by) * 1.0/2.0;

}

Void build_map ()

{

Int I;

Top = 0;

Sort (DD, DD + N, CMP );

Stak [top ++] = 0;

Stak [top ++] = 1;

For (I = 2; I <n; I ++)

{

Stak [top ++] = I;

While (top> = 3)

{

If (judge_right (stak [Top-3], stak [Top-2], stak [Top-1])

Break;

Stak [Top-2] = stak [Top-1];

Top --;

}

}

Top1 = top;

Stak [top ++] = n-2;

For (I = n-3; I> = 0; I --)

{

Stak [top ++] = I;

While (top-top1> = 2)

{

If (judge_right (stak [Top-3], stak [Top-2], stak [Top-1])

Break;

Stak [Top-2] = stak [Top-1];

Top --;

}

}

Top --;

}

Int main ()

{

Int I, J, K;

While (scanf ("% d", & N), n! =-1)

{

For (I = 0; I <n; I ++)

Scanf ("% d", & dd [I]. X, & dd [I]. y );

Build_map ();

Double ans = 0;

For (I = 0; I <top; I ++)

{

J = (I + 1) % top;

K = (J + 1) % top;

While (K! = I & Area (stak [I], stak [J], stak [k]) <area (stak [I], stak [J], stak [(k + 1) % top])

K = (k + 1) % top;

If (k = I)

Continue;

Int KK = (k + 1) % top;

While (J! = KK & K! = I)

{

Ans = max (ANS, area (stak [I], stak [J], stak [k]);

While (K! = I & Area (stak [I], stak [J], stak [k]) <area (stak [I], stak [J], stak [(k + 1) % top])

K = (k + 1) % top;

J = (J + 1) % top;

}

}

Printf ("%. 2lf \ n", ANS );

}

Return 0;

}
 
*/

Related Article

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.