HDU 4089 Activation (概率dp 好题 + 难题)

Activation

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1842    Accepted Submission(s): 689

Problem Description

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

Source

2011 Asia Beijing Regional Contest

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4089

题目大意:n个人排队激活一个东西,初始时目标排在第m个

有以下4种情况:

1.注册失败,但是不影响队列顺序 ,概率为p1

2.连接失败,队首的人排到队尾,    概率为p2

3.注册成功,队首离开队列,           概率为p3

4.服务器崩溃,激活停止,               概率为p4

求目标排在k位以内,且服务器可能崩溃的概率

题目分析:这题的状态和转移方程并不是很难想,难就难在化简和递推上,下面给出详细分析:

dp[i][j]表示队伍里有i个人,目标排在第j个时时间可能发生的概率

dp[i][1] = dp[i][1]*p1+dp[i][i]*p2+p4 (当目标排在第一个且队里有i个人时可能目标注册失败还在第一个,可能链接失败跑到队尾,也可能服务器崩溃)

dp[i][j] = dp[i][j]*p1+dp[i][j-1]*p2+dp[i-1][j-1]*p3+p4  (j<=k)  (当目标排在第j个,且在k之前时,1.第一个人可能注册失败还在第一个且目标位置不变,2.第一个人可能连接失败到队尾,这时目标位置向前移动,3.第一个人可能注册成功并离队,这时目标位置向前移动,且队伍少一人,4.服务器可能崩溃)

dp[i][j] = dp[i][j]*p1+dp[i][j-1]*p2+dp[i-1][j-1]*p3        (j>k) (该情况和 j<=k唯一的不同点就是没有p4了,很好理解,因为当目标排名在k之后时不能让服务器崩溃,否则事件就不可能发生了,我们就是在递推事件可能发生的概率)

说的这里状态和转移方程的问题解决了,接下来要解决如何递推,首先我们将状态转移方程化简得到:

令:

p21 = p2/(1-p2)

p31 = p3/(1-p1)

p41 = p4/(1-p1);

则:

dp[i][1] = dp[i][i]*p21+p41

dp[i][j]  = dp[i][j-1]*p21+dp[i-1][j-1]*p31+p41;   (j<=k)

dp[i][j]  = dp[i][j-1]*p21+dp[i-1][j-1]*p31;           (j>k)

从递推式我们可以知道当要递推dp[i][j]时dp[i-1][j-1]已经被算出来了,我们不妨用c[j]代表dp[i][j]的常数项

即dp[i][j] = dp[i][j-1]*p21 + c[j],则c[j] = dp[i-1][j-1]*p31 + p41 ,c[1] = p41

因此我们先用递推式把c数组得到,然后要求dp[i][i]

dp[i][1] = p21*dp[i][i]  + c[1];

dp[i][2] = p21*dp[i][1] + c[2];

dp[i][3] = p21*dp[i][2] + c[3];

...

dp[i][i] = p21*dp[i][i-1] + c[i];

把以上式子叠加起来dp[i][i] = p21*(dp[i][i-1]) + c[i] = p21*(p21*dp[i][i-2] + c[i-1]) + c[i] = ... = p21*(p21*...dp[i][1] + c[2]) +... 再dp[i][1]=p21*dp[i][i] + c[1]带到上式得dp[i][i] =(p21)^i*dp[i][i] + (p21)^(i-1)c[1] + (p21)^(i-2)c[2] + ...(p21)^0c[i]再移向化简得dp[i][i]
= ∏(p21)^(i-j)c[j] / (1 - (p21)^i)由此可见,我们可以先预处理出(p21)^i,到这里这题就快搞定了,算出了dp[i][i]我们就可以得到dp[i][1]了

dp[i][1]=dp[i][i]*p21+p41,这样供递推的值都得到了,直接根据dp[i][j] =
dp[i][j-1]*p21 + c[j]递推就可以了

初始值dp[1][1]=dp[1][1]*p21+p41 即 dp[1][1]=p4 / (1-p1-p2),还要注意的一点就是精度问题,当p4小于1e-5时,当其为不可能事件,因为题目要求的精度就是小数点后5位

很有趣的一题~

