题目链接 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. 第1章 VMware中安装CentOS7

    目录 1.1 下载CentOS7安装包 1.2 VMware中新建虚拟机 1.3 安装操作系统 本章讲解在VMware中安装CentOS虚拟机的步骤.使用的VMware Workstation版本为1 ...

  2. 南阳 ACM16 矩形嵌套 动态规划

    矩形嵌套 时间限制:3000 ms  |           内存限制:65535 KB 难度:4   描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c, ...

  3. 1180: [CROATIAN2009]OTOCI(LCT)

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 1200  Solved: 747[Submit][ ...

  4. mysql 分类

    一.系统变量 说明:变量由系统提供,不用自定义 语法: 1.查看系统变量 show[global | session]varisables like ‘ ’:如果没有显示声明global 还是sess ...

  5. Python框架之Django学习笔记(九)

    模型 之前,我们用 Django 建造网站的基本途径: 建立视图和 URLConf . 正如我们所阐述的,视图负责处理一些主观逻辑,然后返回响应结果. 作为例子之一,我们的主观逻辑是要计算当前的日期和 ...

  6. Android环境安装简单总结

    1.安装JDK 参考 http://jingyan.baidu.com/article/215817f7e3f2bd1eda1423f4.html 2.安装android SDK 参考 http:// ...

  7. Python-S9—Day86-ORM项目实战之会议室预定相关

    01 会议室预定1 02 会议室预定2 03 会议室预定3 04 会议室预定4 05 会议室预定5 06 会议室预定6 01 会议室预定1 1.1 项目的架构目录: 1.2 使用Pycharm创建Dj ...

  8. Java求职实战之继承和多态

    1.final修饰变量时,是引用不能变,还是引用的对象不能变? 是指引用变量不能变,引用对象的内容可以变. 2.==和equals有什么区别? 网上搜索一下,发现很多人解释的都比较清楚了.自己简单概括 ...

  9. Matlab freqs 函数

    freqs 模拟滤波器的频率响应 语法: h = freqs(b,a,w)[h,w] = freqs(b,a)[h,w] = freqs(b,a,f)freqs(b,a) 描述: freqs 返回一个 ...

  10. c++中读取文件最快的方法

    https://www.byvoid.com/blog/fast-readfile 可以看看了.