HDU 4089 Activation(概率dp)

11 篇文章 0 订阅
3 篇文章 0 订阅

题面

After 4 years' waiting, the game "Chinese Paladin 5" finally comes out. Tomato is a crazy fan, and luckily he got the first release. Now he is at home, ready to begin his journey.
But before starting the game, he must first activate the product on the official site. There are too many passionate fans that the activation server cannot deal with all the requests at the same time, so all the players must wait in queue. Each time, the server deals with the request of the first player in the queue, and the result may be one of the following, each has a probability:
1. Activation failed: This happens with the probability of p1. The queue remains unchanged and the server will try to deal with the same request the next time.
2. Connection failed: This happens with the probability of p2. Something just happened and the first player in queue lost his connection with the server. The server will then remove his request from the queue. After that, the player will immediately connect to the server again and starts queuing at the tail of the queue.
3. Activation succeeded: This happens with the probability of p3. Congratulations, the player will leave the queue and enjoy the game himself.
4. Service unavailable: This happens with the probability of p4. Something just happened and the server is down. The website must shutdown the server at once. All the requests that are still in the queue will never be dealt.
Tomato thinks it sucks if the server is down while he is still waiting in the queue and there are no more than K-1 guys before him. And he wants to know the probability that this ugly thing happens.
To make it clear, we say three things may happen to Tomato: he succeeded activating the game; the server is down while he is in the queue and there are no more than K-1 guys before him; the server is down while he is in the queue and there are at least K guys before him.
Now you are to calculate the probability of the second thing.

Input

There are no more than 40 test cases. Each case in one line, contains three integers and four real numbers: N, M (1 <= M <= N <= 2000), K (K >= 1), p1, p2, p3, p4 (0 <= p1, p2, p3, p4 <= 1, p1 + p2 + p3 + p4 = 1), indicating there are N guys in the queue (the positions are numbered from 1 to N), and at the beginning Tomato is at the Mth position, with the probability p1, p2, p3, p4 mentioned above.

Output

A real number in one line for each case, the probability that the ugly thing happens.
The answer should be rounded to 5 digits after the decimal point.

Sample Input

2 2 1 0.1 0.2 0.3 0.4
3 2 1 0.4 0.3 0.2 0.1
4 2 3 0.16 0.16 0.16 0.52

Sample Output

0.30427
0.23280
0.90343

题目链接

HDU 4089

参考链接

【概率期望动态规划】

HDU 4089 Activation(概率DP) kuangbin

题意

排队请求服务器处理,有4种情况:

1。激活失败:发生这种情况的概率是p1。队列保持不变,服务器下次将尝试处理相同的请求。

2。连接失败:发生这种情况的概率是p2。刚刚发生了一些事情,队列中的第一个玩家失去了与服务器的连接。然后,服务器将从队列中删除他的请求。之后,玩家将立即再次连接到服务器,并在队列的尾部开始排队。

3。激活成功:发生这种情况的概率为p3。恭喜你,玩家将离开队列,自己享受游戏。

4。服务不可用:发生这种情况的概率是P4。刚刚发生了什么事,服务器坏了。网站必须立即关闭服务器。仍在队列中的所有请求将永远不会被处理。 

给出Tomato开始时,队列的长度N以及Tomato所在的位置,问Tomato遇到服务不可用且他前面的人数小于等于k-1的概率。

分析

令dp[i][j]表示Tomato所在队伍长度为i,位置为j,遇到服务不可用且他前面的人数小于等于k-1的概率。

则答案为dp[N][M]。

遇到服务不可用且他前面的人数小于等于k-1的概率。包括两大类:

①自己刚站上i,j的位置,立即遇到了服务不可用的情况,即队首玩家遇到了服务不可用的情况。

②自己站上i,j位置之后,服务器处理了若干玩家的激活请求之后,遇到了服务不可用的情况。

则1<=j<=i,

j==1:    dp[i][1]=p1*dp[i][1]+p2*dp[i][i]+p4;

2<=j<=k: dp[i][j]=p1*dp[i][j]+p2*dp[i][j-1]+p3*dp[i-1][j-1]+p4;

