HDU 4815

B - B
Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u

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[i][j]表示猴子做了前i题得到j分的概率,转移方程:
做对当前题:dp[i + 1][j + val[i+ 1]] += dp[i][j] * 0.5
做错当前题:dp[i + 1][j] += dp[i][j] * 0.5
最后枚举累加dp[n][j]的值,当其大于等于p时,此时的j就是老虎最少要得的分,很好理解,因为j是从小往大枚举的,一旦前面概率的和>= p时,老虎得j分,而这之前猴子得的分都小于等于j,因此老虎不败的可能性就是p,初始状态dp[0][0] = 1,做0题得0分概率为1

代码;

#include<stdio.h>
#include<string.h>
int a[44];
double dp[44][40006];//dp[i][j]表示做完前i道题得到j分的概率; 
int main()
{
	int t,n;
	double p;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%lf",&n,&p);
		int sum=0;
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++)//最好从1开始存; 
		{
			scanf("%d",&a[i]);
			sum+=a[i];
		}
		dp[0][0]=1.0;//做0道题(不做题)得0分的概率为1; 
		for(int i=0;i<=n-1;i++)
		{
			for(int j=0;j<=sum-a[i+1];j++)
			{
				if(dp[i][j])//dp[i][j]为真说明做对i道题能够得到j分,然后才能在dp[i][j]的基础上计算做对i+1道题的概率; 
				{
					dp[i+1][j+a[i+1]]=dp[i][j]*0.5;
				    dp[i+1][j]=dp[i][j]*0.5;
				}
			}
		}
		int j=0;
		double p1=0;
		for(j=0;j<=sum;j++)
		{
			p1+=dp[n][j];//累加做完N道题所得各种分数的概率和; 
			if(p1>=p)
			break;
		}
		printf("%d\n",j);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值