HDU 4815 Little Tiger vs. Deep Monkey

来源:互联网 发布:python new 单例 编辑:程序博客网 时间:2024/06/01 10:09

Little Tiger vs. Deep Monkey

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1433 Accepted Submission(s): 501

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

Source
2013 Asia Regional Changchun

题目意思感觉很难理解。说的是求 小老虎赢了猴子的概率不小于P的时候的最小分数。猴子答题的正确率是二分之一。所以我们看成小老虎答对的概率也是二分之一。
输入解释:输入N和P,N就是题目数量,接下来输入的是每道题的分值。
解题思路:计算老虎赢了猴子的概率,就是 老虎赢的所有情况总和/老虎答题的所有情况 。老虎答题的所有情况种数就是 2 的N次方。所以。老虎赢的所有情况就是 P*(2^n).那么如何计算老虎赢的所有情况呢,就是假设老虎总得分为1赢的种数加上总得分为2……3……4……5….
所以这里就是个DP,DP[I]表示老虎总的分为I的时候赢的种数,可以推出关系式:dp[i] = dp[i-num[j]]+dp[i];就是背包问题,判断j这道题分数放不放上去。

#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <functional>#include <numeric>#include <sstream>#include <stack>#include <map>#include <queue>using namespace std;long long  dp[100000];long long make_pow(long long x,long long n){    long long res = 1;    while(n>0)    {        if(n&1)res = res*x;        x = x*x;        n>>=1;     }     return res; } int main(){    long long T;    cin >> T;    while(T--)    {        long long n;        double p;        scanf("%lld%lf",&n,&p);         long long num[100];        long long sum = 0;        for(int i=0;i<n;i++)        {            scanf("%lld",&num[i]);            sum+=num[i];        }         memset(dp,0,sizeof(dp));        dp[0] = 1;        long long all = make_pow(2,n);        for(int i = 0;i<n;i++)        {            for(int j = sum;j>=num[i];j--)            {                dp[j] = dp[j-num[i]]+dp[j];            }        }        long long need =ceil(all*p);        long long cnt = 0;         for(int i=0;i<=sum;i++)        {            cnt+=dp[i];             if(cnt>=need)            {                printf("%d\n",i);                 break;             }         }     }    return 0;}
0 0
原创粉丝点击