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 = ;
while(t--){
int n;
scanf("%d",&n);
int cnt = ;
int x;
for(int i=;i <= n;i++){
scanf("%d",&x);
if(i != x) cnt++; //如果位置不是本来的位置就加1
}
cout << "Case #" << k++ << ": " << cnt << ".000000" << endl; }
return ;
}

大概意思就是 假设N个数组,里面全部都是没有排序好的,那么拍一次,对于数组中任意的数字,拍一次,它落回正确位置的概率为1/N。假设,拍完一次,有I个数字落回了原来的位置,那么对于没有落回原来位置的数字肯定没有落在这I个数字的位置上,如果落在了这I个数字的上面,则这I个数字肯定就是错误的,因此概率为(N-I)/N,接下来,按住I个正确的,拍一次,落回原来位置的概率为1/N-I,两者相乘的概率依然为1/N,因此一个数组正确排序的期望为整个数组中没有正确排序的数字。

nyoj——297(期望)的更多相关文章

  1. NYOJ 1007

    在博客NYOJ 998 中已经写过计算欧拉函数的三种方法,这里不再赘述. 本题也是对欧拉函数的应用的考查,不过考查了另外一个数论基本定理:如何用欧拉函数求小于n且与n互质所有的正整数的和. 记eule ...

  2. NYOJ 998

    这道题是欧拉函数的使用,这里简要介绍下欧拉函数. 欧拉函数定义为:对于正整数n,欧拉函数是指不超过n且与n互质的正整数的个数. 欧拉函数的性质:1.设n = p1a1p2a2p3a3p4a4...pk ...

  3. 【BZOJ-3143】游走 高斯消元 + 概率期望

    3143: [Hnoi2013]游走 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2264  Solved: 987[Submit][Status] ...

  4. bzoj1415[NOI2005]聪聪和可可-期望的线性性

    这道题之前我写过一个巨逗比的写法(传送门:http://www.cnblogs.com/liu-runda/p/6220381.html) 当时的原因是这道题可以抽象出和"绿豆蛙的归宿&qu ...

  5. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  6. 【BZOJ3036】绿豆蛙的归宿 概率与期望

    最水的概率期望,推荐算法合集之<浅析竞赛中一类数学期望问题的解决方法> #include <iostream> #include <cstdio> using na ...

  7. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  8. NYOJ 333

    http://www.cppblog.com/RyanWang/archive/2009/07/19/90512.aspx?opt=admin 欧拉函数 E(x)表示比x小的且与x互质的正整数的个数. ...

  9. 【BZOJ-1426】收集邮票 概率与期望DP

    1426: 收集邮票 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 261  Solved: 209[Submit][Status][Discuss] ...

随机推荐

  1. HALCON里面的一维测量。

    第一步:将图片导入, 拿到图片的名字 和窗口的句柄 第二步:创建一个测量区域.这个测量区域是一个矩形,假设他的名字叫A gen_measure_rectangle2 (TmpCtrl_Row,//输入 ...

  2. 关联规则之Apriori

    1.关联规则原理 1.关联规则概述 关联规则(Association Rules)是反映一个事物与其他事物之间的相互依存性和关联性,如果两个或多个事物之间存在一定的关联关系,那么,其中一个事物就能通过 ...

  3. [笔记]Delphi 2007写DLL供VC调用实例

    考虑如下几种常用情况: - VC传入int,返回int- VC传入char *,返回int- VC传入char *,返回char *及int 为简化问题,传递的字符串参数只考虑ANSI格式,不考虑UN ...

  4. Java HashMap详细介绍和使用示例

    ①对HashMap的整体认识 HashMap是一个散列表,它存储的内容是键值对(key-value)映射. HashMap继承于AbstractMap,实现了Map.Cloneable.java.io ...

  5. shell脚本批量启动zookeeper

    脚本名称为zk_run.sh 将下面代码粘贴进zk_run.sh 添加执行权限 chmode +x zk_run.sh 运行脚本 ./zk_run.sh start 如果发现zookeeper没有启动 ...

  6. Python --之练习题

    一,两个小组对战,对战规则如下:team1 = ['a','b','c']team2 = ['x','y','z'] #a 不和x对战,b 不和y,z 对战# for i in team1: #法一# ...

  7. cmd中测试常用到的命令汇总

    1.最常用的 ping +ip 2.测试到远程服务器之间是否通的命令 telnet +ip +端口 3.路由跟踪,通过跟踪可以找到到哪地方不通 tracert ip      linux系统  tra ...

  8. Spring事务回滚

    配置事物: @Configuration /**强制使用cglib代理时就把proxy-target-class设为true.*/ @EnableTransactionManagement(proxy ...

  9. python 打印 九九表

    用Python 打印九九表. print 每打印一行默认会带有换行, 在print语句后加上,end = 't' 会变成tab. 排版会好点. def main(): for i in range(1 ...

  10. 第三方CSS安全吗?

    原文:https://jakearchibald.com/201...翻译:疯狂的技术宅 本文首发微信公众号:jingchengyideng欢迎关注,每天都给你推送新鲜的前端技术文章 前一段时间,有很 ...