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(期望)相关推荐

  1. nyist 297 GoroSort

    http://acm.nyist.net/JudgeOnline/problem.php?pid=297 大意:对于一个数组进行排序.可以按住部分元素,对剩余的元素"洗牌"式操作. ...

  2. [NOI2005]聪聪与可可(期望dp)

    题意:给一张无向图,有一只猫和一只老鼠,猫每秒会向老鼠的方向移动两个单位,若它们的距离为一,那么只会移动一个单位,老鼠会等概率向周围移动一步或不动,求猫抓到老鼠的期望时间. Solution luog ...

  3. 洛谷P4316 绿豆蛙的归宿(期望)

    题意翻译 「Poetize3」 题目背景 随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 题目描述 给出一个有向无环图,起点为1终点为N,每条边都有一个长度,并且从起点出 ...

  4. LightOJ - 1038 Race to 1 Again 基础期望概率 dp

    传送门 刚刚学习期望&概率 我们设数X的期望改变次数为P[X] 如果要求X的期望,很容易想到找x的因子; 可以得到下式  ,cnt为X因子个数,ai为X的因子 可以这么理解,当因子ai为1时, ...

  5. 强化学习(五) - 时序差分学习(Temporal-Difference Learning)及其实例----Sarsa算法, Q学习, 期望Sarsa算法

    强化学习(五) - 时序差分学习(Temporal-Difference Learning)及其实例 5.1 TD预测 例5.1 回家时间的估计 5.2 TD预测方法的优势 例5.2 随机移动 5.3 ...

  6. 强化学习(一)- 强化学习介绍、Markov决策过程和贝尔曼期望方程

    强化学习(英语:Reinforcement learning,简称RL)是机器学习中的一个领域,强调如何基于环境而行动,以取得最大化的预期利益.其灵感来源于心理学中的行为主义理论,即有机体如何在环境给 ...

  7. 《对软件工程课程的期望》

    要学习到的能力的预期:要学会个人,结对,团队的代码编辑流程,学会和别人进行交流. 对项目课程的期望:希望不是枯燥的代码详解. 对项目的愿景规划:希望团队里的每个人都能学到有用的知识. 转载于:http ...

  8. EM算法(Expectation Maximization)期望最大化算法

    原文:EM(期望最大化)算法初步认识 - 大数据和AI躺过的坑 - 博客园 https://www.cnblogs.com/zlslch/p/6965374.html 机器学习十大算法之一:EM算法( ...

  9. 2018-3-5 (论文—网络评论中结构化信息处理的应用于研究)笔记三(互信息,信息增益,期望交叉熵,基于词频的方法,CHI统计)

    传统的特征提取的方法: 1.互信息量(Mutual Information MI):评估零个随机变量相关程度(数组额上离散使用了累加,而连续是积分) 百度:互信息_百度百科 https://baike ...

最新文章

  1. 使用OpenCV,Python和dlib进行眨眼检测及计数
  2. 【11/11】模拟赛
  3. 手动创建1个基于xml配置的springmvc 项目(without Maven)
  4. sql-server基础知识四(视图和索引)
  5. Exploring your Postgraduate Study Options
  6. 正确理解HTML,XHTML页面的头部doctype定义
  7. 2020-07-15 CVPR2020 表示学习论文讨论(4) 笔记
  8. oracle11g同步,Oracle11g三种数据同步方式-Oracle
  9. 小师妹学JVM之:cache line对代码性能的影响
  10. some understanding of《Inferring Decision Trees Using the Minimum Description Length Principle*》
  11. C和指针之反转字符串
  12. 【笛卡尔树】【线段树】meetings 会议(P5044)
  13. php文本框自动补全,PHP自动补全表单的两种方法
  14. bmp文件格式_一次性解决CAD转换成BMP格式图片的问题
  15. c hello world
  16. [排版题] 例4.2 叠框
  17. scp(安全副本)到ec2实例,无需密码
  18. MT2503处理器性能介绍,MT2503/MT2503A/MT2503D芯片资料下载
  19. java apktool if_apktool使用教程
  20. 看看最新BTA大厂的Java程序员的招聘技术标准,Java篇

热门文章

  1. usaco ★Agri-Net 最短网络
  2. java的scanner用法_Java Scanner用法详解
  3. java里class有什么用_安装JDK时的java和javac命令有什么用?
  4. laravel ajax vue6,详解用vue.js和laravel实现微信支付
  5. c语言中求一个数的因数,【代码】求一个数的因数和、求优化、顺便也供新人参考算法...
  6. java语言的实现机制_JAVA语言之Java NIO的工作机制和实现原理介绍
  7. mysql去重保留最后一个_MySQL-去重留一
  8. android qq第三方登录,Android调用第三方QQ登录代码分享
  9. php计算属相,一个判断干支、属相和星座的php函数
  10. MySQL面试题 | 附答案解析(八)