TopcoderArchive
Archive
Algorithm problem

MarketQueue

Problem statement, definition, constraints, and public examples.

Problem Statement

Customers are standing in the checkout line at the market, waiting for cashiers to become available. When a cashier is available, the first customer in line starts getting served. If more than one cashier is available, the customer goes to the first of them. After having all their items processed, the customer pays and exits the market (we consider this to happen instantly).

Customers buy different amounts of groceries and cashiers work at different paces. Because of this, a customer may exit the market later than someone who was behind them in line.

You will be given two int[]s, customerItems and timePerItem. Each element of customerItems is the number of items a customer has in his cart. The customers will be given in the order in which they are standing in line. Each element of timePerItem corresponds to one cashier and represents the number of seconds needed for that cashier to process one grocery item.

Return the number of customers that exit the market strictly later than someone who was behind them in line.

Definition

Class:
MarketQueue
Method:
anomalies
Parameters:
int[], int[]
Returns:
int
Method signature:
int anomalies(int[] customerItems, int[] timePerItem)
(be sure your method is public)

Constraints

  • customerItems will contain between 1 and 50 elements, inclusive.
  • Each element of customerItems will be between 1 and 100, inclusive.
  • timePerItem will contain between 1 and 10 elements, inclusive.
  • Each element of timePerItem will be between 1 and 100, inclusive.

Examples

  1. { 3, 4 }
    { 5, 5 }
    Returns: 0
    Both customers get served immediately. Customer 0 goes to the first cashier who needs 5 seconds to process each of the customer's 3 items, so the customer exits after 15 seconds. Customer 1 goes to the second cashier and exits after 4*5 = 20 seconds.
  2. { 3, 4 }
    { 10, 7 }
    Returns: 1
    This time customer 1 finishes after 28 seconds, before customer 0 who finishes after 30 seconds.
  3. { 3, 3, 15, 1 }
    { 5, 5, 2 }
    Returns: 1
    Customer 3 gets done before customer 2.
  4. { 89, 28, 13, 94, 82, 1, 88, 88, 16, 7, 83 }
    { 25, 96, 90, 96, 10 }
    Returns: 5
    Customers 0, 1, 2, 3 and 7 exit later than someone who was behind them in line.
Back to all problems