A Dangerous Maze (II) LightOJ

来源:互联网 发布:linux fat挂载移动硬盘 编辑:程序博客网 时间:2024/06/06 09:02

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
4

2 0
10 10

2 0
10 -10

3 1
10 -10 -20

3 2
10 -10 -20
Sample Output
Case 1: 10
Case 2: 20.000
Case 3: 30.0000000000
Case 4: 25.0000000000

大致题意:是 A Dangerous Maze LightOJ - 1027 这道题的加强版,题意差不多只不过多了个条件,即你能记住前k次所选的门。

思路:假设dp[i]表示此时已经记住的负值门的个数为i时所走出迷宫的期望时间(记住的肯定都是负值门,如果打开了正值门就直接出去了),假设sum1为正值门总和,sum2为负值门总和,cnt1为正值门个数,cnt2为负值门个数。如果k>=cnt2,那么dp[cnt2]=sum1/cnt1,即记住所有的负值门时,你下一步所选的一定是一个正值门,期望时间即正值门的平均值。
然后 当 0<= i < cnt2时,有 dp[i]=sum1 / (n-i)+(cnt2-i)*(dp[i+1]+X)/(n-i) (X表示从cnt2个负值门中选择了i个负值门后,再从剩下的负值门里选择一个门的平均值,其实就是sum2/cnt2,即负值门的平均值),然后从后往前推,所求得的dp[0]值即答案。
如果k < cnt2,那么dp[k]=sum1 / (n-k)+(cnt2-k)*(dp[k]+X)/(n-k),然后解出dp[k]的值,接下来和上面同理。

代码如下

#include<iostream>#include<cstdio>#include<queue>#include<cmath>#include<cstring>using namespace std;#define LL long long #define ULL unsigned long long double ans[105];int main(){    int T;    int n,k;    scanf("%d",&T);    int cnt1,cnt2;//正值门的个数,负值门的个数     double sum1,sum2;//正值的总和,负值的总和     for(int cas=1;cas<=T;cas++)    {        cnt1=cnt2=sum1=sum2=0;        scanf("%d%d",&n,&k);        int x;        for(int i=1;i<=n;i++)        {            scanf("%d",&x);            if(x>0)            {                cnt1++;                sum1+=x;            }            else             {                cnt2++;                sum2-=x;            }        }        if(cnt2==n)            printf("Case %d: -1\n",cas);        else         {            if(k>=cnt2)            {                k=cnt2;                ans[k]=sum1/cnt1;            }            else             {                ans[k]=(sum1/(n-k)+(cnt2-k)*(sum2/cnt2)*1.0/(n-k))/(1-(cnt2-k)*1.0/(n-k));            }            for(int i=k-1;i>=0;i--)//从后往前推,ans[0]即答案             ans[i]=sum1/(n-i)+(cnt2-i)*(ans[i+1]+sum2/cnt2)*1.0/(n-i);            printf("Case %d: %.7lf\n",cas,ans[0]);        }    }    return 0;}
原创粉丝点击