UVALive 6514:Crusher’s Code(概率dp)
题目链接 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)的更多相关文章
- UVALive 6672 Bonus Cards 概率dp
题意呢 就是有两种售票方式 一种是icpc 一种是其他方式 icpc抢票成功的概率是其他方式的2倍…… 这时 一个人出现了 他通过内幕知道了两种抢票方式各有多少人 他想知道自己如果用icpc抢票成功的 ...
- 概率dp小结
好久之前学过,记得是一次亚洲区的前几天看了看概率dp,然后亚洲区就出了一道概率dp,当时虽然做上了,但是感觉有很多地方没懂,今天起早温习了一下,觉得很多地方茅塞顿开,果然学习的话早上效果最好了. 首先 ...
- ZOJ3551 Bloodsucker(概率dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Bloodsucker Time Limit: 2 Seconds Me ...
- atcoderI - Coins ( 概率DP)
I - Coins Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement Let NN b ...
- 【POJ】2151:Check the difficulty of problems【概率DP】
Check the difficulty of problems Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8903 ...
- 【Foreign】开锁 [概率DP]
开锁 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 4 5 1 2 5 4 3 1 5 ...
- SGU 495 Kids and Prizes:期望dp / 概率dp / 推公式
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=495 题意: 有n个礼物盒,m个人. 最开始每个礼物盒中都有一个礼物. m个人依次随 ...
- DP专题之概率DP
注意:在概率DP中求期望要逆着推,求概率要正着推 概率DP求期望: 链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 dp[ i ]表示从i点走到n ...
- 牛客练习赛26B 烟花 (概率DP)
链接:https://ac.nowcoder.com/acm/contest/180/B 来源:牛客网 烟花 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5 ...
随机推荐
- proguaid 混淆代码
注意:这里有一个坑.就是-ignorewarnings 他老是混淆不了,告诉你不行.其实加上这句话,就可以了. 下面贴一下代码: -injars c:/ceb_lib.jar -outjars c:/ ...
- volley框架使用
volley网络请求一个json数据很简单,一句话就搞定了. StringRequest stringRequest=new StringRequest(url, new Listener<St ...
- Pass Data Between ASP.NET Pages
There is the data to send in current page <asp:TextBox ID="DataToSendTextBox" runat=&qu ...
- URIs, URLs, and URN
首先,URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源.而URL是uniform resource locator,统一资源定位器,它是一种具体 ...
- sed处理大txt文件(1G) 比如替换某一串字符串,或者删除一行
1.将11.sql文件中"prompt"替换为"--prompt",然后保存为111.sql文件 sed -e "s,prompt,--prompt, ...
- 来自知乎的pthread_cond_wait为什么总是带着mutex
来自https://www.zhihu.com/question/24116967?q=linux%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%20%E8%99%9A%E5%81 ...
- leetcode NO.7 反转整数 (python实现)
来源 https://leetcode-cn.com/problems/reverse-integer/description/ 题目描述 给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 ...
- 直接插入排序(java实现)
这几天看排序算法,网上大多数排序算法的解释都是“过于专业”,导致一时半会看不明白到底在说什么玩意.现在总结下几大排序算法(java实现) 1.直接插入排序 说的简单点,就是一组无序序列{A1,A2,. ...
- 【转】Unity 游戏存档 PlayerPrefs类的用法
http://www.cnblogs.com/qiaogaojian/p/5969855.html unity3d提供了一个用于本地持久化保存与读取的类——PlayerPrefs.工作原理非常简单,以 ...
- win8中写好的程序,在win7中没办法运行
没有安装相应版本的,net framework win8自带4.0 win7自带2.0 所以4.0及其以上的程序在win7跑必须安装4.0及其以上版本的framework