题目链接:https://ac.nowcoder.com/acm/contest/874/A
题目大意:
有一款游戏,你有 2 个账户,一开始都为 0 级,封顶为 n 级,当你的账户为 i 级时,进行一场战斗胜利并升到下一级的概率为 pi ,失败不会降级。你现在有如下策略:
- 一开始随便用一个账户开始。
- 如果这把赢了,就继续用这个账户。
- 如果这把输了,就换个账户继续打。
求要使其中一个账户封顶的期望游玩次数。
分析:
设 dp[i][j] 为当两个账号分别为 i,j 级时,并且下一场比赛使用 i 级的账户参赛,达到n级仍需要的期望参赛次数。
那么 dp[*][n] = dp[n][*] = 0。
于是转移方程为:$$dp[i][j] = p_i * dp[i + 1][j] + (1 - p_i) * dp[j][i] + 1$$
但是这个转移方程存在环,可以联立以下方程组:
$$dp[i][j] = p_i * dp[i + 1][j] + (1 - p_i) * dp[j][i] + 1$$
$$dp[j][i] = p_j * dp[j + 1][i] + (1 - p_j) * dp[i][j] + 1$$
代入后得:
$$dp[i][j] = frac{p_i * dp[i + 1][j] + (1 - p_i) * (p_j * dp[j + 1][i] + 1) + 1}{p_i + p_j - p_i * p_j}$$
可以通过先计算 i + j 大的数来计算整个dp数组。
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #pragma GCC optimize("Ofast") 2 #include <bits/stdc++.h> 3 using namespace std; 4 5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0); 6 #define Rep(i,n) for (int i = 0; i < (n); ++i) 7 #define For(i,s,t) for (int i = (s); i <= (t); ++i) 8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i) 9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i) 10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i) 11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) 12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) 13 14 #define pr(x) cout << #x << " = " << x << " " 15 #define prln(x) cout << #x << " = " << x << endl 16 17 #define LOWBIT(x) ((x)&(-x)) 18 19 #define ALL(x) x.begin(),x.end() 20 #define INS(x) inserter(x,x.begin()) 21 22 #define ms0(a) memset(a,0,sizeof(a)) 23 #define msI(a) memset(a,inf,sizeof(a)) 24 #define msM(a) memset(a,-1,sizeof(a)) 25 26 #define MP make_pair 27 #define PB push_back 28 #define ft first 29 #define sd second 30 31 template<typename T1, typename T2> 32 istream &operator>>(istream &in, pair<T1, T2> &p) { 33 in >> p.first >> p.second; 34 return in; 35 } 36 37 template<typename T> 38 istream &operator>>(istream &in, vector<T> &v) { 39 for (auto &x: v) 40 in >> x; 41 return in; 42 } 43 44 template<typename T1, typename T2> 45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { 46 out << "[" << p.first << ", " << p.second << "]" << " "; 47 return out; 48 } 49 50 inline int gc(){ 51 static const int BUF = 1e7; 52 static char buf[BUF], *bg = buf + BUF, *ed = bg; 53 54 if(bg == ed) fread(bg = buf, 1, BUF, stdin); 55 return *bg++; 56 } 57 58 inline int ri(){ 59 int x = 0, f = 1, c = gc(); 60 for(; c<48||c>57; f = c=='-'?-1:f, c=gc()); 61 for(; c>47&&c<58; x = x*10 + c - 48, c=gc()); 62 return x*f; 63 } 64 65 typedef long long LL; 66 typedef unsigned long long uLL; 67 typedef pair< double, double > PDD; 68 typedef pair< int, int > PII; 69 typedef set< int > SI; 70 typedef vector< int > VI; 71 typedef vector< LL > VL; 72 typedef vector< VL > VVL; 73 typedef map< int, int > MII; 74 const double EPS = 1e-10; 75 const int inf = 1e9 + 9; 76 const LL mod = 1e8 + 7; 77 const int maxN = 1e5 + 7; 78 const LL ONE = 1; 79 const LL evenBits = 0xaaaaaaaaaaaaaaaa; 80 const LL oddBits = 0x5555555555555555; 81 82 int T; 83 int n; 84 double p[307]; 85 // dp[i][j]表示当前两个账号分别是i,j级,并且下一场比赛使用i级的账户参赛,达到n级仍需要的参赛次数的期望 86 double dp[307][307]; 87 88 int main(){ 89 //INIT(); 90 scanf("%d", &T); 91 while(T--) { 92 scanf("%d", &n); 93 Rep(i, n) scanf("%lf", &p[i]); 94 95 Rep(i, n + 1) { 96 dp[i][n] = 0; 97 dp[n][i] = 0; 98 } 99 100 // 枚举 i + j 101 rFor(k, 2 * n - 2, 0) { 102 int tmp = k - n + 1; 103 if(tmp < 0) tmp = 0; 104 For(i, tmp, n - 1) { 105 int j = k - i; 106 if(j < 0) break; 107 dp[i][j] = (p[i] * dp[i + 1][j] + (1 - p[i]) * (p[j] * dp[j + 1][i] + 1) + 1) / (p[i] + p[j] - p[i] * p[j]); 108 } 109 } 110 printf("%.4f ", dp[0][0] + EPS); 111 } 112 return 0; 113 }