P3239 [HNOI2015] Arthur - Probability DP

Questions surface: King Arthur

Recently exam is expected autistic ah, I did not do this type of problem, only now a training practice;

So-called expectation is the state multiplied by their probability; for this question, we are asking for is multiplied by each card's damage and probability of hit;

Of course not directly take, because the probability to each round of play this card, this card did not play the next card to be considered, to have a license issued before the end of the round skills; unless a card are not made come out;

Each set of cards played probability is exp [], the answer is exp [i] * d [i];

 

exp [i] how demand?

We should always be treated equally in front of probability;

Set f [i] [j] to play cards before i j probability of cards, respectively, by f [i-1] [j-1] and f [i-1] [j] transferred over to play this card this card did not go out and play out;

This card to play out, then the wheel j-1, he does not throw out, r-j + 1 round him thrown out, that is f [i-1] [j -1] * (1- (1- P [I]) R & lt J-+. 1 );

 (1-P [I]) R & lt-j + 1   is the probability that no play is left, that is, 1 minus the probability of j-round;

Probability is not playing wheel j F [. 1-I] [j] * (l- (. 1-P [I]) RJ  )

 

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=222;
typedef double dd;
int T;
int n,r;
dd p[maxn];
int d[maxn];

dd ksm_p[maxn][maxn];
void pre_p()
{
    for(int i=1;i<=n;i++)
    {
        ksm_p[i][0]=1;
        for(int j=1;j<=r;j++)
        {
            ksm_p[i][j]=ksm_p[i][j-1]*(1-p[i]);
        }
    }
}
dd ans;
dd f[maxn][maxn];//i card j used
dd exp[maxn];

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        ans=0;
        memset(f,0,sizeof(f));
        memset(exp,0,sizeof(exp));
        scanf("%d%d",&n,&r);
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%d",&p[i],&d[i]);
        }
        pre_p();
        f[1][0]=ksm_p[1][r];
        f[1][1]=exp[1]=1.0-ksm_p[1][r];
        for(int i=2;i<=n;i++)
        {
            for(int j=0;j<=r;j++)
            {
                if(j>i) break;
                if(j!=i) exp[i]+=f[i-1][j]*(1-ksm_p[i][r-j]);
                if(j) f[i][j]+=f[i-1][j-1]*(1-ksm_p[i][r-j+1]);
                if(i!=j) f[i][j]+=f[i-1][j]*ksm_p[i][r-j];
            }
        }
        for(int i=1;i<=n;i++) ans+=exp[i]*d[i];
        printf("%.10lf\n",ans);
    }
    return 0;
}

 

 

 

 

  

Guess you like

Origin www.cnblogs.com/WHFF521/p/11628627.html