题目链接 https://icpcarchive.ecs.baylor.edu/external/65/6514.pdf

题意:给出n个数(n<8) 求这n个数分别两个程序排成有序时,程序的期望迭代次数。排序程序如下。

// Monty's Code
while (!sorted(a)) {
int i = random(n) ;
int j = random(n) ;
if (a[min(i,j)] > a[max(i,j)])
swap(a[i], a[j]) ;
} //Carlos's Code
while (!sorted(a)) {
int i = random(n-) ;
int j = i + ;
if (a[i] > a[j])
swap(a[i], a[j]) ;
}

思路:正常的概率dp。这里“亮”的地方在与,其状态的定义就暴力的定义成了这个串。……因为 8! < 50000。 所以能过。

代码[略锉]:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std; map<int,int> id;
int idp;
int isSortedA;
double e[]; int getid(int a) {
if (id[a] == ) id[a] = idp++;
return id[a];
} int hash(int a[], int n) {
int ret = ;
for (int i = ; i < n; i++) {
ret = ret* + a[i];
}
return ret;
} int n; //monty(anow) = p1*monty(anext1) + p2*monty(anext2) + .. + (1-p1-p2)*monty(anow) + 1;
//p = 2/n*n
double monty(int a) {
if (a == isSortedA) return ;
if (e[getid(a)] != ) return e[getid(a)]; int tmp[];
int tmpa = a;
for (int i = n-; i >= ; i-- ) {
tmp[i] = tmpa%;
tmpa/=;
} int num = ;
for (int i = ; i < n; i++) {
for (int j = i+; j < n; j++) {
if (tmp[i] > tmp[j]) num++;
}
} if (num == ) {
isSortedA = a;
return ;
} double ans = n*n/(num*2.0);
for (int i = ; i < n; i++) {
for (int j = i+; j < n; j++) {
if (tmp[i] > tmp[j]) {
swap(tmp[i], tmp[j]);
ans += 1.0/num * monty(hash(tmp,n));;
swap(tmp[i], tmp[j]);
}
}
}
return e[getid(a)] = ans;
} double carlos(int a) {
if (a == isSortedA) return ;
if (e[getid(a)] != ) return e[getid(a)]; int tmp[];
int tmpa = a;
for (int i = n-; i >= ; i-- ) {
tmp[i] = tmpa%;
tmpa/=;
} int num = ;
for (int i = ; i < n-; i++) {
if (tmp[i] > tmp[i+]) num++;
} if (num == ) {
isSortedA = a;
return ;
} double ans = (n-1.0)/num;
for (int i = ; i < n-; i++) {
int j = i+;
if (tmp[i] > tmp[j]) {
swap(tmp[i], tmp[j]);
ans += 1.0/num * carlos(hash(tmp,n));;
swap(tmp[i], tmp[j]);
}
}
return e[getid(a)] = ans;
} int a[]; int main() {
int t;
scanf("%d", &t);
while (t--) { isSortedA = -; scanf("%d", &n);
int tmp[];
for (int i = ; i < n; i++) {
scanf("%d", &a[i]);
tmp[i] = a[i];
}
sort(tmp, tmp+n);
unique(tmp, tmp+n);
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
if (a[i] == tmp[j]) {
a[i] = j;
break;
}
}
} idp = ;
id.clear();
memset(e,,sizeof(e));
printf("Monty %.6lf ", monty(hash(a,n))); idp = ;
id.clear();
memset(e,,sizeof(e));
printf("Carlos %.6lf\n", carlos(hash(a,n)));; }
return ;
}

UVALive 6514:Crusher’s Code(概率dp)的更多相关文章

  1. UVALive 6672 Bonus Cards 概率dp

    题意呢 就是有两种售票方式 一种是icpc 一种是其他方式 icpc抢票成功的概率是其他方式的2倍…… 这时 一个人出现了 他通过内幕知道了两种抢票方式各有多少人 他想知道自己如果用icpc抢票成功的 ...

  2. 概率dp小结

    好久之前学过,记得是一次亚洲区的前几天看了看概率dp,然后亚洲区就出了一道概率dp,当时虽然做上了,但是感觉有很多地方没懂,今天起早温习了一下,觉得很多地方茅塞顿开,果然学习的话早上效果最好了. 首先 ...

  3. ZOJ3551 Bloodsucker(概率dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Bloodsucker Time Limit: 2 Seconds      Me ...

  4. atcoderI - Coins ( 概率DP)

    I - Coins Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement Let NN b ...

  5. 【POJ】2151:Check the difficulty of problems【概率DP】

    Check the difficulty of problems Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8903   ...

  6. 【Foreign】开锁 [概率DP]

    开锁 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 4 5 1 2 5 4 3 1 5 ...

  7. SGU 495 Kids and Prizes:期望dp / 概率dp / 推公式

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=495 题意: 有n个礼物盒,m个人. 最开始每个礼物盒中都有一个礼物. m个人依次随 ...

  8. DP专题之概率DP

    注意:在概率DP中求期望要逆着推,求概率要正着推 概率DP求期望: 链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 dp[ i ]表示从i点走到n ...

  9. 牛客练习赛26B 烟花 (概率DP)

    链接:https://ac.nowcoder.com/acm/contest/180/B 来源:牛客网 烟花 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5 ...

随机推荐

  1. 致敬wusir懒孩子自有懒孩子的生存之道之二

    https://www.cnblogs.com/wupeiqi/ https://www.cnblogs.com/Eva-J/ https://www.cnblogs.com/wupeiqi/p/90 ...

  2. day 59 MySQL之锁、事务、优化、OLAP、OLTP

    MySQL之锁.事务.优化.OLAP.OLTP   本节目录 一 锁的分类及特性 二 表级锁定(MyISAM举例) 三 行级锁定 四 查看死锁.解除锁 五 事务 六 慢日志.执行计划.sql优化 七 ...

  3. 一个人的旅行 HDU - 2066 (最短路)

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  4. German Collegiate Programming Contest 2018​ C. Coolest Ski Route

    John loves winter. Every skiing season he goes heli-skiing with his friends. To do so, they rent a h ...

  5. Reachability from the Capital

    题目描述 There are nn cities and mm roads in Berland. Each road connects a pair of cities. The roads in ...

  6. Eclipse 读取config目录下文件

    最近在一个项目,在项目下新建了一个config配置文件夹,添加一个配置文件config.properties. 使用classpath:config.properties方式加载配置文件, 具体实现代 ...

  7. Hyper-V动态迁移中?小心性能损失

    动态迁移是虚拟化技术的一个标志,它允许虚拟机在服务器间进行动态迁移.调节负载平衡.性能管理.备灾管理和数据中心维护.Windows Server 2012 R2中的Hyper-V动态迁移默认功能具备相 ...

  8. 【BZOJ 3620】似乎在梦中见过的样子

    题目 (夢の中で逢った.ような--) 「Madoka,不要相信 QB!」伴随着 Homura 的失望地喊叫,Madoka 与 QB 签订了契约. 这是 Modoka 的一个噩梦,也同时是上个轮回中所发 ...

  9. 整理的一些Android开发类免费视频课程

    1.Android实战淘宝网项目视频:http://edu.ibeifeng.com/view-index-id-248.html 2.Android滚动视差实现课程:http://edu.ibeif ...

  10. mysql环境变量配置(复制)

    前面步骤完成后安装好MySQL,为MySQL配置环境变量.MySQL默认安装在C:\Program Files下. 1)新建MYSQL_HOME变量,并配置:C:\Program Files\MySQ ...