HDU-4089 Activation

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): 1500    Accepted Submission(s):
570

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人都是仙剑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   = 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];

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const double eps=1e-5;
double dp[2000][2000];
double pp[2000],c[2000];
int main()
{
    int n,m,k,i,j;
   double p1,p2,p3,p4;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        memset(dp,0,sizeof(dp));
        scanf("%lf%lf%lf%lf",&p1,&p2,&p3,&p4);
          if(p4<eps)
        {
            printf("0.00000\n");
            continue;
        }
        dp[1][1]=p4/(1-(p1+p2));
         double p=p2/(1-p1);
          double p31=p3/(1-p1);
         double p41=p4/(1-p1);
         c[1]=p41;
         pp[0]=1;
         for(i=1;i<=n;i++)
           pp[i]=p*pp[i-1];
         for(i=2;i<=n;i++)
         {
             for(j=2;j<=k&&j<=i;j++)
               c[j]=p31*dp[i-1][j-1]+p41;
             for(j=k+1;j<=n&&j<=i;j++)
               c[j]=p31*dp[i-1][j-1];
                double temp=c[1]*pp[i-1];
              for(j=2;j<=n;j++)
                temp+=c[j]*pp[i-j];
               dp[i][i]=temp/(1-pp[i]);
             dp[i][1]=p*dp[i][i]+c[1];
             for(j=2;j<i;j++)
                 dp[i][j]=p*dp[i][j-1]+c[j];
         }
         printf("%.5lf\n",dp[n][m]);
    }
    return 0;
}

HDU-4089 Activation,布布扣,bubuko.com

时间: 2024-08-05 06:56:10

HDU-4089 Activation的相关文章

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.

hdu 4089 Activation (概率dp 手动消元)

Activation Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1562    Accepted Submission(s): 592 Problem Description After 4 years' waiting, the game "Chinese Paladin 5" finally comes out.

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经常出现推出了式子但是自己不会写代码的情况,

【HDU】4089 Activation

http://acm.hdu.edu.cn/showproblem.php?pid=4089 题意: 有n个人排队等着在官网上激活游戏.主角排在第m个. 对于队列中的第一个人.有以下情况:1.激活失败,留在队列中等待下一次激活(概率为p1)2.失去连接,出队列,然后排在队列的最后(概率为p2)3.激活成功,离开队列(概率为p3)4.服务器瘫痪,服务器停止激活,所有人都无法激活了. 求服务器瘫痪时主角在队列中的位置<=k的概率 n, m<=1000, p1+p2+p3+p4=1 #include

HDU 4089 &amp;&amp; UVa 1498 Activation 带环的概率DP

要在HDU上交的话,要用滚动数组优化一下空间. 这道题想了很久,也算是想明白了,就好好写一下吧. P1:激活游戏失败,再次尝试. P2:连接失服务器败,从队首排到队尾. P3:激活游戏成功,队首的人出队. P4:服务器down掉,所有人都不能激活了. 设d(i, j)表示i个人排队,主人公排在第j位,发生所求事件的概率. d(i, 1) = P1 d(i, 1) + P2 d(i, i) + P4 //分别对应激活失败,重新尝试:连接失败排到队尾:服务器down掉 特殊地可以直接计算出 d(1,

Activation(hdu 4089)

题目:仙5的激活序列.有以下4种情况: 1.注册失败,但是不影响队列顺序 ,概率为p1 2.连接失败,队首的人排到队尾,概率为p2 3.注册成功,队首离开队列,概率为p3 4.服务器崩溃,激活停止,概率为p4 求主角的位置在K以内,而且服务器崩溃的概率 /* dp[i][j]表示有i个人,主角在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

hdu 4089 概率dp

1 /* 2 题目大意:注册一款游戏需要排队,一共有四种事件: 3 1.注册失败,队列不变,概率为p1 4 2.注册过程中断开连接,正在注册的人排到队列的末尾,概率为p2 5 3.注册成功,移出队列,概率为p3 6 4.服务器暂停服务,概率为p4 7 求一个人他前面有不超过k-1个人的时候暂停服务的概率. 8 从前往后推,统计答案太麻烦,所以选择从后往前推. 9 dp(i,j)表示一共i个人,他排在j位置的达到目标状态的概率. 10 j==1 dp[i][j]=dp[i][j]*p1+dp[i]

HDU 4089

很容易列出方程 设dp[i][j]为排在第i位置,总共有j个人排队到达目标状态的概率 i=1 dp[i][j]=p4+p1*dp[i][j]+p2*dp[j][j] 2<=i<=k dp[i][j]=p4+p1*dp[i][j]+p2*dp[i-1][j]+p3*dp[i-1][j-1] i>k dp[i][j]=p1*dp[i][j]+p2*dp[i-1][j]+p3*dp[i-1][j-1] 设p2=p2/(1-p1),p3=p3/(1-p1),p4=p4/(1-p4) 上述三个转移

ACM总结——dp专辑(转)

感谢博主——      http://blog.csdn.net/cc_again?viewmode=list       ----------  Accagain  2014年5月15日 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. 本人动态规划博客地址:http://blog.csdn.net/cc_again/article/category/1261899 ***********************

【DP专辑】ACM动态规划总结

转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. 本人动态规划博客地址:http://blog.csdn.net/cc_again/article/category/1261899 ******************