hdu4813 01 + prefix and backpack

Meaning of the questions: \ (A, B \) two, there \ (N \) events, the probability of occurrence of each piece are \ (0.5 \) , if the event \ (i \) occurs, the \ (B \) plus \ (v_i \) score, if it does not happen, then \ (B \) is not a plus, given a probability \ (P \) , asked how the score at least, in order to make $ a $ have \ (P \) the probability score is not less than \ (B \)

Solution: obtaining a probability value corresponding to each, the question becomes, \ (B \) to obtain each fraction \ (I \) has a probability \ (Q_I \) , find the smallest \ (Ans \) , satisfies \ (\ ^ {Ans sum_0 Q_I} <= P \) .
DP + and prefix.

#include<iostream>
#include<cstdio>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i = (a);i>=(b);--i)
#define endl '\n'
using namespace std;
typedef long long ll;
const int maxn = 4e4+10;//40000
int T,n;
int v[40+10];
double dp[40+1][maxn],p;
int main(){
    scanf("%d",&T);
    int ans = -1;
    while(T--){
        scanf("%d%lf",&n,&p);
        rep(i,1,n) scanf("%d",&v[i]);
        rep(i,1,n) rep(j,0,maxn-1) dp[i][j] = 0;
        dp[0][0] = 1;
        rep(i,1,n)per(j,maxn-1,0){
            dp[i][j] += dp[i-1][j]*0.5;
            dp[i][j + v[i]] += dp[i-1][j]*0.5;
        }
        double sum = 0;
        for(int i = 0;i<maxn;++i){
            sum += dp[n][i];
            if(sum >= p){
                ans = i;break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/tea-egg/p/11817032.html