acm-GoroSort

来源:互联网 发布:火炬之光2for mac汉化 编辑:程序博客网 时间:2024/04/27 07:11

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 arrayof 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 downseveral elements of the array and hit the table with his third andfourth fists as hard as possible. This will make the unsecuredelements 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 hitswill it take Goro to sort the given array, on average, if he actsintelligently when choosing which elements of the array to holddown before each hit of the table? Goro has an infinite number offingers on the two hands he uses to hold down the array.

More precisely, before each hit, Goro may choose any subset of theelements of the array to freeze in place. He may choose differentlydepending on the outcomes of previous hits. Each hit permutes theunfrozen elements uniformly at random. Each permutation is equallylikely.

输入
The first line of the input gives the number of test cases, T. Ttest cases follow. Each one will consist of two lines. The firstline will give the number N. The second line will list the Nelements of the array in their initial order.
1 ≤ T ≤ 100;
The second line of each test case will contain a permutation of theN smallest positive integers.
1 ≤ N ≤ 1000;
输出
For each test case, output one line containing "Case #x: y", wherex is the case number (starting from 1) and y is the expected numberof hit-the-table operations when following the best hold-downstrategy. Answers with an absolute or relative error of at most10-6 will be considered correct.
样例输入
322 131 3 242 1 4 3
样例输出
Case #1: 2.000000Case #2: 2.000000Case #3: 4.000000
提示
In test case #3, one possible strategy is to hold down the twoleftmost elements first. Elements 3 and 4 will be free to move.After a table hit, they will land in the correct order [3, 4] withprobability 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 thecorrect order. After that, Goro can hold down elements 3 and 4 andhit the table until 1 and 2 land in the correct order, which willtake another 2 hits, on average. The total is then 2 + 2 = 4hits.
来源
Google Code Jam 2011 资格赛
我的代码:
#include
int main()
{
 int t;
 int ch=1;
 scanf("%d",&t);
 while(ch<=t)
 {
  int n;
  int a;
  scanf("%d",&n);
  int count=0;
  for(int i=0;i
  {
   scanf("%d",&a);
   if(a!=i+1)
    count++;
  }
  printf("Case #%d: ",ch);
  ch++; 
  printf("%d.000000\n",count);
 }
 return 0;
}