#P13364. [GCJ 2011 Qualification] GoroSort

    ID: 15231 远端评测题 3000~6000ms 1024MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>数学2011Special Judge期望Google Code Jam

[GCJ 2011 Qualification] GoroSort

题目描述

Goro has 4 arms. Goro is very strong. You don't mess with Goro. Goro needs to sort an array of NN different integers. Algorithms are not Goro's strength; strength is Goro's strength. Goro's plan is to use the fingers on two of his hands to hold down several elements of the array and hit the table with his third and fourth fists as hard as possible. This will make the unsecured elements of the array fly up into the air, get shuffled randomly, and fall back down into the empty array locations.

Goro wants to sort the array as quickly as possible. How many hits will it take Goro to sort the given array, on average, if he acts intelligently when choosing which elements of the array to hold down before each hit of the table? Goro has an infinite number of fingers on the two hands he uses to hold down the array.

More precisely, before each hit, Goro may choose any subset of the elements of the array to freeze in place. He may choose differently depending on the outcomes of previous hits. Each hit permutes the unfrozen elements uniformly at random. Each permutation is equally likely.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. Each one will consist of two lines. The first line will give the number NN. The second line will list the NN elements of the array in their initial order.

输出格式

For each test case, output one line containing "Case #xx: yy", where xx is the case number (starting from 1) and yy is the expected number of hit-the-table operations when following the best hold-down strategy. Answers with an absolute or relative error of at most 10610^{-6} will be considered correct.

3
2
2 1
3
1 3 2
4
2 1 4 3
Case #1: 2.000000
Case #2: 2.000000
Case #3: 4.000000

提示

Sample Explanation

In test case #3, one possible strategy is to hold down the two leftmost elements first. Elements 3 and 4 will be free to move. After a table hit, they will land in the correct order [3,4][3, 4] with probability 1/21/2 and in the wrong order [4,3][4, 3] with probability 1/21/2. Therefore, on average it will take 2 hits to arrange them in the correct order. After that, Goro can hold down elements 3 and 4 and hit the table until 1 and 2 land in the correct order, which will take another 2 hits, on average. The total is then 2+2=42 + 2 = 4 hits.

Limits

  • 1T1001 \leq T \leq 100;
  • The second line of each test case will contain a permutation of the NN smallest positive integers.

Small dataset (10 Pts, Test set 1 - Visible)

  • 1N101 \leq N \leq 10;
  • Time limit: 30 3 seconds.

Large dataset (20 Pts, Test set 2 - Hidden)

  • 1N10001 \leq N \leq 1000;
  • Time limit: 60 6 seconds.