暑期dp46道(6)抢劫Robberies ——HDOJ 2955

来源:互联网 发布:埃米特矩阵 编辑:程序博客网 时间:2024/05/17 04:33

<span style="font-family: Arial, Helvetica, sans-serif; font-size: 18px; background-color: rgb(255, 255, 255);">题目链接:<span style="color: rgb(51, 102, 255);">http://acm.hdu.edu.cn/showproblem.php?pid=2955</span><span style="color: rgb(255, 0, 0);">(01背包)</span></span>

思路:刚开始想裸背包,求在安全概率内的最大金钱数,但是这题的浮点位数不能确定,而且概率不是相加的,所以就应该反过来想,求获得i金钱的最大安全概率,

然后枚举,记录满足条件的最大i值.

w[i]表示第i个银行能抢到的金额,c[i]表示在第i个银行抢劫的安全概率(各银行间相互独立)

ans[j]表示获取数量为j的金钱的最大安全概率,剩下的就是01背包,

ans[j]=Max(ans[j],ans[j-c[i]]*w[i])//注意是“*”

要注意安全概率初始化,这个没什么说的

PS:01背包是逆向枚举

附上代码:

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>#include<stack>#include<cmath>#include<limits>#include<string>using namespace std;#define LL long long#define debug 0const int maxn=100+5;int w[maxn];double ans[10010],c[maxn];int caseNum,n,v;double total;void Do(){    //printf("%d\n",n);    for(int i=1; i<=n; i++)        for(int j=v; j>=w[i]; j--)        {            if(ans[j]<ans[j-w[i]]*c[i])                ans[j]=ans[j-w[i]]*c[i];        }    //printf("%d\n",n);    for(int i=v; i>=0; i--)    {        if(ans[i]>total)        {            printf("%d\n",i);            break;        }    }}int main(){#if debug    freopen("in.txt","r",stdin);#endif // debug    scanf("%d",&caseNum);    while(caseNum--)    {        //printf("%d\n",caseNum);        //memset(ans,0,sizeof(ans));        //ans[0]=1;        v=0;        scanf("%lf %d",&total,&n);        total=1-total;        for(int i=1; i<=n; i++)        {            scanf("%d %lf",&w[i],&c[i]);            v+=w[i];//记录最大金额,用于后来的背包计算            c[i]=1-c[i];        }        for(int i=1; i<=v; i++)        {            ans[i]=0;        }        ans[0]=1;        Do();    }    return 0;}

0 0
原创粉丝点击