nyoj——297(期望)
GoroSort
時間限制:3000?ms ?|? 內存限制:65535?KB 難度:4- 描述
-
Goro has 4 arms. Goro is very strong. You don't mess with Goro. Goro needs to sort an array of?N?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, T. T test cases follow. Each one will consist of two lines. The first line will give the number N. The second line will list the N elements of the array in their initial order.
1 ≤ T ≤ 100;
The second line of each test case will contain a permutation of the N smallest positive integers.
1 ≤ N ≤ 1000; 輸出 - For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y 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 10-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
提示 - 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] with probability 1/2 and in the wrong order [4, 3] with probability 1/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 = 4 hits. 來源
- Google Code Jam 2011 資格賽 上傳者
- 張云聰
-
#include "bits/stdc++.h" using namespace std;int main() {int t;scanf("%d",&t);int k = 1;while(t--){int n;scanf("%d",&n);int cnt = 0;int x;for(int i=1;i <= n;i++){scanf("%d",&x);if(i != x) cnt++; //如果位置不是本來的位置就加1}cout << "Case #" << k++ << ": " << cnt << ".000000" << endl;}return 0; }
大概意思就是?假設N個數組,里面全部都是沒有排序好的,那么拍一次,對于數組中任意的數字,拍一次,它落回正確位置的概率為1/N。假設,拍完一次,有I個數字落回了原來的位置,那么對于沒有落回原來位置的數字肯定沒有落在這I個數字的位置上,如果落在了這I個數字的上面,則這I個數字肯定就是錯誤的,因此概率為(N-I)/N,接下來,按住I個正確的,拍一次,落回原來位置的概率為1/N-I,兩者相乘的概率依然為1/N,因此一個數組正確排序的期望為整個數組中沒有正確排序的數字。
轉載于:https://www.cnblogs.com/cunyusup/p/8497116.html
總結
以上是生活随笔為你收集整理的nyoj——297(期望)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql事务处理用法与实例详解
- 下一篇: 数学界牛人