题目链接:https://vjudge.net/problem/LightOJ-1395

1395 - A Dangerous Maze (II)
    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors.

If you choose the ith door, it can either take you back to the same position where you begun in xi minutes, or can take you out of the maze after xi minutes. If you come back to the same position, you can remember last K doors you have chosen. And when you are about to choose a door, you never choose a door that is already visited by you. Or we can say that you never choose a door that is visited as one of the last K doors. And the probability of choosing any remaining door is equal.

Now you want to find the expected time to get out of the maze.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and two integers n K (1 ≤ n ≤ 100, 0 ≤ K ≤ n). The next line contains n space separated integers. If the ith integer (xi) is positive, you can assume that the ith door will take you out of maze after xi minutes. If it's negative, then the ith door will take you back to the beginning position after abs(xi) minutes. You can safely assume that 1 ≤ abs(xi) ≤ 10000.

Output

For each case, print the case number and the expected time to get out of the maze. If it's impossible to get out of the maze, print '-1'. Otherwise print the result. Error less than 10-6 will be ignored.

Sample Input

Output for Sample Input

4

2 0

10 10

2 0

10 -10

3 1

10 -10 -20

3 2

10 -10 -20

Case 1: 10

Case 2: 20.000

Case 3: 30.0000000000

Case 4: 25.0000000000

题意:

有n扇门,一扇门要么能在特定时间内把人带出迷宫,要么能在特定时间内把人带会原地,人能记住前k个选择,每扇门被选择的几率是相等的。问走出迷宫的平均时间。

题解:

1.LightOJ - 1027 A Dangerous Maze此题的强化版。

2.简称能把人带出去的为A门,带回原地的为B门。假设A门有cnt1扇,B门有cnt2扇,cost1为经过A门的平均时间,cost2为经过B门的平均时间。因为所有门被选中的概率是相等的,所以经过任意一扇A门所用的时间可用cost1代替,经过任意一扇B门所用的时间可用cost2代替。

3.人可以记住前k个选择,可知这k个选择必定都为B门。当k>cnt2时,即人能够把所有B门都记住并且还有剩余,但这些剩余是没用的,因为下一次选择必定是A门。所以只需考虑min(cnt2, k)。

4.取k = min(cnt2, k),设dp[i]为:在记住了i个选择的状况下,走出去所需的平均时间。

当i==k时,dp[k] = (cnt1/(n-k))*cost1 + ( (cnt2-k)/(n-k))*(cost2+dp[k]) ,移项得:dp[k] = cost1 + ((cnt2-k)/cnt1)*cost2

当i < k时,dp[i] = (cnt1/(n-i))*cost1 + ((cnt2-i)/(n-i))*(cost2+dp[i+1]) 。

则dp[0]即为答案。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const double EPS = 1e-6;
15 const int INF = 2e9;
16 const LL LNF = 9e18;
17 const int MOD = 1e5;
18 const int MAXN = 1e2+10;
19
20 double dp[MAXN];
21 int main()
22 {
23     int T, kase = 0, n, k;
24     scanf("%d", &T);
25     while(T--)
26     {
27         int cnt1 = 0, cnt2 = 0;
28         double cost1 = 0, cost2 = 0;
29         scanf("%d%d", &n,&k);
30         for(int i = 1; i<=n; i++)
31         {
32             int val;
33             scanf("%d", &val);
34             if(val>0) cnt1++, cost1 += val;
35             else cnt2++, cost2 -= val;
36         }
37         if(cnt1==0)
38         {
39             printf("Case %d: %d\n", ++kase, -1);
40             continue;
41         }
42         if(cnt1==n)
43         {
44             printf("Case %d: %.8lf\n", ++kase, cost1/cnt1);
45             continue;
46         }
47         cost1 /= cnt1;
48         cost2 /= cnt2;
49         k = min(k, cnt2);
50         dp[k] = cost1 + 1.0*(cnt2-k)/cnt1*cost2;
51         for(int i = k-1; i>=0; i--)
52             dp[i] = 1.0*cnt1/(n-i)*cost1 + 1.0*(cnt2-i)/(n-i)*(cost2+dp[i+1]);
53         printf("Case %d: %.8f\n", ++kase, dp[0]);
54     }
55 }

View Code

转载于:https://www.cnblogs.com/DOLFAMINGO/p/8494099.html

