码迷,mamicode.com
首页 > 其他好文 > 详细

hdu4087(概率dp)

时间:2019-07-27 11:22:52      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:some   cal   release   就是   offic   failed   other   ane   esc   

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

 

Activation

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


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

 

题意大意:有n个人排队等着在官网上激活游戏。Tomato排在第m个。
对于队列中的第一个人。有一下情况:
1、激活失败,留在队列中等待下一次激活(概率为p1)
2、失去连接,出队列,然后排在队列的最后(概率为p2)
3、激活成功,离开队列(概率为p3)
4、服务器瘫痪,服务器停止激活,所有人都无法激活了。
求服务器瘫痪时Tomato在队列中的位置<=k的概率

首先定义状态:dp[i][j] i个人排队Tomato排在第j位到目标状态的概率。
那么可以得到状态转移方程:
j==1 :dp[i][j]=p1*dp[i][j]+p2*dp[i][i]+p3*dp[i-1][j-1]+p4
j<=2<=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]
写到这里我们会觉得dp[i][j]这个状态求不出来,因为我们必须先得到dp[i][i]才能求出dp[i][1],这是我们不妨将dp[i][j]写成别的形式
我们可以发现:每个状态都可以写成dp[i][j]=b[j]*dp[i][i]+c[j](b,c为常数)
所以我们先迭代,得到 dp[i][i]=b[i]*dp[i][i]+c[i],然后求得dp[i][i]
最后返回依次求出dp[i][j]=b[j]*dp[i][i]+c[j] (1<=j<i)了
最后的答案就是dp[n][m]。
最后还得注意:当p4足够小的时候,是没有解的此时直接输出0即可,还要注意的是此题需要用滚动数组,不然会超内存。

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e3+5;
const double eps=1e-9;
double dp[2][maxn];
double b[maxn],c[maxn];
int main(){
    double p1,p2,p3,p4;
    int n,m;
    double op;
    int k;
    while(~scanf("%d%d%d%lf%lf%lf%lf",&n,&m,&k,&p1,&p2,&p3,&p4)){
        double p=1.0-p1;
        dp[0][0]=0.0;
        if(p4<eps){
            puts("0.00000");
            continue;
        }
        for(int i=1;i<=n;i++){
            
            //j==1
            b[1]=p2/(1-p1);
            c[1]=p3/(1-p1)*dp[(i-1)&1][0]+p4/(1-p1);
            //j==2
            b[2]=p2/p*b[1];
            c[2]=p3/p*dp[(i-1)&1][1]+p4/p+c[1]*p2/p;

            for(int j=3;j<=k;j++){
                b[j]=b[j-1]*p2/p;
                c[j]=c[j-1]*p2/p+p3/p*dp[(i-1)&1][j-1]+p4/p;                
            }
            for(int j=k+1;j<=i;j++){
                b[j]=b[j-1]*p2/p;
                c[j]=c[j-1]*p2/p+p3/p*dp[(i-1)&1][j-1];
            }
            dp[i&1][i]=c[i]/(1.0-b[i]);
            for(int j=1;j<i;j++){
                dp[i&1][j]=b[j]*dp[i&1][i]+c[j];
            }
        }
        printf("%.5lf\n",dp[n&1][m]);
    }
    return 0;
}

 




hdu4087(概率dp)

标签:some   cal   release   就是   offic   failed   other   ane   esc   

原文地址:https://www.cnblogs.com/Zhi-71/p/11254232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!