首页 > ACM题库 > HDU-杭电 > HDU 4815-Little Tiger vs. Deep Monkey-动态规划-[解题报告]HOJ
2015
09-18

HDU 4815-Little Tiger vs. Deep Monkey-动态规划-[解题报告]HOJ

Little Tiger vs. Deep Monkey

问题描述 :

A crowd of little animals is visiting a mysterious laboratory �C 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!�

输入:

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

输出:

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

样例输入:

1
3 0.5
1 2 3

样例输出:

3

题意:  n个题目,每个题目答对或打错, 分数一个有2^n种可能, 求最小的数x, 使得这些可能的数中 x what score should I get at least so that I will not lose in the contest with probability of at least P?

背包处理所有情况,二分查找x即可 。


typedef  long long LL ;
int  x[48] ;
LL   dp[40008] ;
int  num[40008] ;
LL   cnt[40008] ;

int  main(){
     double p ;
     int  sum , n  , i , j , k  , t ;
     cin>>t ;
     while(t--){
          scanf("%d%lf" , &n , &p) ;
          sum = 0 ;
          for(i = 1 ; i <= n ; i++){
                scanf("%d" , &x[i]) ;
                sum += x[i] ;
          }
          memset(dp , 0 , sizeof(dp)) ;
          dp[0] = 1 ;
          for(i = 1 ; i <= n ; i++){
              for(j = sum ; j >= x[i] ; j--){
                  if(dp[j - x[i]]) dp[j] += dp[j-x[i]] ;
              }
          }
         k = 0 ;
         for(i = 0 ; i <= sum ; i++){
             if(dp[i] == 0)  continue ;
             k++ ;
             num[k] =  i ;
             cnt[k] =  dp[i] ;
         }
         double all = pow(2.0 , n)  , s ;
         int L = 0 , R = sum  , M  , ans  ;
         while(L <= R){
              M = (L + R) >> 1 ;
              s = 0 ;
              for(i = 1 ; i <= k ; i++){
                   if(num[i] > M) break ;
                   s += cnt[i] ;
              }
              if(s >= p * all){
                   ans = M ;
                   R = M-1 ;
              }
              else  L = M+1 ;
         }
         printf("%d\n" , ans) ;
     }
     return 0  ;
}

参考:http://blog.csdn.net/u013491262/article/details/31762839