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

1395 - A Dangerous Maze (II)
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]即为答案。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e5;
const int MAXN = 1e2+; double dp[MAXN];
int main()
{
int T, kase = , n, k;
scanf("%d", &T);
while(T--)
{
int cnt1 = , cnt2 = ;
double cost1 = , cost2 = ;
scanf("%d%d", &n,&k);
for(int i = ; i<=n; i++)
{
int val;
scanf("%d", &val);
if(val>) cnt1++, cost1 += val;
else cnt2++, cost2 -= val;
}
if(cnt1==)
{
printf("Case %d: %d\n", ++kase, -);
continue;
}
if(cnt1==n)
{
printf("Case %d: %.8lf\n", ++kase, cost1/cnt1);
continue;
}
cost1 /= cnt1;
cost2 /= cnt2;
k = min(k, cnt2);
dp[k] = cost1 + 1.0*(cnt2-k)/cnt1*cost2;
for(int i = k-; i>=; i--)
dp[i] = 1.0*cnt1/(n-i)*cost1 + 1.0*(cnt2-i)/(n-i)*(cost2+dp[i+]);
printf("Case %d: %.8f\n", ++kase, dp[]);
}
}

LightOJ - 1395 A Dangerous Maze (II) —— 期望的更多相关文章

  1. Lightoj 1027 - A Dangerous Maze 【期望】

    1027 - A Dangerous Maze PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Y ...

  2. LightOJ 1027 - A Dangerous Maze(求期望)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1027 题意:又一个迷宫,有n个门,每个门又一个值num,如果num>0 说明在n ...

  3. LightOJ 1027 A Dangerous Maze(期望)题解

    题意:n扇门,每扇门后都有一个值x,如果x<0会让你等待-x再重新回到这里选择门,x>0你经过x时间就会被传送走,问你被传送走的期望 思路:假设被传送走的期望为E,那么对于x<0来说 ...

  4. LightOJ 1027 A Dangerous Maze(期望)

    https://cn.vjudge.net/problem/LightOJ-1027 题意:有n扇门,每扇门有个时间ti,选择正数的门可以在ti后带你走出迷宫,负数的门会在ti后带你回到起点,然后重新 ...

  5. LightOJ 1027 A Dangerous Maze (数学期望)

    题意:你面前有 n 个门,每次你可以选择任意一个进去,如果xi是正数,你将在xi后出去,如果xi是负数,那么xi后你将回来并且丢失所有记忆,问你出去的期望. 析:两种情况,第一种是直接出去,期望就是 ...

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

    A Dangerous Maze (II) LightOJ - 1395(概率dp) 这题是Light Oj 1027的加强版,1027那道是无记忆的. 题意: 有n扇门,每次你可以选择其中一扇.xi ...

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

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

  8. [LightOJ 1027] A Dangerous Maze

    A Dangerous Maze You are in a maze; seeing n doors in front of you in beginning. You can choose any ...

  9. LightOj 1027 A Dangerous Maze【概率】

    题目链接:http://www.lightoj.com/volume_showproblem.php? problem=1027 题意: 你面前有n个门,每一个相应一个数字,若为正xi.代表xi分钟后 ...

随机推荐

  1. 使用Jsoup解决网页中图片链接问题

    在做Facebook和WhatsApp分享的时候,分享出去的谷歌短链,Facebook获取不到大图,和竞品展示的不一样,WhatsApp分享出去的短链没有图片和描述. WhatsApp: 分析竞品UC ...

  2. SVG图片背景透明

    今天在调整网页的时候,将logo以原有直接贴代码形式,改为加载文件. 其实真正的目的是做SEO.上次SEO交流后得出 结论:核心在于内容的本身的优化.信噪比很重要.也就是有效信息需要占文章的主要内容, ...

  3. ArcGIS教程:面积制表

    摘要 计算两个数据集之间交叉制表的区域并输出表. 插图 使用方法 · 区域定义为输入中具有同样值的全部区.各区无需相连. 栅格和要素数据集都可用于区域输入. · 假设区域输入和类输入均为具有同样分辨率 ...

  4. EVB-P6UL:一识庐山真面目

    前言 原创文章,转载引用务必注明链接.水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验与正确的图片链接显示,请访问我的博客原文: 在爱板网上看到这个活动,昨晚确认,今 ...

  5. php猴子吃桃

    <?php header("content-type:text/html;charset=utf-8"); /* 有一堆桃子,猴子第一天吃了其中的一半,并再多吃了一个! 以后 ...

  6. shell脚本实现定时重启进程

    ##############################Deploy crontab for yechang ad*******eta restart ###################### ...

  7. python实现区块链代码

    如果你明白了原理其实挺简单的. 加密算法是python自带的 需要导入hashlib import hashlib as hash sha = hasher.sha256() sha.update(' ...

  8. RobotFramework-Selenium2Library--关键字

    Selenium2Library用户关键字 *** Settings *** Library Selenium2Library *** Keywords *** Checkbox应该不被选择 [Arg ...

  9. 设计模式之Visitor模式(笔记)

    訪问者模式:表示一个作用于某个对象结构中的各元素操作.它使你能够不改变各元素的类的前提下定义作用于这些元素的新操作. 首先定义一个visitor抽象类,为每一个详细类声明一个visit操作 publi ...

  10. Cocostudio学习笔记(5) Text + TextAtlas + TextBMFont

    下午一群大学生到我们公司參观学习,搞得我好紧张.于是滔滔不绝的给他们介绍了怎样开发一款游戏... 今晚研究的控件就是三个label:Text,TextAtlas,TextBMFont 我先在cocos ...