LightOJ 1079 Just another Robbery

$01$背包,概率。

要所有银行都不被抓住才算不抓住,有一个银行被抓住了就算是被抓住了。所以要计算反面。$dp[i]$表示抢到$i$元钱不被抓住的最大概率。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\in.txt","r",stdin);
    freopen("D:\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar();
    x = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
}

int T,n,m[200];
double p,q[200];
double dp[10010];

int main()
{
    scanf("%d",&T); int cas=1;
    while(T--)
    {
        scanf("%lf%d",&p,&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%lf",&m[i],&q[i]);
            q[i]=1.0-q[i];
        }
        p=1-p;

        memset(dp,0,sizeof dp); dp[0]=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=10000;j>=m[i];j--)
            {
                dp[j]=max(dp[j],dp[j-m[i]]*q[i]);
            }
        }

        int ans=0;
        for(int i=10000;i>=0;i--)
        {
            if(dp[i]>p) ans=max(ans,i);
        }
        printf("Case %d: %d
",cas++,ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/6290329.html