TopCoder problem "VectorPolygon" used in SRM 272 (Division II Level Three)



Problem Statement

    

You will be given some two-dimensional vectors (the i-th element of the int[]s dx and dy represent the i-th vector (dx[i], dy[i])). Using some (or all) of these in some order you may be able to construct a polygon by appending them end to end - i.e., represent each vector as an arrow, and place the arrows such that each arrow begins where the last one ends. In order to get a polygon, the last arrow must end at the beginning of the first arrow. The image below shows an example with three vectors.

The left image shows the three vectors as arrows. We can start with the red vector (2, 2), append the green vector (3, -4) and finally append the blue vector (-5, 2) and we get the triangle in the middle image. We can also append them in a different order, starting again with the red one, but appending first the blue one and finally the green one, getting the triangle in the right image.

You are to find the convex polygon that you can construct using some (or all) of the given vectors in some order that has the maximum area, and return this area. If you cannot construct a convex polygon using the given vectors, return 0.0.

 

Definition

    
Class:VectorPolygon
Method:maxArea
Parameters:int[], int[]
Returns:double
Method signature:double maxArea(int[] dx, int[] dy)
(be sure your method is public)
    
 

Notes

-Your return value must have an absolute or relative error less than 1e-9.
-If two or more parallel (or even identical) vectors are given in the input, you are allowed to use them in a row - i.e., the polygon may have 180 degrees angles, see example 6.
 

Constraints

-dx and dy will each have between 1 and 8 elements, inclusive.
-dx and dy will have the same number of elements.
-Each element of dx and dy will be between -100 and 100, inclusive.
-There will be no zero-vector given, i.e., dx[i] and dy[i] will not be both 0 for any i.
 

Examples

0)
    
{2, 3, -5}
{2, -4, 2}
Returns: 7.0

The example from the problem statement. Both shown triangles have the same area.

1)
    
{2, -3, -5}
{2, 4, 2}
Returns: 0.0

Note that the vectors are directed: this is a similar example as that of the problem statement, but with the green arrow pointing in the opposite direction. We can not construct a polygon using these vectors.

2)
    
{0, 0, 1, -1}
{1, -1, 0, 0}
Returns: 1.0

We can build a unit square using these four unit vectors.

3)
    
{25, 50, 75, 100, -25, -50, -75, -100}
{100, 75, 50, 25, -100, -75, -50, -25}
Returns: 31250.0
4)
    
{100}
{-100}
Returns: 0.0
5)
    
{1, -1, 0}
{0, 1, -1}
Returns: 0.5
6)
    
{0, 1, 1, -2}
{2, -1, -1, 0}
Returns: 2.0
Note that we can append parallel vectors in a row. If we connect all vectors in the order they are given in the input, we get a right triangle with the hypotenuse consisting of the two (1, -1) vectors appended in a row.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=5885

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=8069&pm=5885

Writer:

gepa

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Brute Force, Geometry