BZOJ4008 [HNOI2015]亚瑟王 【概率dp】
题目链接
题解
要求所有牌造成伤害的期望,就是求每一张牌发动的概率\(g[i]\)
我们发现一张牌能否发动,还与其前面的牌是否发动有关
那我们设\(f[i][j]\)表示前\(i\)张在\(r\)轮游戏中总共发动了\(j\)张的概率
那么
\[g[i] = \sum\limits_{j = 0}^{min\{i - 1,r\}} f[i - 1][j](1 - (1 - p[i])^{r - j})
\]
因为前\(i - 1\)张发动了\(j\)个,一定占用了\(j\)轮,剩余\(r - j\)轮中\(i\)必须发动
考虑如何求\(f[i][j]\)
同样是考虑\(i\)发动不发动
如果发动
\[f[i][j] += f[i - 1][j - 1](1 - (1 - p[i])^{r - j + 1})
\]
如果不发动
\[f[i][j] += f[i - 1][j](1 - p[i])^{r - j}
\]
这样这题就做完了
时间复杂度\(O(Tnr)\)
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
double f[222][135],g[222],p[222],d[222],pw[222][135];
int n,r;
int main(){
int T = read();
while (T--){
n = read(); r = read();
REP(i,n){
scanf("%lf",&p[i]);
d[i] = read();
pw[i][0] = 1;
for (int j = 1; j <= r; j++)
pw[i][j] = pw[i][j - 1] * (1 - p[i]);
}
f[0][0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 0; j <= min(i,r); j++){
f[i][j] = 0;
if (j) f[i][j] += f[i - 1][j - 1] * (1 - pw[i][r - j + 1]);
if (i > j) f[i][j] += f[i - 1][j] * pw[i][r - j];
}
double ans = 0;
for (int i = 1; i <= n; i++){
g[i] = 0;
for (int j = 0; j <= min(i - 1,r); j++)
g[i] += f[i - 1][j] * (1 - pw[i][r - j]);
ans += g[i] * d[i];
}
printf("%.10lf\n",ans);
}
return 0;
}