Dudu is a very starving possum. He currently stands in the first shelf of a fridge. This fridge is
composed of N shelves, and each shelf has a number Qi (1 ≤ i ≤ N) of food. The top shelf, where
Dudu is, is identified by the number 1, and the lowest is identified by number N. Dudu doesn’t eat
more than one food in the same shelf, because he doesn’t want to get noticed. Furthermore, Dudu is
very fat and cannot climb the wall of the fridge to a shelf above — nobody knows how did he end up
in the first shelf. Dudu is also afraid of height, so he is only able to climb down at most K shelves at
a time (if he is at shelf i, he is only able to reach shelves i + 1, i + 2, . . . , i + K). There is a chance
Pj that he chooses to get down from a shelf i to a shelf i + j (1 ≤ j ≤ K). If he tries to go down a
number of shelves that makes him get past the lowest shelf, he gets out of the fridge — he will always
get out of the fridge eventually, because someone left the door open. Each food of shelf i has a number
of calories Ci,j that is absorbed by Dudu in case he eats it, and a probability Xi,j that it is chosen by
Dudu, for j from 1 to Qi
. Dudu starts his journey at shelf 1 and, when he is in a shelf, he will always
choose a food to eat and then will go to another shelf. What is the expected number of calories that
Dudu will absorb by the time he gets out of the fridge?
Input
The first line contains T (T ≤ 100) — the number of test cases, after this line T test cases follows.
The first line of a test case contains two integers, N and K (1 ≤ N ≤ 500; 1 ≤ K ≤ 10) — the
number of shelves in the fridge and the maximum number of shelves Dudu can climb down at a time,
correspondingly. The second line of a test case contains K real numbers Pj , where Pj is the probability
that Dudu goes down j shelves, for j from 1 to K (0 ≤ Pj ≤ 1; ∑K
j=1 Pj = 1). Each of the next N
lines of a test case describes a shelf (from the shelf 1 to shelf N). Each line starts with a integer Qi
(1 ≤ Qi ≤ 20), which is the amount of food existent is in this shelf. Qi pair follows, each pair containing
2 real numbers Ci,j and Xi,j (0 ≤ Ci,j ≤ 100; 0 ≤ Xi,j ≤ 1; ∑Qi
j=1 Xi,j = 1).
Output
For each test case print a line containing ‘Case #X: Y ’, where X is the case number, starting at 1,
and Y is the expected number of calories that Dudu will absorb by the time he gets out of the fridge.
Y should be rounded up to 6 digits after the decimal point.
Sample Input
2
2 1
1.0
2 50 0.5 100 0.5
2 10 0.5 20 0.5
5 2
0.3 0.7
5 10 0.2 20 0.3 5 0.1 25 0.35 2 0.05
2 20 0.4 40 0.6
1 4 1.0
3 30 0.8 3 0.1 4 0.1
10 1 0.1 2 0.1 3 0.1 4 0.1 5 0.1 6 0.1 7 0.1 8 0.1 9 0.1 10 0.1
Sample Output
Case #1: 90.000000
Case #2: 44.929950

题目大意:已知老鼠在第一层,只能往下走走到第n层结束。在第i层时可以走i+1、i+2、....、i+k,选择第i+j层的概率是Pj(1<=<=k)。每层有若干食物包括选它的概率跟它所含的卡路里,老鼠会选一种食物吃。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100007 double ex[];
double dp[];
double pk[];
double C[][],X[][];
double sum[]; int main()
{
int t,cs = ,i,j,k;
int n,m,K,Q;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&K);
for(i=;i<=K;i++)
scanf("%lf",&pk[i]);
for(i=;i<=n;i++)
{
scanf("%d",&Q);
ex[i] = 0.0;
for(j=;j<=Q;j++)
{
scanf("%lf%lf",&C[i][j],&X[i][j]);
ex[i] += C[i][j]*X[i][j];
}
}
memset(dp,,sizeof(dp));
dp[] = ; //dp is probability
for(i=;i<=n;i++)
{
for(k=;k<=K;k++)
{
if(i-k >= )
dp[i] += dp[i-k]*pk[k];
}
}
double res = ;
for(i=;i<=n;i++)
res += dp[i]*ex[i];
printf("Case #%d: %.6lf\n",cs++,res);
}
return ;
}

