nyoj——297(期望)
GoroSort
- 描述
-
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(期望)的更多相关文章
- NYOJ 1007
在博客NYOJ 998 中已经写过计算欧拉函数的三种方法,这里不再赘述. 本题也是对欧拉函数的应用的考查,不过考查了另外一个数论基本定理:如何用欧拉函数求小于n且与n互质所有的正整数的和. 记eule ...
- NYOJ 998
这道题是欧拉函数的使用,这里简要介绍下欧拉函数. 欧拉函数定义为:对于正整数n,欧拉函数是指不超过n且与n互质的正整数的个数. 欧拉函数的性质:1.设n = p1a1p2a2p3a3p4a4...pk ...
- 【BZOJ-3143】游走 高斯消元 + 概率期望
3143: [Hnoi2013]游走 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2264 Solved: 987[Submit][Status] ...
- bzoj1415[NOI2005]聪聪和可可-期望的线性性
这道题之前我写过一个巨逗比的写法(传送门:http://www.cnblogs.com/liu-runda/p/6220381.html) 当时的原因是这道题可以抽象出和"绿豆蛙的归宿&qu ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- 【BZOJ3036】绿豆蛙的归宿 概率与期望
最水的概率期望,推荐算法合集之<浅析竞赛中一类数学期望问题的解决方法> #include <iostream> #include <cstdio> using na ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- NYOJ 333
http://www.cppblog.com/RyanWang/archive/2009/07/19/90512.aspx?opt=admin 欧拉函数 E(x)表示比x小的且与x互质的正整数的个数. ...
- 【BZOJ-1426】收集邮票 概率与期望DP
1426: 收集邮票 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 261 Solved: 209[Submit][Status][Discuss] ...
随机推荐
- XDU 1001 又是苹果(状态压缩)
#include<cstdio> #include<cstring> ; using namespace std; int r[maxn],c[maxn]; char pic[ ...
- HDU 2191 珍惜现在,感恩生活(多重背包模板题)
多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...
- JForum的运行环境
JForum的运行环境: 开始本文之前,我们确认一下JForum的运行环境. - Java动态运行环境(JRE) - 支持J2EE Servlet标准的任何一款Web服务器:Tomcat,JBoss, ...
- POJ - 2125 Destroying The Graph (最小点权覆盖)
题意:给一张图,现在要删去所有的边,删去一个点的所有入边和所有出边都有其对应\(W_{i+}\)和\(W_{i-}\).求删去该图的最小花费,并输出解 分析:简而言之就是用最小权值的点集去覆盖所有的边 ...
- java synchronized关键字的底层实现
每个对象都有一个锁(Monitor,监视器锁),class对象也有锁,如果synchronized关键字修饰同步代码块,通过反编译可以看到,其实是有个monitorenter和monitorexit指 ...
- SpringBoot入门学习(三)
基于第二讲,这一讲我们主要讲解包含以下内容 springBoot添加对freemarker的支持 使用@RestController处理ajax请求 使用@PathVariable注解获取url参数 ...
- oracle同一个库上面,不同用户相互赋予权限
用法: 有两个用户:user1和user2,都是在库TEST上,分别有表user1.table1,user2.table2 但是用user1登录的时候,user2上表就不能用,此时就可以使用grant ...
- 【c++ primer, 5e】函数声明 & 分离式编译
p186~p188: 函数声明1.函数只能定义一次,但是可以声明多次. 2.函数的接口:返回类型 + 函数名 + 形参类型 3.为什么要在头文件中进行函数声明???在源文件中定义?暂时理解到,这么做可 ...
- 【c++ primer, 5e】函数匹配
练习 6.49 候选函数:与所调用的函数的名字相同的函数的集合. 可行函数:给候选函数加上参数数量.参数类型的约束所得到的函数的集合. 6.50 a 3.4可行,二义匹配 b 2.4可行,2是最佳匹配 ...
- react-refetch的使用小例子
出处:<react设计模式和最佳实践> 作者:米凯莱·贝尔托利 出版时间:2018年8月第1版(还算新) 使用react-refetch来简化api获取数据的代码 const List = ...