题目链接 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. python变量声明及简单数据类型

    一.python声明变量 变量的命名和使用 # 在Python中使用变量时,需要遵守一些规则和指南. # 违反这些规则将引发错误,而指南旨在让你编写的代码更容易阅读和理解.请务必牢记下述有关变量的规则 ...

  2. Laravel — homestead 配置多站点

    一.homestead.yaml 配置 homestead.yaml 文件配置sites,如下 sites: - map: homestead.test to: /home/vagrant/Code/ ...

  3. JZOJ 4722. 跳楼机

    Description  DJL为了避免成为一只咸鱼,来找srwudi学习压代码的技巧.Srwudi的家是一幢h层的摩天大楼.由于前来学习的蒟蒻越来越多,srwudi改造了一个跳楼机,使得访客可以更方 ...

  4. MTCNN学习资源

    MTCNN pytorch版本的实现 TropComplique/mtcnn-pytorch https://github.com/TropComplique/mtcnn-pytorch MTCNN实 ...

  5. 三分钟明白 Activity工作流 -- java运用

    一. 什么是工作流 以请假为例,现在大多数公司的请假流程是这样的 员工打电话(或网聊)向上级提出请假申请——上级口头同意——上级将请假记录下来——月底将请假记录上交公司——公司将请假录入电脑 采用工作 ...

  6. 权限组件(11):基于formset实现批量增加

    效果图: 增加页面: 编辑页面: 因为后面要对权限进行批量操作,所以先用这个示例演示下如何实现批量操作 数据库 from django.db import models class Menu(mode ...

  7. PAT Basic 1085

    1085 PAT单位排行 每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数 N(≤10​5​​),即考生人数.随后 N 行, ...

  8. windows下pip安装python模块时报错【转】

    windows下pip安装python模块时报错总结 请给作者点赞--> 原文链接 1 权限问题 C:\Users\ljf>pip install xlwt Exception: Trac ...

  9. loj2045 「CQOI2016」密钥破解

    CQOI 板子大赛之 pollard rho #include <iostream> #include <cstdio> using namespace std; typede ...

  10. IOS开发---菜鸟学习之路--(十三)-利用MBProgressHUD进行异步获取数据

    本章将介绍如何利用MBProgressHUD实现异步处理数据. 其实我本来只是像实现一个加载数据时提示框的效果,然后问了学长知道了这个类,然后就使用了 接着就发现了一个“BUG” 再然后就发现原来MB ...