TopcoderArchive
Archive
Algorithm problem

SeesawFulcrum

Problem statement, definition, constraints, and public examples.

Problem Statement

A bunch of kids want to sit on the seesaw. Kid i (0-based) has weight ws[i] and will sit ps[i] units from the middle (on either side). You must determine who will sit on the seesaw, and which side each seated kid will occupy. The maximum number of kids must be seated without having two kids sitting at the same position. Each seating arrangement has an associated torque difference between the two sides. Return the smallest possible non-negative difference (see notes for how to calculate torque).

Definition

Class:
SeesawFulcrum
Method:
getMin
Parameters:
int[], int[]
Returns:
int
Method signature:
int getMin(int[] ws, int[] ps)
(be sure your method is public)

Notes

  • A child of weight w sitting m units to the left of p will exert a counterclockwise torque of w*m. Similarly, a child on the right would exert a clockwise torque of w*m. The torque difference is the difference between the clockwise and counterclockwise torques.
  • The "maximum number of kids must be seated" constraint cannot be violated, even if violating it will lower the return value.

Constraints

  • ws will contain between 1 and 50 elements inclusive.
  • Each element of ws will be between 1 and 100 inclusive.
  • ps will contain the same number of elements as ws.
  • Each element of ps will be between 1 and 100 inclusive.

Examples

  1. {20, 20}
    {5, 5}
    Returns: 0
    Two kids of equal weight want to sit 5 meters from the middle. Putting them one on each side leads to an ideal situation.
  2. {5, 10, 25, 35}
    {10, 10, 10, 10}
    Returns: 50
    10 units from the middle seems to be a popular distance. We can only seat 2 of the kids, so we choose the ones weighing 5 and 10.
  3. {1}
    {1}
    Returns: 1
  4. {100,100,100}
    {100,99,98}
    Returns: 9700
Back to all problems