HDU 4815 概率dp,背包

Little Tiger vs. Deep Monkey

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


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
 
一直没做过概率dp竟有些zz了,搞好久才看懂胜负的概率都是1/2,dp[i][j]表示处理前i件题目得到j分的概率,
那么有  dp[i][j]+=dp[i-1][j-a[i]]*0.5       j>=a[i];
             dp[i][j]+=dp[i-1][j]*0.5;
这两个式子分别表明了买和不买第i件物品得到j分的概率,加在一起就是前i件物品获得j分的概率了,加法和乘法定律要搞清楚。
这里我用了滚动数组优化,注意概率不同于价值,即使不买也会有一定的概率得到,所以j要循环到0;
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define eps 1e-8
 4 double dp[40005];
 5 int main()
 6 {
 7     int n,m,i,j,t,k;
 8     int a[45];
 9     double P;
10     cin>>t;
11     while(t--){int s=0;cin>>n>>P;
12     memset(dp,0,sizeof(dp)); dp[0]=1.0;
13     for(i=1;i<=n;++i) cin>>a[i],s+=a[i];
14     for(i=1;i<=n;++i){
15         for(j=s;j>=0;--j){
16             dp[j]=dp[j]/2;
17             if(j>=a[i]) dp[j]+=dp[j-a[i]]/2;
18         }
19     }
20      double x=0;
21      for(i=0;i<=s;++i){
22         x+=dp[i];
23         if(x>=P||(fabs(x-P)<=eps)){cout<<i<<endl;break;}
24      }
25     }
26     return 0;
27 }
 
 
原文地址:https://www.cnblogs.com/zzqc/p/7246741.html