題意:有A,B兩個人,n道題目,每題有對應的分數,B答對題目的概率是0.5,求A不輸給B的概率不小於P要拿的最低分數
思路:DP,dp[i][j]來表示B答了前i題後分數為j的概率,,然後通過B的概率求A的最低分數
#include#include #include #include using namespace std; const int MAXN = 40010; int a[MAXN],n; double P,dp[50][MAXN]; int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%lf", &n, &P); int sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum += a[i]; } memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for (int i = 0; i < n; i++) for (int j = 0; j <= sum-a[i]; j++) if (dp[i][j] > 0) { dp[i+1][j+a[i]] += dp[i][j]*0.5; dp[i+1][j] += dp[i][j]*0.5; } int ans = 0; double cnt = 0; for (int i = 0; i <= sum; i++) { cnt += dp[n][i]; if (cnt >= P) { ans = i; break; } } printf("%d\n", ans); } return 0; }