TopcoderArchive
Archive
Algorithm problem

ConnectToCell

Problem statement, definition, constraints, and public examples.

Problem Statement

When a user turns on his cellular phone, the phone must be connected to one of the cell towers. You will be given two int[]s - x and y, giving you the cartesian coordinates for all cell towers. You will be also given a int[] power, representing powers for all cells (ith elements of x, y and power represent a single tower). Assuming that the phone is located at point (0, 0), you must find the 0-based index of the nearest tower. In case of a tie, the most powerful one must be chosen. If the tie still exists, choose the cell tower with the smallest index.

Definition

Class:
ConnectToCell
Method:
findNearest
Parameters:
int[], int[], int[]
Returns:
int
Method signature:
int findNearest(int[] x, int[] y, int[] power)
(be sure your method is public)

Notes

  • In the plane, the distance between points (x1, y1) and (x2, y2) is equal to sqrt((x1 - x2)2 + (y1 - y2)2), where sqrt stands for a square root.

Constraints

  • x, y and power will all have the same number of elements.
  • x, y and power will each contain between 2 and 50 elements, inclusive.
  • All elements of x and y will be between -10000 and 10000, inclusive.
  • All elements of power will be between 1 and 10000, inclusive.
  • No two cells will be located at the same point.

Examples

  1. {0, 1, 3}
    {0, 2, 0}
    {100, 10, 10}
    Returns: 0
    You are located next to the first tower.
  2. {1, -1, 0, 0}
    {0, 0, 1, -1}
    {10, 10, 10, 10}
    Returns: 0
    All cell towers are of the same distance and of the same power. You should return the one with the minimal index.
  3. {6448, 1695, -4334, -1982, -438,
     3038, -7432, 8133, 3284, -6925,
     8610, -3168, 8180, -6425, -6918,
     4468, -4508}
    {-6853, -5381, -4458, -9325, 753,
     -7686, 5584, -5262, 1125, -9749,
     7782, -8640, 8578, 3901, 8118,
     8384, 2036}
    {5753, 9667, 3253, 3452, 5535,
     5891, 8547, 2416, 7118, 703,
     5087, 5959, 4237, 4250, 7622,
     8205, 1083}
    Returns: 4
  4. {10000,9999}
    {9998,9999}
    {2,1}
    Returns: 1
    Tower 1 is ever so slightly closer.
Back to all problems