UVA1498題解

UVA1498題解

UVA1498

題目描述

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

題解

這是一道2011年區域賽原題,難度頗高,做到一半但是沒做出來,看了題解后如夢方醒,遂寫此篇題解。
首先,我們可以設 d p [ i ] [ j ] dp[i][j] dp[i][j]為Tomato在隊列長度為i的隊列中,處於j位置的狀態,從而可以推出狀態轉移方程如下:

{ d p [ i ] [ 1 ] = p 1 ∗ d p [ i ] [ 1 ] + p 2 ∗ d p [ i ] [ i ] + p 4 j = 1 ( 1 ) d p [ i ] [ j ] = p 1 ∗ d p [ i ] [ j ] + p 2 ∗ d p [ i ] [ j − 1 ] + p 3 ∗ d p [ i − 1 ] [ j − 1 ] + p 4 1 < j < = k ( 2 ) d p [ i ] [ j ] = p 1 ∗ d p [ i ] [ j ] + p 2 ∗ d p [ i ] [ j − 1 ] + p 3 ∗ d p [ i − 1 ] [ j − 1 ] k < j < = i ( 3 ) \left\{ \begin{aligned} \pmb{dp[i][1]} & = p1*\pmb{dp[i][1]} + p2*dp[i][i] + p4 &j=1 & (1)\\ \pmb{dp[i][j]} & = p1*\pmb{dp[i][j]} + p2*dp[i][j-1] + p3*dp[i-1][j-1] +p4 & 1<j<=k & (2)\\ \pmb{dp[i][j]} & = p1*\pmb{dp[i][j]} + p2*dp[i][j-1] + p3*dp[i-1][j-1] & k<j<=i & (3)\\ \end{aligned} \right. dp[i][1]dp[i][1]dp[i][1]dp[i][j]dp[i][j]dp[i][j]dp[i][j]dp[i][j]dp[i][j]=p1dp[i][1]dp[i][1]dp[i][1]+p2dp[i][i]+p4=p1dp[i][j]dp[i][j]dp[i][j]+p2dp[i][j1]+p3dp[i1][j1]+p4=p1dp[i][j]dp[i][j]dp[i][j]+p2dp[i][j1]+p3dp[i1][j1]j=11<j<=kk<j<=i(1)(2)(3)
方程(3)中因爲國王本不在隊列中,因此如果飯堂關門,國王也無法躋身與前k人中,因此無需增加p4.
預熱工作準備結束,下面進入正題。我們觀察方程(1),(2),(3)可知方程左右兩邊有相同項,從而我們可以對公式中加粗部分進行移項處理,從而可以得到新的方程組如下:

{ d p [ i ] [ 1 ] = p 2 1 − p 1 ∗ d p [ i ] [ i ] + p 4 1 − p 1 j = 1 ( 1 ) d p [ i ] [ j ] = p 2 1 − p 1 ∗ d p [ i ] [ j − 1 ] + p 3 1 − p 1 ∗ d p [ i − 1 ] [ j − 1 ] + p 4 1 − p 1 1 < j < = k ( 2 ) d p [ i ] [ j ] = p 2 1 − p 1 ∗ d p [ i ] [ j − 1 ] + p 3 1 − p 1 ∗ d p [ i − 1 ] [ j − 1 ] k < j < = i ( 3 ) \left\{ \begin{aligned} dp[i][1] & = \frac{p2}{1-p1}*dp[i][i] + \frac{p4}{1-p1} &j=1 & (1)\\ dp[i][j] & = \frac{p2}{1-p1}*dp[i][j-1] + \frac{p3}{1-p1}*dp[i-1][j-1] +\frac{p4}{1-p1} & 1<j<=k & (2)\\ dp[i][j] & = \frac{p2}{1-p1}*dp[i][j-1] + \frac{p3}{1-p1}*dp[i-1][j-1] & k<j<=i & (3)\\ \end{aligned} \right. dp[i][1]dp[i][j]dp[i][j]=1p1p2dp[i][i]+1p1p4=1p1p2dp[i][j1]+1p1p3dp[i1][j1]+1p1p4=1p1p2dp[i][j1]+1p1p3dp[i1][j1]j=11<j<=kk<j<=i(1)(2)(3)

令:

{ p = p 2 1 − p 1 p 13 = p 3 1 − p 1 p 14 = p 4 1 − p 1 \left\{ \begin{aligned} p=\frac{p2}{1-p1}\\ p13=\frac{p3}{1-p1}\\ p14=\frac{p4}{1-p1}\\ \end{aligned} \right. p=1p1p2p13=1p1p3p14=1p1p4

我們通過觀察可以發現方程(2)和(3)中的 d p [ i − 1 ] [ j − 1 ] dp[i-1][j-1] dp[i1][j1]是可以在循環過程中被直接求出的,因此我們可以將它當作常數來看待,我們令 c [ i ] c[i] c[i]為常數項,從而可以得到

{ c [ i ] = p 14 j = 1 c [ i ] = p 13 ∗ d p [ i − 1 ] [ j − 1 ] + p 14 1 < j < = k c [ i ] = p 13 ∗ d p [ i − 1 ] [ j − 1 ] k < j < = i \left\{ \begin{aligned} c[i]=& p14 & j=1\\ c[i]=& p13*dp[i-1][j-1]+p14 & 1<j<=k\\ c[i]=& p13*dp[i-1][j-1] & k<j<=i\\ \end{aligned} \right. c[i]=c[i]=c[i]=p14p13dp[i1][j1]+p14p13dp[i1][j1]j=11<j<=kk<j<=i
從而方程(2)的後兩項,方程(1)的最後一項與方程(3)的最後一項我們可以將其看作是常數來處理,進而我們可以推出一系列數列式:

d p [ i ] [ 1 ] = p ∗ d p [ i ] [ i ] + c [ 1 ] d p [ i ] [ 2 ] = p ∗ d p [ i ] [ 1 ] + c [ 2 ] . . . d p [ i ] [ i ] = p ∗ d p [ i ] [ i − 1 ] + c [ i ] \begin{aligned} dp[i][1]=p*dp[i][i]+c[1] \\ dp[i][2]=p*dp[i][1]+c[2] \\ ... \\ dp[i][i]=p*dp[i][i-1]+c[i]\\ \end{aligned} dp[i][1]=pdp[i][i]+c[1]dp[i][2]=pdp[i][1]+c[2]...dp[i][i]=pdp[i][i1]+c[i]
現在我們所存在的問題是無法算出 d p [ i ] [ 1 ] dp[i][1] dp[i][1]中的 d p [ i ] [ i ] dp[i][i] dp[i][i], 但是我們通過上面的數列可以發現,這件事是可以解決的,我們只需要將上面的式子往下面帶入,這樣就可以在最後一個式子中得到一個關於dp[i][i]的一元一次方程從而,就可以得出答案了。敲了那麽多累死了(:-)),直接上代碼了

#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
double dp[2010][2010];
double c[2010]/*常數數組*/,p[2010]/*係數數組*/;
#define eps 1e-10

int main()
{
    int n,m,k;
    double p1,p2,p3,p4;
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    while(cin>>n>>m>>k>>p1>>p2>>p3>>p4) {
        memset(dp,0,sizeof(dp));
        memset(c,0,sizeof(c));
        memset(p,0,sizeof(p));
        if (p4 < eps) {
            cout << "0.00000\n";
            continue;
        }//此處是大坑,如果p4太小就直接返回0.
        double p12 = p2 / (1 - p1);
        double p13 = p3 / (1 - p1);
        double p14 = p4 / (1 - p1);
        p[0] = 1;//數列帶入時的係數數組
        for (int i = 1; i <= n; i++) {
            p[i] = p[i - 1] * p12;
        }//計算的係數
        dp[1][1] = p4 / (1 - p1 - p2);//dp[1][1]可直接算出
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                if (j <= k) {
                    c[j] = dp[i - 1][j - 1] * p13 + p14;//算不同情況下的係數
                } else c[j] = dp[i - 1][j - 1] * p13;
            }
            double temp = 0;
            for (int j = 1; j <= i; j++) {
                temp += p[i - j] * c[j];
            }//數列帶入得到的係數
            dp[i][i] = temp / (1 - p[i]);//計算得到dp[i][i]
            dp[i][1] = p12 * dp[i][i] + p14;//計算dp[i][1]
            for (int j = 2; j < i; j++) {
                dp[i][j] = dp[i][j - 1] * p12 + c[j];
            }//帶入數列求出其他值
        }
        cout << fixed << setprecision(5) << dp[n][m]<<'\n';
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值