【HDOJ】4089 Activation

时间:2023-03-09 08:09:42
【HDOJ】4089 Activation

1. 题目描述
长度为n的等待队列,tomato处于第m个,有如下四种可能:
(1)激活失败,概率为$p_1$,队列中的顺序不变;
(2)连接失败,概率为$p_2$,队头玩家重新排在队尾;
(3)激活成功,概率为$p_3$,队头出队;
(4)服务器down机,概率为$p_4$,队伍停止。
问当服务器当机时,tomato处在队列前k个人的概率是多少?

2. 基本思路
这显然是个概率DP。dp[i][j]表示队伍中有i个人,tomato处在第j个满足所求的概率。
\begin{align}
    j=1,        (1-p_1) \times dp[i][j] &= p_2 \times dp[i][i] + p_4                                \\
    j\in [2,k], (1-p_1) \times dp[i][j] &= p_2 \times dp[i][j-1] + p_3 \times dp[i-1][j-1] + p_4    \\
    j>k,        (1-p_1) \times dp[i][j] &= p_2 \times dp[i][j-1] + p_3 \times dp[i-1][j-1]
\end{align}
显然,对于$\forall i$均存在i个等式,组成方程组。可以通过(1)式代入,解出dp[i][i]。
注意$p_4 \rightarrow 0$时,即不会发生down机的情况。概率为0。

3. 代码

 /* 4089 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const double eps = 1e-;
const int maxn = ;
double dp[maxn][maxn];
double P[maxn];
int n, kth, k;
double p1, p2, p3, p4; void solve() {
if (p4 < eps) {
puts("0.00000");
return ;
} dp[][] = p4 / (-p1-p2);
double p21 = p2 / (-p1);
double p31 = p3 / (-p1);
double p41 = p4 / (-p1);
rep(i, , n+) {
double coef = 0.0, cons = 0.0; rep(j, , i+) {
if (j == ) {
coef = p21;
cons = p41;
} else if (j <= k) {
cons = p41 + p31 * dp[i-][j-] + p21 * cons;
coef = p21 * coef;
} else {
cons = p31 * dp[i-][j-] + p21 * cons;
coef = p21 * coef;
}
} dp[i][i] = cons / (1.0 - coef);
rep(j, , i) {
if (j == ) {
dp[i][j] = p21 * dp[i][i] + p41;
} else if (j <= k) {
dp[i][j] = p21 * dp[i][j-] + p31 * dp[i-][j-] + p41;
} else {
dp[i][j] = p21 * dp[i][j-] + p31 * dp[i-][j-];
}
}
} double ans = dp[n][kth];
printf("%.05lf\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (cin >> n >> kth >> k >> p1 >> p2 >> p3 >> p4) {
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}