TopCoder problem "ElectronicScarecrows" used in SRM 173 (Division I Level Three)

Problem Statement


This problem contains an image. If you don't see it, view the problem description in the applet instead.

In the future, farmers won't have to rely on primitive scarecrows to keep the birds away from their crops. A new revolutionary invention, an "electronic scarecrow", will make sure the birds stay out of the farmers' fields forever.

If you have three electronic scarecrows located on a field so that they form a triangle, a bird won't be able to fly inside this area because it will get zapped by a laser beam coming out from the scarecrows. Of course, if you have more scarecrows, the whole area that is surrounded by these scarecrows becomes protected (this area is known as the convex hull). Consider the picture below:

The black dots represents electronic scarecrows and the area shaded gray is the part of the field that is inaccessible to the birds.

This sounds great, but there are two drawbacks. First, the scarecrows are of course very expensive, so a farmer can't afford very many of them. Second, they are quite heavy and need firm soil to stand on, and must also be in range of a power outlet. This severely limits the number of locations the farmer can place such scarecrows.

Given the coordinates of possible locations for the scarecrows and the maximum number of scarecrows the farmer can afford to buy, calculate the largest area that can be guarded by these scarecrows. The farmer's field is a rectangular area, and all locations given will be inside this area.

Create a class ElectronicScarecrows containing the method largestArea which takes a int[] x and a int[] y, the coordinates of possible locations for the scarecrows (element i in x and y correspond to the coordinates of the ith location) and an int n, the maximum number of scarecrows the farmer can afford to buy. The method should return a double containing the largest area the scarecrows can cover. You may safely assume that it will always be possible to put the scarecrows so they will cover an area strictly greater than 0.



Parameters:int[], int[], int
Method signature:double largestArea(int[] x, int[] y, int n)
(be sure your method is public)


-The area of a simple polygon with corners at (x1,y1), (x2,y2), ..., (xm,ym) - in clockwise or counterclockwise direction - is the absolute value of ((x1*y2 - x2*y1) + (x2*y3 - x3*y2) + ... + (xm*y1 - x1*ym))/2
-A solution will be judged correct if the absolute or relative error is within 1e-9.


-x will contain between 3 and 40 elements, inclusive.
-y will contain between 3 and 40 elements, inclusive.
-x will contain the same number of elements as y.
-Each element in x will be between 0 and 1000, inclusive.
-Each element in y will be between 0 and 1000, inclusive.
-No location will appear more than once.
-n will be between 3 and 40, inclusive.
-It will be possible to select the coordinates so that an area strictly greater than 0 is covered.


Returns: 24.0
Selecting the points (2,2), (6,1), (9,4) and (3,7) will yield an area of 24 (this corresponds to the picture in the figure above).
Returns: 347200.0
The farmer can afford more scarecrows than there are possible locations for him to put them.
Returns: 740.5
Returns: 685819.5

Problem url:

Problem stats url:




lbackstrom , brett1479

Problem categories:

Dynamic Programming, Geometry