TopcoderARCHIVE
Archive/Problems/ProbabilisticStreamMinimum
SRM · Problem 17371

ProbabilisticStreamMinimum

Problem statement, definition, constraints, and public examples.

Problem Statement

N elements are going to appear on an input stream. The elements are all comparable, the comparisons are transitive, and the elements are distinct. Hence, the elements have a unique sorted order.

We would like to select the K smallest of these elements.


We are going to do this using a randomized algorithm. We will take K^2 buckets. Each time we process an element, we will assign it a bucket uniformly at random. If the bucket is empty, the element is placed into the bucket. If the bucket already contains another item, we compare the two and keep the smaller one. After processing the entire stream, we pool the items from all buckets and select the K smallest among them.

You do not know anything about the order in which the N items will appear in the input. In particular, it is not guaranteed that they will be in a random order.


Given N and K, calculate the worst-case probability of the above algorithm returning the correct answer.

(That is, imagine that for each possible order of the N items in the input stream we have calculated the probability of the algorithm giving the right answer. You should return the minimum of all those probabilities.)

Definition

Class:
ProbabilisticStreamMinimum
Method:
calculate
Parameters:
int, int
Returns:
double
Method signature:
double calculate(int N, int K)
(be sure your method is public)

Notes

  • As usual, assume that all random choices made by the algorithm are mutually independent.
  • Your answer will be accepted if it has an absolute error not exceeding 1e-9.

Constraints

  • N will be between 1 and 10^6, inclusive.
  • K will be between 1 and min( N, 100 ), inclusive.

Examples

  1. 47
    1
    Returns: 1.0
    The algorithm is guaranteed to work. The minimum element will end in one of the buckets.
  2. 3
    3
    Returns: 0.691358024691358
  3. 20
    4
    Returns: 0.66650390625
    One possible execution of the algorithm starts as follows: An item of size 14576 arrives and it is placed into an empty bucket #13. An item of size 54245 arrives and it is placed into an empty bucket #7. An item of size 81234 arrives and again bucket #7 is chosen. As this item is bigger than the one already in bucket #7, we discard it. Bucket #7 still contains the item of size 54245. An item of size 9127 arrives and it is placed into an empty bucket #2. An item of size 23 arrives and again bucket #13 is chosen. As this item is smaller than the one in the bucket, we discard the item of size 14576 and instead we place the item of size 23 into bucket #13. ... followed by another 15 steps similar to these. The probability we seek is almost, but not exactly, equal to 2/3.
← All problems