Hdu 4815 Little Tiger vs. Deep Monkey(dp)

Problem 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!

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]

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

Sample Input
1
3 0.5
1 2 3

Sample Output
3

题目大意:有n道题,每道题分别有对应的分数,求概率大于p最少的分数!
解题思路:dp,dp[i][j]表示前i道题得j分得概率
状态转移方程为:
dp[i+1][j+num[i]] += dp[i][j]*0.5; 该题正确
dp[i+1][j] += dp[i+1][j]*0.5; 该题错误
这个时候老铁们可能要问了,为什么是+而不是直接=;这是因为每到题目的分数不同,有可能不同的组合产生同样的分数,这个时候该分数出现的概率就应该相加了!

代码如下:

#include <stdio.h>
#include <string.h>

double dp[41][40010];

int main()
{
    int T,n;
    double p;
    scanf("%d",&T);
    while(T--)
    {
        memset(dp,0,sizeof(dp));        //每次都需要初始化,避免对下一次的数据产生影响
        scanf("%d%lf",&n,&p);
        int i,num[45];
        for(i = 0 ; i < n ; i++)
            scanf("%d",&num[i]);
        dp[0][0] = 1;                   //注意初始化
        int j,sum = 0;
        for(i = 0 ; i < n ; i++)
        {
            sum += num[i];
            for(j = 0 ; j <= sum ; j++)
            {
                if(dp[i][j])            //这个是一定要判断的,表示前i题能出现j分
                {
                    dp[i+1][j+num[i]] += dp[i][j]*0.5;
                    dp[i+1][j] += dp[i][j]*0.5;
                }
            }
        }
        double ans = 0;
        for(j = 0 ; j <= sum ; j++)
        {
            ans += dp[n][j];
            if(ans >= p)
            {
                printf("%d\n",j);
                break;
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shuati2000/article/details/99941849