hdu 4089 Activation(概率dp)

Activation

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 880    Accepted Submission(s): 315


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
 

Recommend
lcy
 
题意:
有n人都是仙剑5的fans,现在要在官网上激活游戏,n个人排成一个队列(其中主角Tomato最初排名为m),
对于队列中的第一个人,在激活的时候有以下五种情况:
    1.激活失败:留在队列中继续等待下一次激活(概率p1)
    2.失去连接:激活失败,并且出队列然后排到队列的尾部(概率p2)
    3.激活成功:出队列(概率p3)
    4.服务器瘫:服务器停止服务了,所有人都无法激活了(概率p4)
求服务器瘫痪并且此时Tomato的排名<=k的概率。

题解:

思路来源于大神链接

是一个概率题,分析一下题意后发现和“dp求期望”的题目有点像,因为其中都有一种死循环的可能,
该题中,如果总是发生p1概率的情况那就是死循环了。然后想到一个二维dp:
dp[i][j]表示队列中有i个人,Tomato排在第j个,能发生所求事件的概率。
显然,dp[n][m]即为所求。
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;
j > k  : dp[i][j] = p1*dp[i][j] + p2*dp[i][j-1] + p3*dp[i-1][j-1];
化简:
j == 1 : dp[i][1] = p*dp[i][i]   + p41;
2<=j<=k: dp[i][j] = p*dp[i][j-1] + p31*dp[i-1][j-1] + p41;
j > k  : dp[i][j] = p*dp[i][j-1] + p31*dp[i-1][j-1];
其中:
p=p21 = p2 / (1 - p1);
p31 = p3 / (1 - p1);
p41 = p4 / (1 - p1);
现在可以循环 i = 1 -> n 递推求解dp[i],所以在求dp[i]时,dp[i-1]就相当于常数了,
设dp[i][j]的常数项为c[j]:
j == 1 : dp[i][1] = p*dp[i][i]   + c[1];
2<=j<=k: dp[i][j] = p*dp[i][j-1] + c[j];
j > k  : dp[i][j] = p*dp[i][j-1] + c[j];
在求dp[i]时,就相当于求“i元1次方程组”:
dp[i][1] = p*dp[i][i] + c[1];
dp[i][2] = p*dp[i][1] + c[2];
dp[i][3] = p*dp[i][2] + c[3];
...

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

没怎么做过概率DP的题。比赛时推样例就推了半天。最后推出来样例程序又不知道怎么写了。

哎。。。题做少了的过。后来看看题解其实蛮简单。做概率题关键在于思路清晰。一步一步推

自然方程就出来了。基础还是很差。还需要努力呀。加油!

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=2010;
double dp[maxn][maxn];
double pp[maxn],c[maxn];
int main()
{
    double p1,p2,p3,p4,p21,p31,p41,tmp;
    int n,m,k,i,j;

    while(~scanf("%d%d%d",&n,&m,&k))
    {
        scanf("%lf%lf%lf%lf",&p1,&p2,&p3,&p4);
        if(p4<1e-9)
        {
            puts("0.00000");
            continue;
        }
        p21=p2/(1-p1);
        p31=p3/(1-p1);
        p41=p4/(1-p1);
        dp[1][1]=p4/(1-p1-p2);
        pp[0]=1;
        c[1]=p41;
        for(i=1;i<=n;i++)
            pp[i]=pp[i-1]*p21;
        for(i=2;i<=n;i++)
        {
            for(j=2;j<=k;j++)
                c[j]=p31*dp[i-1][j-1]+p41;
            for(j=k+1;j<=i;j++)
                c[j]=p31*dp[i-1][j-1];
            tmp=0;
            for(j=i;j>0;j--)
            {
                tmp+=pp[i-j]*c[j];
            }
            dp[i][i]=tmp/(1-pp[i]);
            dp[i][1]=p21*dp[i][i]+p41;
            for(j=2;j<i;j++)
            dp[i][j]=p21*dp[i][j-1]+c[j];
        }
        printf("%.5lf\n",dp[n][m]);
    }
    return 0;
}


你可能感兴趣的:(c,算法,ACM)