TopCoder problem "Crossroads" used in SRM 217 (Division II Level Three)



Problem Statement

    You are watching a car race. Each car starts a different point on the x-axis, travels at the same speed, and starts at the same time. However, each car is travelling along a different road (which extends to infinity in one direction, and is stopped by the x-axis in the other direction), and each road has its own direction specified by an angle between 1 and 179, inclusive. An angle of 90 indicates that the road heads directly in the positive y direction, while an angle of 1 indicates that the road heads just a little bit north of the positive x direction. Note that cars never head in the negative y direction.







Sometimes, two or more roads intersect at some point. When this happens, the car that reaches the intersection first is able to block the intersection so that no other cars can pass through it. If two cars reach an intersection at the same time, the one with the lower index passes, while the other one is blocked.





In this picture, the cars following the red paths make it through all of the intersections, while the cars on the gray paths are blocked.

You will be given a int[] angles, where the ith element of angles is the angle between the x-axis and the road that the ith car drives on, in degrees. The order of the elements of angles corresponds to the order of the cars along the x-axis (no two cars start at the exact same location), with the first element of angles corresponding to the car with the leftmost starting position on the x-axis.



Your method should return a int[] containing the 0-based indices of all the cars that will pass all of the intersections along their roads. Your return should be sorted in ascending order.



Note that the exact locations of the cars along the x-axis do not matter. All that matters is their order, and the directions in which they head.
 

Definition

    
Class:Crossroads
Method:getOut
Parameters:int[]
Returns:int[]
Method signature:int[] getOut(int[] angles)
(be sure your method is public)
    
 

Constraints

-angles will contain between 1 and 50 elements, inclusive.
-Each elemtent of angles will be between 1 and 179, inclusive.
 

Examples

0)
    
{105, 75, 25, 120, 35, 75}
Returns: { 0,  1,  5 }
The example from the problem statement.



1)
    
{1, 1, 1, 1, 1, 1}
Returns: { 0,  1,  2,  3,  4,  5 }
No two cars' paths will ever intersect, so they all pass all intersections.
2)
    
{13}
Returns: { 0 }
Only one car.
3)
    
{90, 123, 1, 23, 132, 11, 28, 179, 179, 77, 113, 91}
Returns: { 0 }
The first car passes all intersections. The last car will be stopped by the first car. All other cars are between those two, and will be stopped by one of them.
4)
    
{179, 89, 90, 91, 1}
Returns: { 0,  2,  4 }
Neither the first nor the last car will intersect with any other car. Car 1 and car 3 will both be stopped by car 2.
5)
    
{89, 91}
Returns: { 0 }
Both cars reach the intersection at the same time, and hence only the first one passes.
6)
    
{140, 118, 54, 166, 151, 44, 90, 5, 14, 6,
 64, 129, 74, 33, 134, 25, 11, 95, 65, 145,
 29, 162, 24, 147, 45, 103, 63, 97, 120, 156,
 167, 170, 19, 28, 100, 144, 161, 13, 157, 148}
Returns: { 0,  1,  6 }

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5863&pm=3042

Writer:

Olexiy

Testers:

PabloGilberto , lbackstrom , brett1479

Problem categories:

Geometry, Greedy, Simple Math