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. [Silverlight] UI 测试/UI 自动化相关知识
  2. ADO与ADO.NET
  3. 计算机四级网络工程师考点速查,全国计算机等级考试标准教程:四级网络工程师...
  4. 变种GandCrab样本分析
  5. hibernate--
  6. 历史上最有影响力的10款开源项目
  7. font awesome java_java awt实现 fontawesome转png
  8. 把网站图片和php程序分离,我的图片服务器和WEB应用服务器相分离的简单方案
  9. Bochs 调试命令
  10. SVN部署与简单使用
  11. python爬取妹子图片1_利用爬虫爬取清纯妹子图片
  12. NSGA-II的算法介绍
  13. C语言情人节浪漫烟花表白代码
  14. java进行微信公众号开发
  15. (转)wincap抓包原理
  16. 学习型通用红外遥控设备(一)
  17. 基尔霍夫定律的验证与multisim仿真(附工程文件)
  18. Slf4j之MDC机制
  19. 类与队列的简单实战爬虫
  20. Neuroink脑计算机的未来展现了什么

热门文章

  1. .Net MVC3中取得当前区域的名字(Area name)
  2. 编译工具 之 ant
  3. mpvue还在维护吗_mpvue 问题汇总(持续更新)
  4. python pip指令_python 常用 pip 命令合集
  5. 工信部 学习类app_工信部整治APP侵权行为,私自收集个人信息等8类问题被点名...
  6. php 原生查询mongo,PHP操作MongoDB的原生CURD方法
  7. 广播站 PHP,让一实听见你的声音——“校园之声”广播站招募小记
  8. 卷积神经网络基础:(7)经典的网络架构
  9. 力扣(LeetCode)刷题,简单题(第13期)
  10. 深入理解 wpa_supplicant(二)