集训第六周 数学概念与方法 概率 N题

N - 概率
Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status

Description

As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - Hermione and Ron have decided upon a tolerable probabilityP of getting caught. They feel that he is safe enough if the banks he robs together give a probability less than P.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a real number P, the probability Harry needs to be below, and an integer N (0 < N ≤ 100), the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj (0 < Mj ≤ 100) and a real number Pj . Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj. A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.

Output

For each case, print the case number and the maximum number of millions he can expect to get while the probability of getting caught is less than P.

Sample Input

3

0.04 3

1 0.02

2 0.03

3 0.05

0.06 3

2 0.03

2 0.03

3 0.05

0.10 3

1 0.03

2 0.02

3 0.05

Sample Output

Case 1: 2

Case 2: 4

Case 3: 6

题意:哈利波特毕业了,由于没找到工作,他想去抢银行,他的两个基友为他算了一卦,告诉他如果他被抓的概率小于或等于P,他就会很安全,否则就一定会被抓。

于是哈利波特去调查了一下,记录每个银行的存钱量和被抓的风险,要求你计算他最多能抢多少钱

先转化一下:改限制为不被抓的概率大于等于(1-P),再将这个问题转化为背包01问题

dp(i)代表抢i枚大钱不被抓的概率

dp (i)=max { dp ( i - a[j] ) *( 1- p[j] ) , dp[i] }  (i--0~所有银行的钱,j遍历所有银行)

#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
const int maxn=110;
double dp[maxn*maxn],P,b[maxn];
int n,a[maxn],sum,ca=1;
void Init()
{
    cin>>P>>n;
    sum=0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i]>>b[i];
        sum+=a[i];
    }
    memset(dp,0,sizeof(dp));
}

void Work()
{
    dp[0]=1;
    for(int i=0;i<n;i++)
    {
        for(int j=sum;j>=a[i];j--)
        {
            dp[j]=max(dp[j-a[i]]*(1-b[i]),dp[j]);
        }
    }
}

void Print()
{
    cout<<"Case "<<ca++<<": ";
    for(int i=sum;i>=0;i--)
    {
        if(dp[i]>(1-P))
        {
            cout<<i<<endl;
            break;
        }
    }
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
     Init();
     Work();
     Print();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zsyacm666666/p/4744536.html