[Hdoj 4815] Little Tiger vs. Deep Monkey

链接:http://acm.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


题目大意:首先输入t(1 < t <= 10)表示样例个数,对每个样例输入n (1 <= n <= 40)、p(p∈[0, 1]),给定n个题目,每个题目有一个分数(1 < score <= 1000),答对得分,答错不得分。小虎和傻猴进行比赛答题,每个题目都要答,总分高者取胜。对傻猴而言,每个题目答对和答错的概率相等。求小虎至少需要得到多少分数(必须是能得到的分数)才能至少有p的概率不输给傻猴。


分析:话不多说先想暴力为敬,但是n = 40时枚举所有答题情况复杂度为2^40显然不行了。于是只能考虑其他做法。分析题目,对于n个问题,答题的方案数为2 ^ n,要使至少有p的概率不输给傻猴,小虎的得分就应当大于等于总分前p / (2 ^ n)小的答题情况。因此要解出此题便是要知道某个答题情况所得分数在所有的情况中的排名。很容易想到是数据结构或是递推了,由于数据结构上没什么想法,并且总分最大才40 * 1000= 40000,考虑到dp。定义状态state[i][j]表示回答前i个问题得到j分的方案数, s[i]表示回答正确第i个问题的得分。初始化state[i][j] = j == 0, 递推state[i][j] += state[i - 1][j - s[i]],时间复杂度为O(n * totscore),第一维可以使用滚动数组优化掉。最后注意方案数可能爆int,故dp数组用long long存储。


代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[40001];
int s[40];
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        double p;
        cin >> n >> p;
        for (int i = 0; i < n; i++)
            cin >> s[i];
        memset(dp, 0, sizeof dp);
        dp[0] = 1;
        for (int i = 0; i < n; i++)
            for (int j = 40000; j >= s[i]; j--)
                dp[j] += dp[j - s[i]];
        ll num = 0, tot = 1LL << n;
        for (int i = 0; i <= 40000; i++)
        {
            num += dp[i];
            if (1.0 * num / tot >= p)
            {
                cout << i << endl;
                break;
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值