k<j<=i:  dp[i][j]=p1*dp[i][j]+p2*dp[i][j-1]+p3*dp[i-1][j-1];

队首玩家有p1的概率遇到激活失败,①Tomato是队首,位置还在i,1。②Tomato不是队首,位置不动。dp[i][1]和dp[i][j]分别指①②两种队首玩家遇到激活失败的情况下,遇到服务不可用且他前面的人数小于等于k-1的概率

队首玩家有p2的概率遇到链接失败,①Tomato是队首排到队尾,位置转移到i,i。②Tomato不是队首,位置向前移。dp[i][i]和dp[i][j-1]分别指①②两种情况下,遇到服务不可用且他前面的人数小于等于k-1的概率

队首玩家有p3的概率遇到激活成功,dp[i-1][j-1]指队伍变短,位置向前挪,位置转移到i-1,j-1的情况下,遇到服务不可用且他前面的人数小于等于k-1的概率

队首玩家有p4的概率遇到服务不可用,在这种情况下,只要Tomato之前的玩家人数<=K,就是遇到服务不可用且他前面的人数小于等于k-1的概率。假如j>K,则遇到服务不可用且他前面的人数小于等于k-1的概率为0。

x2=p2/(1-p1)
x3=p3/(1-p1)
x4=p4/(1-p1)

化简:
j==1:    dp[i][1]=x2*dp[i][i]+x4;
2<=j<=k: dp[i][j]=x2*dp[i][j-1]+x3*dp[i-1][j-1]+x4;
k<j<=i:  dp[i][j]=x2*dp[i][j-1]+x3*dp[i-1][j-1];

可以循环i=1->n 递推求解dp[i].在求解dp[i]的时候dp[i-1]就相当于常数了。
在求解dp[i][1~i]时等到下列i个方程
j==1:   dp[i][1]=x2*dp[i][i]+c[1];
2<=j<=k:dp[i][j]=x2*dp[i][j-1]+c[j];
k<j=i:  dp[i][j]=x2*dp[i][j]+c[j];
其中c[j]都是常数了。上述方程可以解出dp[i]了。
首先是迭代得到 dp[i][i].然后再代入就可以得到所有的dp[i]了。

p4<eps时候,直接输出0

程序分析

  • dp[i][j]表示Tomato所在队伍长度为i,位置为j,遇到服务不可用且他前面的人数小于等于k-1的概率。
  • x2_power[]指p2/(1-p1)的各次方
  • c[]指dp[i][j]中除dp[i][i]之外的常数部分

程序

#include<stdio.h>
#define maxn 2005
double dp[maxn][maxn];
double x2_power[maxn];
double c[maxn];

int main()
{
    int N,M,K;
    double p1,p2,p3,p4;
    while(scanf("%d%d%d",&N,&M,&K)!=EOF)
    {
        scanf("%lf%lf%lf%lf",&p1,&p2,&p3,&p4);
        if(p4<0.000001)
        {
            printf("%.5lf\n",0);
            continue;
        }
        double x2,x3,x4;
        x2=p2/(1-p1);
        x3=p3/(1-p1);
        x4=p4/(1-p1);
        x2_power[0]=1.0;
        for(int i=1;i<maxn;i++)
            x2_power[i]=x2_power[i-1]*x2;
        dp[1][1]=x4/(1-x2);
        for(int i=2;i<=N;i++)
        {
            c[1]=x4;
            for(int j=2;j<=K;j++)
                c[j]=x3*dp[i-1][j-1]+x4;
            for(int j=K+1;j<=i;j++)
                c[j]=x3*dp[i-1][j-1];
            double C=0;
            for(int j=1;j<=i;j++)
                C+=c[j]*x2_power[i-j];
            dp[i][i]=C/(1-x2_power[i]);
            dp[i][1]=dp[i][i]*x2+x4;

            for(int j=2;j<i;j++)
            {
                dp[i][j]=x2*dp[i][j-1]+c[j];
            }
        }
        printf("%.5lf\n",dp[N][M]);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值