hdu 4815 Little Tiger vs. Deep Monkey(01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=4815

Description

A crowd of little animals is visiting a mysterious laboratory � The Deep Lab of SYSU. 

“Are you surprised by the STS (speech to speech) technology of Microsoft Research and the cat face recognition project of Google and academia? Are you curious about what technology is behind those fantastic demos?” asks the director of the Deep Lab. “Deep learning, deep learning!” Little Tiger raises his hand briskly. “Yes, clever boy, that’s deep learning (深度学习/深度神经网络)”, says the director. “However, they are only ‘a piece of cake’. I won’t tell you a top secret that our lab has invented a Deep Monkey (深猴) with more advanced technology. And that guy is as smart as human!” 

“Nani ?!” Little Tiger doubts about that as he is the smartest kid in his kindergarten; even so, he is not as smart as human, “how can a monkey be smarter than me? I will challenge him.” 

To verify their research achievement, the researchers of the Deep Lab are going to host an intelligence test for Little Tiger and Deep Monkey. 

The test is composed of N binary choice questions. And different questions may have different scores according to their difficulties. One can get the corresponding score for a question if he chooses the correct answer; otherwise, he gets nothing. The overall score is counted as the sum of scores one gets from each question. The one with a larger overall score wins; tie happens when they get the same score. 

Little Tiger assumes that Deep Monkey will choose the answer randomly as he doesn’t believe the monkey is smart. Now, Little Tiger is wondering “what score should I get at least so that I will not lose in the contest with probability of at least P? ”. As little tiger is a really smart guy, he can evaluate the answer quickly. 

You, Deep Monkey, can you work it out? Show your power!�/div>
 

Input

The first line of input contains a single integer T (1 ≤ T ≤ 10) indicating the number of test cases. Then T test cases follow. 

Each test case is composed of two lines. The first line has two numbers N and P separated by a blank. N is an integer, satisfying 1 ≤ N ≤ 40. P is a floating number with at most 3 digits after the decimal point, and is in the range of [0, 1]. The second line has N numbers separated by blanks, which are the scores of each question. The score of each questions is an integer and in the range of [1, 1000]�/div>
 

Output

For each test case, output only a single line with the answer.
 

Sample Input

 
3 0.5
1 2 3
 

Sample Output

3
 
 
 
题目大意:n道题。每道题有一定的分值(如果答对一道题就会获得相应的分数,答错该题则没分),两个
人来答题A随机答题,问B最少得拿多少分才能保证有p的概率不会输(即有1-p的概率会赢)
 
01背包问题,dp[i][j]表示答i道题共得j分的概率,那么答第i+1答题有两种情况,有可能答对也有可能答错,各占0.5
1.第i+1答题答对,则得分概率为的dp[i+1][j+a[i]] ;(a[i]表示第i+1道题的分数,因为i是从0开始)
2.第i+1答题答错,则得分概率为的dp[i+1][j];
统计道n道题的各种得分情况概率之和x,然后当x满足条件x>1-p时便可输出此时得分;
 
 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>

using namespace std;

const int N = 50;
const int M = 40010;

int a[N];
double dp[N][M];

int main()
{
    int t, n;
    double p;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%lf", &n, &p);
        for(int i = 0 ; i < n ; i++)
            scanf("%d", &a[i]);
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;//写0道题得0分的概率为1
        for(int i = 0 ; i < n ; i++)
        {
            for(int j = 0 ; j < n * 1000 ; j++)
            {
                dp[i + 1][j] += dp[i][j] * 0.5;//第i+1道题没有写对,没有拿到这道题的分
                dp[i + 1][j + a[i]] += dp[i][j] * 0.5;//第i+1道题写对了,拿到了这道题的分
            }
        }
        double x = 0;
        int m = 0;
        for(int j = n * 1000 ; j >= 0 ; j--)
        {
            x += dp[n][j];//写n道题各个得分的概率和
            if(x > 1 - p)
            {
                m = j;
                break;
            }
        }
        printf("%d
", m);
    }
    return 0;
}