【01背包求方案数】HDU4815-Little Tiger vs. Deep Monkey
2016-08-22 14:32
411 查看
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4815
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
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
/*
题意:
给出N道题,和一个概率P,然后给出每道题对应的得分s[i]
(每道题只有两个选项,一个正确一个错误)。
两个人来答题,一个人A是随机选择答案,
问另一个人B至少要答多少分才能保证有P的概率不会失败
分析:
题目意思理解起来很容易,但是要当作一个问题去处理,却很抽象了。
问B得S分才能保证P的概率不会输;
我们换句话理解:A得小于S分的概率大于等于P;
这样我们的问题就转换成了求A得S分的概率;
对于A,随机选择答案,每种答案只有两种选择,对或者错,就成了01背包了;
当我们是求概率,所以,转换成了01背包计算方案数的题目了;
*/
const int maxS=40005; // 最大分数;
const int maxN=45;
int n;
double p;
long long dp[maxS];
int s[maxN];
void init()
{
memset(dp,0,sizeof(dp));
dp[0]=1;
}
void ZeroOnePack()
{
for(int i=0;i<n;i++){
for(int j=maxS-1;j>=s[i];j--){
dp[j]+=dp[j-s[i]];
}
}
}
void solve()
{
long long cnt=0; // 统计小于等于S分的总方案数;
long long sum=1ll<<n; // A答题的总方案数;
for(int i=0;i<maxS;i++){
cnt+=dp[i];
if((cnt*1.0/sum)>=p){
printf("%d\n",i);
break;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%lf",&n,&p);
for(int i=0;i<n;i++)
scanf("%d",&s[i]);
ZeroOnePack();
solve();
}
return 0;
}
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
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
/*
题意:
给出N道题,和一个概率P,然后给出每道题对应的得分s[i]
(每道题只有两个选项,一个正确一个错误)。
两个人来答题,一个人A是随机选择答案,
问另一个人B至少要答多少分才能保证有P的概率不会失败
分析:
题目意思理解起来很容易,但是要当作一个问题去处理,却很抽象了。
问B得S分才能保证P的概率不会输;
我们换句话理解:A得小于S分的概率大于等于P;
这样我们的问题就转换成了求A得S分的概率;
对于A,随机选择答案,每种答案只有两种选择,对或者错,就成了01背包了;
当我们是求概率,所以,转换成了01背包计算方案数的题目了;
*/
const int maxS=40005; // 最大分数;
const int maxN=45;
int n;
double p;
long long dp[maxS];
int s[maxN];
void init()
{
memset(dp,0,sizeof(dp));
dp[0]=1;
}
void ZeroOnePack()
{
for(int i=0;i<n;i++){
for(int j=maxS-1;j>=s[i];j--){
dp[j]+=dp[j-s[i]];
}
}
}
void solve()
{
long long cnt=0; // 统计小于等于S分的总方案数;
long long sum=1ll<<n; // A答题的总方案数;
for(int i=0;i<maxS;i++){
cnt+=dp[i];
if((cnt*1.0/sum)>=p){
printf("%d\n",i);
break;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%lf",&n,&p);
for(int i=0;i<n;i++)
scanf("%d",&s[i]);
ZeroOnePack();
solve();
}
return 0;
}
相关文章推荐
- 杭电-4815-Little Tiger vs. Deep Monkey(01背包的运用)
- hdu 4815 Little Tiger vs. Deep Monkey(01背包)
- -----hdu 4815 -01背包- Little Tiger vs. Deep Monkey
- HDU4815 Little Tiger vs. Deep Monkey (母函数应用)
- [hdu4815]Little Tiger vs. Deep Monkey
- hdu4815- Little Tiger vs. Deep Monkey-概率dp或者背包计数
- hdu 4815 Little Tiger vs. Deep Monkey
- 【HDU】4815 - Little Tiger vs. Deep Monkey(母函数)
- HDOJ 4815 Little Tiger vs. Deep Monkey(概率DP)
- HDU 4815 Little Tiger vs. Deep Monkey 2013 长春现场赛C题
- HDU 4815 Little Tiger vs. Deep Monkey 背包问题
- HDU - 4815 Little Tiger vs. Deep Monkey (长春赛区C题)
- HDU 4815 Little Tiger vs. Deep Monkey 01背包
- Live Archive 6705 Little Tiger vs. Deep Monkey
- hdu 4815 Little Tiger vs. Deep Monkey 概率dp
- HDU 4815 Little Tiger vs. Deep Monkey(母函数)
- HDU 4815 Little Tiger vs. Deep Monkey
- HDU 4815 Little Tiger vs. Deep Monkey(2013长春现场赛C题)
- [hdu 4815]Little Tiger vs. Deep Monkey 01背包
- HDU 4815 Little Tiger vs. Deep Monkey (0/1背包应用)