Little Tiger vs. Deep Monkey(01背包)

Little Tiger vs. Deep Monkey

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3715    Accepted Submission(s): 1276

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

题目意思:  求解最小的得分以至少为p的不会输;

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;
int t;
int n;
int arr[50];
double dp[50][40010];     //几道题 几分的 概率
double pp;

int main(){
    cin>>t;
    while(t--){
        cin>>n>>pp;
        for(int i=0;i<n;i++){
            cin>>arr[i];
        }
        memset(dp,0,sizeof(dp));
        dp[0][0]=1.0;   //0道题0分的概率为1
        for(int i=0;i<n;i++){
            for(int j=0;j<n*1000;j++){
                dp[i+1][j]+=dp[i][j]*0.5;   //得分为j的总数
                dp[i+1][j+arr[i]]+=dp[i][j]*0.5;
            }
        }
        double res=0;
        int flag=0;
        for(int i=n*1000;i>=0;i--){   //注意0也是一种得分
            res+=dp[n][i];
            if(res>1-pp) {flag=i;break;}
        }
        cout << flag << endl;
    }
    return 0;
}

第一种

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;
int t;
int n;
int arr[50];
double dp[50][40010];     //几道题 几分的 概率
double pp;

int main(){
    cin>>t;
    while(t--){
        memset(dp,0,sizeof(dp));
        dp[0][0]=1.0;   //0道题0分的概率为1
        cin>>n>>pp;
        for(int i=0;i<n;i++){
            cin>>arr[i];
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n*1000;j++){
                dp[i+1][j]+=dp[i][j]*0.5;   //得分为j的总数
                dp[i+1][j+arr[i]]+=dp[i][j]*0.5;
            }
        }
        double res=0;
        int flag=0;
        for(int i=n*1000;i>=0;i--){   //注意0也是一种得分
            res+=dp[n][i];
            if(res>1-pp) {flag=i;break;}
        }
        cout << flag << endl;//要用flag标记一下  有可能没有打那输出0
    }
    return 0;
}

第二种

原文地址:https://www.cnblogs.com/qq-1585047819/p/11687499.html

时间: 2024-10-17 21:33:36

Little Tiger vs. Deep Monkey(01背包)的相关文章

hdu 4815 Little Tiger vs. Deep Monkey(01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=4815 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 recogn

HDU4815 Little Tiger vs. Deep Monkey——0-1背包

题目描述 对于n道题目,每道题目有一个分值,答对加分,答错不得分,你要和一个叫深猴的比赛,题目你可以假设成判断题(不是对就是错),深猴对于所有的题目都是随机选择一个答案,而你是有脑子的,求为了不输掉比赛(平局或你获胜)的可能性至少为p时你至少需要得到多少分,有t组数据,每次输入两行,第一行为n,p(有n道题目,n<=40, 不会输的可能性为p,0.0<=p<=1.0),第二行输入n个1~1000的整数,代表这n道题分别答对能获得的分数 样例输入 1 3 0.5 1 2 3 样例输出 3

HDOJ 4815 Little Tiger vs. Deep Monkey

递推... Little Tiger vs. Deep Monkey Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 661    Accepted Submission(s): 244 Problem Description A crowd of little animals is visiting a mysterious labo

HDU 4815 Little Tiger vs. Deep Monkey 背包问题

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4815 题意:很"内涵"的一个题面,题意是给出N道题,和一个概率P,然后给出每道题对应的得分aa[i](每道题只有两个选项,一个正确一个错误).两个人来答题,一个人是随机选择答案,问另一个人至少要答多少分才能保证有P的概率不会失败. 思路:是一道DP题,最开始想强行枚举所有情况,找到需要分数,后来发现40道题强行枚举2^40必然超时,思路就中断了,后来想到DP,刚开始DP的可能得到的分数,但是很

HDU 4815 Little Tiger vs. Deep Monkey

http://acm.hdu.edu.cn/showproblem.php?pid=4815 题意: 给定N个题目各自的分数a[i] A有50%的概率答对一道题目得到相应分数,B想要在至少P的概率上总分不低于A 问B至少要得到多少分 解法: 概率DP dp[i][j]表示前i题得到j分的概率 递推式: dp[i][j] = dp[i-1][j] * 0.5 (a[i]>j) dp[i][j] = (dp[i-1][j] + dp[i-1][j - a[i]]) * 0.5 (a[i]<=j)

习题:刷图(01背包)

洛谷2297 题目描述 Loidc是一个勤奋的孩子. 他每天都会勤奋的搬砖刷疲劳,每天都会期待着各种BOSS给他爆点什么神奇的东西(例如魔剑..),但是每次刷远古总会坑到连门票钱也赚不回来. 最近loidc新技能get√ 他每天都会朗诵一遍线段树,将自己升级为"Deep Dark Fantasy"模式(其实是每天去给腾讯爹送钱)下才去搬砖,所以他人品次次爆发,每次刷图后腾讯都会象征性给他爆点好东西. 今天他又来刷一个叫做"痛苦之村列瑟芬"的图,每次刷完图后都会有N个

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

17-又见01背包

/*                                        又见01背包时间限制:1000 ms  |  内存限制:65535 KB难度:3 描述        有n个重量和价值分别为wi 和 vi 的 物品,从这些物品中选择总重量不超过 W     的物品,求所有挑选方案中物品价值总和的最大值.    1 <= n <=100    1 <= wi <= 10^7    1 <= vi <= 100    1 <= W <= 10^

HDU - 2602 Bone Collector(01背包讲解)

题意:01背包:有N件物品和一个容量为V的背包.每种物品均只有一件.第i件物品的费用是volume[i],价值是value[i],求解将哪些物品装入背包可使价值总和最大. 分析: 1.构造二维数组:dp[i][j]---前i件物品放入一个容量为j的背包可以获得的最大价值. dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - volume[i]] + value[i]);---(a) (1)dp[i - 1][j]---不放第i件物品,因此前i件物品放入一个容量为