#include <cstdio>
int const MAX = 2005;
double const EPS = 1e-5;
double dp[MAX][MAX], c[MAX], p[MAX];
int main()
{
    int n, m, k;
    double p1, p2, p3, p4;
    while(scanf("%d %d %d %lf %lf %lf %lf", &n, &m, &k, &p1, &p2, &p3, &p4) != EOF)
    {
        if(p4 < EPS)
        {
            printf("0.00000\n");
            continue;
        }
        double p21 = p2 / (1 - p1);
        double p31 = p3 / (1 - p1);
        double p41 = p4 / (1 - p1);
        double tmp;
        p[0] = 1;
        for(int i = 1; i <= n; i++)
            p[i] = p[i - 1] * p21;
        dp[1][1] = p4 / (1 - p1 - p2);
        for(int i = 2; i <= n; i++)
        {
            for(int j = 1; j <= i; j++)
            {
               if(j <= k)
                   c[j] = dp[i - 1][j - 1] * p31 + p41;
               else
                   c[j] = dp[i - 1][j - 1] * p31;
            }
            tmp = 0;
            for(int j = 1; j <= i; j++)
                tmp += p[i - j] * c[j];
            dp[i][i] = tmp / (1 - p[i]);
            dp[i][1] = p21 * dp[i][i] + p41;
            for(int j = 2; j < i; j++)
               dp[i][j] = p21 * dp[i][j - 1] + c[j];
        }
        printf("%.5f\n", dp[n][m]);
    }
}
时间: 2024-10-18 20:25:37

HDU 4089 Activation (概率dp 好题 + 难题)的相关文章

hdu 5001 walk 概率dp入门题

Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel t

HDU 4035 Maze 概率DP 好题

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 2012    Accepted Submission(s): 802Special Judge Problem Description When wake up, lxhgww find himself in a huge maze. The maze consisted by

HDU4089/Uva1498 Activation 概率DP(好题)

题意:Tomato要在服务器上激活一个游戏,一开始服务器序列中有N个人,他排在第M位,每次服务器会对序列中第一位的玩家进行激活,有四种结果: 1.有p1的概率会激活失败,这时候序列的状态是不变的.2.有p2的概率第一位的玩家会连接错误,这时候序列中第一位的玩家会成为最后一位,其他玩家相对位置不变.3.有p3的概率第一位的玩家激活成功,这时候第一位的玩家会离开序列.4.有p4的概率服务器崩溃,发生这件事之后所有玩家都不能激活了.求Tomato遇到服务器崩溃并且在服务器崩溃时处于前K位的概率. 解法

hdu 4405(概率dp简单题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1535    Accepted Submission(s): 1050 Problem Description Hzz loves aeroplane

HDU 4576 Robot 概率DP 水题

Robot Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 3851    Accepted Submission(s): 1246 Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells.

hdu 4089 不错的DP 北京现场赛题

http://acm.hdu.edu.cn/showproblem.php?pid=4089 还有疑惑,需要重新推: 但是学到的: 1.A=a+b+c  abc是三种情况,那么P(A)=a*P(a->事件)+b*P(b->事件)+c*P(c->事件); a->事件意思是 在a情况下的事件,就是全概率公式的思想吧 2.一定注意每一步会不会出现分母为0 的情况,以及预处理的时候对于一些特殊情况导致自己的式子会出现分母为0的排除掉 3.概率DP经常出现推出了式子但是自己不会写代码的情况,

概率DP入门题

一 概率问题的论文 1.算法合集之<信息学竞赛中概率问题求解初探> 2.有关概率和期望问题的研究 3.算法合集之<浅析竞赛中一类数学期望问题的解决方法> 二 入门题目 1.POJ 3744 Scout YYF I (简单题) 题意:一条路上有n个地雷 ,a[i]代表第i个地雷放的位置,求安全走过这段路的概率 分析:若第k个位置有地雷则安全走过这个位置的方案为在第k-1个位置跳两步概率为(1-p) 从反面考虑 已经安全走过了第i-1个雷 则在第i个雷的死掉的概率为 1-p(从走到a[

HDU 4035Maze(概率DP)

HDU 4035   Maze 体会到了状态转移,化简方程的重要性 题解转自http://blog.csdn.net/morgan_xww/article/details/6776947 /** dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点1处(概率为ki) 2.找到出口,走出迷宫 (概率为ei) 3.和该点相连有m条边,随机走一条 求:走出迷宫所要走的边数的期望值. 设 E[i]表示

13年山东省赛 The number of steps(概率dp水题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Memory Limit: 128 M Description Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two ro