uva 12723 概率dp的更多相关文章

  1. UVA 11427 (概率DP+期望)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35396 题目大意:每晚打游戏.每晚中,赢一局概率p,最多玩n局, ...

  2. [uva 11762]Race to 1[概率DP]

    引用自:http://hi.baidu.com/aekdycoin/item/be20a91bb6cc3213e3f986d3,有改动 题意: 已知D, 每次从[1,D] 内的所有素数中选择一个Ni, ...

  3. UVa 11427 Expect the Expected (数学期望 + 概率DP)

    题意:某个人每天晚上都玩游戏,如果第一次就䊨了就高兴的去睡觉了,否则就继续直到赢的局数的比例严格大于 p,并且他每局获胜的概率也是 p,但是你最玩 n 局,但是如果比例一直超不过 p 的话,你将不高兴 ...

  4. UVA 10900 So you want to be a 2n-aire? (概率dp)

    题意:玩家初始的金额为1:给出n,表示有n道题目:t表示说答对一道题目的概率在t到1之间均匀分布. 每次面对一道题,可以选择结束游戏,获得当前奖金:或者回答下一道问题,答对的话奖金翻倍,答错的话结束游 ...

  5. Substring UVA - 11468 AC自动机+概率DP

    题意: 给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S. 给出K个模板串,计算S不包含任何一个模板串的概率 dp[i][j]表示走到AC自动机 i 这个节点 还需要走 ...

  6. 概率dp专辑

    求概率 uva11021 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. DP专题之概率DP

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

  8. Codeforces 28C [概率DP]

    /* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...

  9. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

随机推荐

  1. Dojo常用函数

    1.array函数:和原生的JavaScript中的数组遍历方法forEach方法用法相同 define(['dojo/_base/declare', "dojo/_base/array&q ...

  2. Python读写文件实际操作的五大步骤

    Python读写文件在计算机语言中被广泛的应用,如果你想了解其应用的程序,以下的文章会给你详细的介绍相关内容,会你在以后的学习的过程中有所帮助,下面我们就详细介绍其应用程序. 一.打开文件 Pytho ...

  3. Python中文编码问题(字符串前面加'u')

    中文编码问题是用中文的程序员经常头大的问题,在python下也是如此,那么应该怎么理解和解决python的编码问题呢? 我们要知道python内部使用的是unicode编码,而外部却要面对千奇百怪的各 ...

  4. 01_9_ServletContext

    01_9_ServletContext 1. 例子 public void doGet(HttpServletRequest request, HttpServletResponse response ...

  5. Java--equals和 == 的比较和equals()、HashCode()的重写

    一. equals和 == 的比较 1.== 运算符 ① == 如果比较的是基本数据类型,则比较的是值. ② == 如果比较的是引用数据类型,则比较的是地址值. 2.equals ①它属于java.l ...

  6. js替换函数用法

    定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replac ...

  7. GTX 750等低配显卡如何玩转Deepfakes?

    这里说的Deepfakes软件还是DeepFaceLab,人工智能换脸,是使用深度学习方法来实现的.而深度学习程序对电脑配置要求是非常高的,尤其是跑模型这个环节.很多低配电脑,根本就跑步起来.比如像G ...

  8. 22.Yii2.0框架多表关联一对一查询之hasOne

    思路: 通过文章查它对应的分类信息 一对一的关系 控制器里 //一对一关联查询 public function actionRelatesone() { //方法一,hasOne() 用查一条文章的结 ...

  9. CodeForces - 948C Producing Snow(优先队列)

    题意: n天. 每天你会堆一堆雪,体积为 v[i].每天都有一个温度 t[i] 所有之前堆过的雪在第 i 天体积都会减少 t[i] . 输出每天融化了的雪的体积. 这个题的正解我怎么想都很难理解,但是 ...

  10. 正在创建模型,此时不可使用上下文“的解决办法。 正在创建模型,此时不可使用上下文。如果在 OnModelCreating 方法内使用上下文或如果多个线程同时访问同一上下文实例,可能引发此异常。请注意不

    //默认为: Database.SetInitializer<testContext>(null);//这里报错, 检查原因:catch(Exception ex) 错误提示: 基础连接未 ...