eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Crusher`s Code

Crusher`s Code

Wesley Crusher is the teaching assistant for Introduction to Algorithms. During his rst class, the cadets were asked to come up with their own sorting algorithms. Monty came up with the following code: \textbf{while (!sorted(a)) \{} \textbf{int i = random(n);} \textbf{int j = random(n);} \textbf{if (a\[min(i,j)\] > a\[max(i,j)\])} \textbf{swap(a\[i\], a\[j\]);} \textbf{\}} Carlos, inspired, came up with the following code: \textbf{while (!sorted(a)) \{} \textbf{int i = random(n-1);} \textbf{int j = i + 1;} \textbf{if (a\[i\] > a\[j\])} \textbf{swap(a\[i\], a\[j\]) ;} \textbf{\}} Wesley needs to determine which algorithm is better. For a given input array of up to \textbf{8} values, calculate and print the expected number of iterations for each algorithm. That is, on average, how many iterations should each algorithm take for the given input? \InputFile The first line contains \textbf{T}, the number of test cases: \textbf{2} ≤ \textbf{T} ≤ \textbf{100}. Each test case is given on a single line. The first value is \textbf{N}, the number of array elements; \textbf{2} ≤ \textbf{N} ≤ \textbf{8}. This is followed on the same line by \textbf{N} integer array elements. The array elements will have values between \textbf{0} and \textbf{100} inclusive. The array elements may not be distinct. \OutputFile For each test case, print out the expected number of iterations for Monty's algorithm and for Carlos's algorithm, as shown in the sample output section. There should be exactly one space between words and no spaces at the start of each line or at the end of each line. There should be exactly six digits after the decimal point. Rounding should be to nearest representable value.
Ліміт часу 20 секунд
Ліміт використання пам'яті 128 MiB
Вхідні дані #1
12
2 1 2
2 2 1
3 1 2 3
3 3 2 1
4 1 2 3 4
4 4 3 2 1
4 2 1 4 3
5 1 1 1 1 1
5 5 4 3 2 1
8 8 7 6 5 4 3 2 1
8 3 1 4 1 5 9 2 6
8 2 7 1 8 2 8 1 8
Вихідні дані #1
Monty 0.000000 Carlos 0.000000
Monty 2.000000 Carlos 1.000000
Monty 0.000000 Carlos 0.000000
Monty 6.000000 Carlos 5.000000
Monty 0.000000 Carlos 0.000000
Monty 14.666667 Carlos 12.500000
Monty 12.000000 Carlos 4.500000
Monty 0.000000 Carlos 0.000000
Monty 26.382275 Carlos 23.641975
Monty 89.576273 Carlos 79.496510
Monty 79.161905 Carlos 33.422840
Monty 63.815873 Carlos 38.910494
Джерело 2013 North America - Pacific Northwest Region Programming Contest, Задача C