hdu4815(0-1背包)

本文介绍了一个基于深度学习的智力竞赛场景,小老虎与DeepMonkey进行一场包含多个二选一问题的竞赛,目标是最少获得多少分数以确保不低于特定概率获胜。通过0-1背包算法计算在随机答题情况下,所需最低分数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HDU4815

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个问题,每个题只有两个选项,答对的概率为1/2,一个人随机答题,要问另一个人必须至少有多少分才能保证有p的概率不会输. 

分析:首先答问题所有的可能为all=2^n种,假设另一个人得分为x,就是要求小于等于x的得分出现次数有num(x)次,要保证num(x)/all>=p,那么可以用0-1背包记录小于等于x分的得分出现次数,再从0-40000(最大得分40*1000)开始遍历,找到满足num(x)/all>=p的最小得分。

ac code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[42],dp[40005];
int main()
{
    ll n;
    double p;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof dp);
        scanf("%lld%lf",&n,&p);
        for(ll i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        ll sum=0,tmp=(1ll<<n);//如果不用1ll会爆,因为左移n位得到的结果会跟1类型保持一致
        dp[0]=1;
        for(int i=1;i<=n;i++)
            for(int j=40000;j>=a[i];--j)
                 dp[j]+=dp[j-a[i]];
        ll ans=0;
        for(int i=0;i<=40000;i++)
        {
            sum+=dp[i];
            if(1.0*sum/tmp>=p){
                ans=i;
                break;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值