zoj 3640 Help Me Escape (概率dp 递归求期望)
Help Me Escape
Time Limit: 2 Seconds Memory Limit: 32768 KB
Background
If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at the door. And unto thee shall be his desire, and thou shalt rule over him.
And Cain talked with Abel his brother: and it came to pass, when they were in the field, that Cain rose up against Abel his brother, and slew him.
And the LORD said unto Cain, Where is Abel thy brother? And he said, I know not: Am I my brother's keeper?
And he said, What hast thou done? the voice of thy brother's blood crieth unto me from the ground.
And now art thou cursed from the earth, which hath opened her mouth to receive thy brother's blood from thy hand;
When thou tillest the ground, it shall not henceforth yield unto thee her strength; a fugitive and a vagabond shalt thou be in the earth.
—— Bible Chapter 4
Now Cain is unexpectedly trapped in a cave with N paths. Due to LORD's punishment, all the paths are zigzag and dangerous. The difficulty of the ith path is ci.
Then we define f as the fighting capacity of Cain. Every day, Cain will be sent to one of the N paths randomly.
Suppose Cain is in front of the ith path. He can successfully take ti days to escape from the cave as long as his fighting capacity f is larger than ci. Otherwise, he has to keep trying day after day. However, if Cain failed to escape, his fighting capacity would increase ci as the result of actual combat. (A kindly reminder: Cain will never died.)
As for ti, we can easily draw a conclusion that ti is closely related to ci. Let's use the following function to describe their relationship:
After D days, Cain finally escapes from the cave. Please output the expectation of D.
Input
The input consists of several cases. In each case, two positive integers N and f (n ≤ 100, f ≤ 10000) are given in the first line. The second line includes N positive integers ci (ci ≤ 10000, 1 ≤ i ≤ N)
Output
For each case, you should output the expectation(3 digits after the decimal point).
Sample Input
3 1
1 2 3
Sample Output
6.889
题意:
有一个吸血鬼被困了,有n条路可以逃出去,每条路有一个难度c[],他初始的战斗力是f,对于第i条路,若f > c[i]他花t[i]天就能出去,否则,他就停留一天,同时战斗力增加c[i]然后再选一条路走出去,他走每条路的概率是相同的。问他逃出去的天数的期望。
注意t[i]是整数。
分析:
d[i]表示战斗力为 i 的时候的逃出去的期望。
用递归的好处是思路比较清楚,就是每次按照概率加 什么时候能逃出去的期望。
这个题也有逆推 递推的做法。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <algorithm>
#define LL __int64
const int maxn = +;
using namespace std;
double d[+], t[maxn]; //注意数组开到了2倍,因为终态可能是20000.
int c[maxn], n; double dfs(int f)
{
if(d[f]>) return d[f]; //相当于剪枝,已经计算过的不再计算 for(int i = ; i < n; i++)
{
int tmp = (int)t[i];
if(f>c[i])
d[f] += 1.0/n*(double)tmp;
else
d[f] += 1.0/n*(1.0+dfs(f+c[i]));
}
return d[f];
} int main()
{
int f, i;
while(~scanf("%d%d", &n, &f))
{
for(i = ; i < n; i++)
{
scanf("%d", &c[i]);
t[i] = (double)c[i]*c[i]*1.0*(1.0+sqrt(5.0))/2.0;
}
memset(d, , sizeof(d));
printf("%.3lf\n", dfs(f));
}
return ;
}
zoj 3640 Help Me Escape (概率dp 递归求期望)的更多相关文章
- zoj 3640 Help Me Escape 概率DP
记忆化搜索+概率DP 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...
- POJ 2096 Collecting Bugs (概率DP,求期望)
Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu ...
- ZOJ 3329 One Person Game 【概率DP,求期望】
题意:有三个骰子,分别有k1,k2,k3个面. 每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和. 当分数大于n时结束.求游戏的期望步数.初始分数为0 设dp[i]表示达到 ...
- ZOJ 3329 One Person Game(概率DP,求期望)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题目大意: 有三个骰子,分别有K1,K2,K3个面,一次投掷可以得到三个 ...
- BZOJ2554 color 【概率DP】【期望DP】
题目分析: 好题. 一开始看错题了,以为是随机选两个球,编号在前的染编号在后的. 但这样仍然能获得一些启发,不难想到可以确定一个颜色,剩下的颜色是什么就无关了. 那么答案就是每种颜色的概率乘以期望.概 ...
- 概率dp——逆推期望+循环迭代zoj3329
首先要推出dp[i]的期望方程,会发现每一项都和dp[0]相关, 那我们将dp[i]设为和dp[0]有关的式子dp[i]=a[i]*dp[0]+b[i],然后再回代到原来的期望方程里 然后进行整理,可 ...
- ZOJ 3640 Help Me Escape:期望dp
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3640 题意: 有一个吸血鬼被困住了,他要逃跑... 他面前有n条 ...
- BZOJ 3640: JC的小苹果 [概率DP 高斯消元 矩阵求逆]
3640: JC的小苹果 题意:求1到n点权和\(\le k\)的概率 sengxian orz的题解好详细啊 容易想到\(f[i][j]\)表示走到i点权为j的概率 按点权分层,可以DP 但是对于\ ...
- zoj 3329 One Person Game 概率DP
思路:这题的递推方程有点麻烦!! dp[i]表示分数为i的期望步数,p[k]表示得分为k的概率,p0表示回到0的概率: dp[i]=Σ(p[k]*dp[i+k])+dp[0]*p0+1 设dp[i]= ...
随机推荐
- MongoDB的CRUD操作(java Util )
1.保存插入操作: public static synchronized String insert(DBObject record) { DBCollection col = MongoDB.get ...
- Nested Classes
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html package priceton; /* * Copyright (c) ...
- dm层 集市层 四层 Build a multi-level data strategy
集市层 四层模型 ODS(临时存储层) MID(中间层) DM(数据集市层) APP(应用层) http://www.datamartist.com/data-warehouse-vs-data-ma ...
- JavaScript学习笔记(持续更新)
函数有“定义式”和“变量式”两种写法的区别 函数有“定义式”和“变量式”两种写法,两者基本区别不大.主要区别在于,如果在同一个代码块(Script标签对)定义两个同名的函数,浏览器在预编译代码时,使用 ...
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- Flask中的内置session
Flask中的Session非常的奇怪,他会将你的SessionID存放在客户端的Cookie中,使用起来也非常的奇怪 1. Flask 中 session 是需要 secret_key 的 from ...
- 1.2 Data Abstraction(算法 Algorithms 第4版)
1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...
- 【C】四则运算生成和核对器----by郁卓、谢明浩
[Github项目地址] 完成功能: 1. 使用 -n 参数控制生成题目的个数 2. 使用 -r 参数控制题目中数值(自然数.真分数和真分数分母)的范围 3. 生成的题目中计算过程不能产生负数,也就是 ...
- struts2 validate手动验证
我们前面学习struts2知道,struts2通过拦截器实现了一些验证操作. 比如,如果是不能转换的类型在action中接受的话会跳转到错误页面,错误信息中会包含对应的错误信息,例如: 首先我们了解一 ...
- 【Codeforces】Round #460 E - Congruence Equation 中国剩余定理+数论
题意 求满足$na^n\equiv b \pmod p$的$n$的个数 因为$n \mod p $循环节为$p$,$a^n\mod p$循环节为$p-1$,所以$na^n \mod p$循环 ...