LightOJ - 1395 A Dangerous Maze (II) —— 期望相关推荐

  1. LightOJ 1395 A Dangerous Maze (II) 期望DP

    A Dangerous Maze (II) LightOJ - 1395 这个题是 LightOJ 1027 A Dangerous Maze 基础概率DP 的加强版. 有 nnn 个门,其中有些门通 ...

  2. LightOJ 1395 A Dangerous Maze (II) (概率dp)

    题意:给出n扇门,每扇门都给出一个数x,若为正数,则表示在x时间后走出迷宫,若为负数,则表示在x时间后回到起点,你会记得最后k扇你走过的门(不会再走),求最后的期望时间. 题解:概率dp 这题的进阶版 ...

  3. LightOJ - 1027 A Dangerous Maze —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1027 1027 - A Dangerous Maze     PDF (English) Statistics Fo ...

  4. LightOj 1027 A Dangerous Maze

    一个迷宫有n扇门,走第i扇门时间为xi,若xi为正,则走出迷宫,若xi为负,则回到原来位置并忘记已走过的门.问走出迷宫的时间期望,若不能走出迷宫输出inf,否则以分数形式输出p/q. 题目链接 我们设 ...

  5. [A Dangerous Maze LightOJ - 1027 ][概率题]

    A Dangerous Maze LightOJ - 1027 题目大意:就是你有nnn个门每次你都会随机选一个门,这个门对应得数值如果是负的那么你将会在aia_iai​的时间后回到原来位置,如果是正 ...

  6. A - A Dangerous Maze

    Lightoj 1027 A - A Dangerous Maze //题目大意:一个迷宫有n个门,每个对应一个值,正值表示经过这么多秒后直接出迷宫,负值代表这么多秒后回到最开始的地方,问最后出去的期 ...

  7. 【LightOJ - 1027】A Dangerous Maze(概率dp,数学期望)

    题干: You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like ...

  8. LightOJ 1030 【概率DP求期望】

    借鉴自:https://www.cnblogs.com/keyboarder-zsq/p/6216762.html 题意:n个格子,每个格子有一个值.从1开始,每次扔6个面的骰子,扔出几点就往前几步, ...

  9. hdu4035 Maze 【期望dp + 数学】

    题目链接 BZOJ4035 题解 神题啊...orz 不过网上题解好难看,数学推导不写\(Latex\)怎么看..[Latex中毒晚期] 我们由题当然能很快写出\(dp\)方程 设\(f[i]\)表示 ...

最新文章

  1. 疫情之下的网站优化该怎样进行?
  2. 如何写一个清晰明了的Bug
  3. 中国煤炭行业十四五投资战略与供需形势分析报告2022版
  4. Mac安装oracleVM VMware安装失败,解决方案
  5. cassandra学习笔记五
  6. shell 简介(常用shell之bash)
  7. Scala进阶之路-I/O流操作之文件处理
  8. 解决idea使用maven打自定义jar缺失
  9. MapXtreme绿色部署
  10. MatConvNet对自己的图片分两类及提取图片特征
  11. Linux如何关闭自动锁屏
  12. 看到的不错的产品助理面试题
  13. 解决uniapp调试过程中的请求跨域和环境配置
  14. 30. 小浣熊干脆面
  15. 通信原理:信源编码(一)
  16. 知道创宇区块链实验室受邀参加“2021 CCF中国区块链技术大会”
  17. 电脑已连接无Internet访问解决记录
  18. 什么是SOA架构?为什么使用SOA架构?
  19. 在没有Google的情况下使用Android :(一种)指南
  20. flutter 自定义drawer,并打开

热门文章

  1. ubuntu折腾笔记【三】
  2. Word打开文件时,提示文件扩展名和文件格式不匹配,无法打开
  3. 最让人舒服的11种颜色RGB值和十六进制值
  4. [项目]——文件搜索工具Java-Everything
  5. 【整理学习Hadoop】Hadoop学习基础之一:服务器集群技术
  6. 无限流量与5G即将来临,我们距淘汰Wi-Fi还有多少时间?
  7. 网站推荐-极简壁纸网站
  8. 【渝粤教育】国家开放大学2018年秋季 2408T中国当代文学 参考试题
  9. JavaScript学习之旅-导言篇
  10. 3个套路带你玩转Excel动态图表