PlaylistShuffler
TCO11 Round 5 · 2011-05-07 · by ivan_metelsky
Problem Statement
The player has a "Shuffle" button which changes the order of songs in the playlist as follows:
- The user is asked to enter an integer K, which must be between K1 and K2, inclusive.
- The player generates a random permutation P = (p[0], p[1], ..., p[N-1]) of integers between 0 and N-1, inclusive. All possible permutations have the same probability of being generated.
- The generated permutation P is applied to the playlist exactly K times. Each application works as follows: for each i, 0 <= i < N, the song at position i of the playlist is moved to position p[i] (these movements occur simultaneously for all N songs).
Constraints
- N will be between 1 and 50, inclusive.
- X will be between 0 and N-1, inclusive.
- Y will be between 0 and N-1, inclusive.
- K1 will be between 1 and 10,000,000,000 (10^10), inclusive.
- K2 will be between K1 and 10,000,000,000 (10^10), inclusive.
2 0 1 2 4 Returns: 3
Suppose that the songs are called A and B and the initial order is A, B. If the generated permutation is {0, 1}, then the list is just left intact no matter what the value of K is. If the permutation is {1, 0}, the list keeps changing between A, B and B, A consecutively. We want to maximize the probability that the final list is B, A. For K = 2 and K = 4 this probability is 0, and for K = 3 the probability is 0.5.
2 1 1 1 10 Returns: 2
This time we want the final list to be A, B. This happens with probability 1 for every even value of K. The smallest one among them is 2.
1 0 0 1111111111 9999999999 Returns: 1111111111
Since there is just 1 song, all values of K lead to the same final result.
10 3 7 1000 10000 Returns: 1003
1 0 0 1 10000000000 Returns: 1
Submissions are judged against all 211 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class PlaylistShuffler with a public method long long bestShuffle(int N, int X, int Y, long long K1, long long K2) · 211 test cases · 2 s / 256 MB per case