標籤:
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.51 2 3
Sample Output
3 題目大意:n道題。每道題有一定的分值(如果答對一道題就會獲得相應的分數,答錯該題則沒分),兩個人來答題A隨機答題,問B最少得拿多少分才能保證有p的機率不會輸(即有1-p的機率會贏) 01背包問題,dp[i][j]表示答i道題共得j分的機率,那麼答第i+1答題有兩種情況,有可能答對也有可能答錯,各佔0.51.第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\n", m); } return 0;}
hdu 4815 Little Tiger vs. Deep Monkey(01背包)