TopcoderArchiveVisit Topcoder
SRM30 Aug 2017
SRM 721

FlightDataRecorder

Problem statement, definition, constraints, and public examples.

Problem Statement

A Flight Data Recorder is a device that records lots of parameters during a flight. In this task you are going to use some of those parameters to calculate the distance between the start point and the end point of the flight.

The whole flight consists of one or more segments. During each segment the plane was heading in a fixed direction. For each segment, you are given the direction and the distance traveled. More precisely, you are given two int[]s: heading and distance. For each valid i: during segment i of the flight the plane traveled in the direction heading[i] and the distance it traveled was distance[i]. The heading is an angle in degrees between North and the direction of flight, measured in clockwise direction. For example, 0 means North, 90 means East, 180 means South, and 270 means West.

Assume that the airplane is a point moving in a two-dimensional plane. Also, assume that between the segments the airplane can change its heading in an instant.

Calculate and return the distance between the start and the end of the flight.

Definition

Class:
FlightDataRecorder
Method:
getDistance
Parameters:
int[], int[]
Returns:
double
Method signature:
double getDistance(int[] heading, int[] distance)
(be sure your method is public)

Notes

  • Your answer will be considered correct if its absolute or relative error does not exceed 10^(-9).

Constraints

  • heading will contain between 1 and 50 elements, inclusive.
  • heading and distance will contain the same number of elements.
  • Each element in heading will be between 0 and 359, inclusive.
  • Each element in distance will be between 1 and 1,000, inclusive.

Examples

  1. {90,0}
    {3,4}
    Returns: 5.0
    The airplane flew 3 units of distance towards the East and then 4 units of distance towards the North. From the Pythagorean theorem we know that the distance between the start and the end of the flight is sqrt(3*3 + 4*4) = 5.
  2. {37,37,37,37}
    {1,10,100,1000}
    Returns: 1111.0
    The heading never changed, so the answer is 1+10+100+1000.
  3. {0,120,240,0,120,240}
    {999,999,999,999,999,999}
    Returns: 6.431098710768743E-13
    The airplane returned back to the start point.
  4. {76,321,214,132,0,359,74,65,213}
    {621,235,698,1,35,658,154,426,965}
    Returns: 153.54881555325184
  5. {0}
    {1}
    Returns: 1.0
Back to all problems