Light OJ 1342 - Aladdin and the Magical Sticks (概率DP)

来源:互联网 发布:万合星多屏软件破解版 编辑:程序博客网 时间:2024/06/05 01:05

解析:设dp[i][j]为有i个未放入的可辨别的砖块和j个未放入的不可辨别的砖块时,放入不可辨别的砖块的重量期望值。

num1,tot1分别是可辨别砖块的个数和重量和;

num2,tot2分别是不可辨别砖块的个数和重量和。

那么dp[i][j] = (num2-j)*dp[i][j]/(i+num2) + j*dp[i][j-1]/(i+num2) + i*dp[i-1][j]/(i+num2) + tot2/(i+num2);

[code]:

#include<cstdio>#include<cstring>using namespace std;const int maxn = 5005;double dp[2][maxn];int n,a[maxn],b[maxn],num1,num2,tot1,tot2;void init(){    num1 = num2 = 0;    tot1 = tot2 = 0;    dp[0][0] = 0;}void sol(){    int i,j,cr,pr;double ans;    for(i = 0;i <= num1;i++){        cr = i&1;pr = (cr+1)&1;        for(j = 0;j <= num2;j++){            if(!i&&!j) continue;            ans = tot2;            if(j) ans += j*dp[cr][j-1];            if(i) ans += i*dp[pr][j];            dp[cr][j] = ans/(i+j);        }    }    printf("%.6f\n",dp[num1&1][num2]+tot1);}int main(){    int i,j,cas;    scanf("%d",&cas);    for(int T=1;T<=cas;T++){        scanf("%d",&n);        init();        for(i = 1;i <= n;i++){            scanf("%d%d",&a[i],&b[i]);            if(b[i]==1) tot1 += a[i],num1++;            else tot2 += a[i],num2++;        }        printf("Case %d: ",T);        sol();    }    return 0;}


0 0
原创粉丝点击