1022 of dp

来源:互联网 发布:网络主播年收入 编辑:程序博客网 时间:2024/06/07 09:48

Problem V

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 27   Accepted Submission(s) : 10
Problem Description
The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, before retiring to a comfortable job at a university.<br><br><center><img src=../../../data/images/con211-1010-1.jpg></center> <br>For a few months now, Roy has been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.<br><br><br>His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.
 

Input
The first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj and a floating point number Pj . <br>Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .
 

Output
For each test case, output a line with the maximum number of millions he can expect to get while the probability of getting caught is less than the limit set.<br><br>Notes and Constraints<br> 0 < T <= 100<br> 0.0 <= P <= 1.0<br> 0 < N <= 100<br> 0 < Mj <= 100<br> 0.0 <= Pj <= 1.0<br> A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.
 

Sample Input
30.04 31 0.022 0.033 0.050.06 32 0.032 0.033 0.050.10 31 0.032 0.023 0.05
 

Sample Output
246
 题目要求:roy想要偷银行,每偷一次都会增加一些犯罪率,要求在一定犯罪率之内获得最大的钱数。并且偷每所银行犯罪率相互独立。
 解题思路:其实是背包问题的变形,因为直接求偷几家银行被逮的概率不易求,但但可以求不被抓的概率,这样就容易计算了,只要在不小于某个不被抓的概率下拿最多的钱。抢多家银行的概率是想乘的。银行的总价值为背包容积,其状态转移方程为d[v]=max(d[v],d[v-a[i]]*p[i])其中概率p[v-a[i])*p[i]>p才合法,所以最后从后往前进行检索,当那个最先合法一定是最大值,输出d[i]中的i即可。
解题代码:
#include<stdio.h>#include<iostream>#include<string.h>using namespace std;double d[10005],p[10000];int a[10005];int main(){    int i,j,t,m,sum;    double n;    cin>>t;    while(t--){    scanf("%lf%d",&n,&m);       n=1-n;sum=0;      for(i=0;i<m;i++)      {          scanf("%d%lf",&a[i],&p[i]);          sum+=a[i];          p[i]=1-p[i];  //不能得到Offer的概率      }      for(i=0;i<=sum;i++)          d[i]=0.0;d[0]=1;          for(i=0;i<m;i++)            for(j=sum;j>=a[i];j--)              if(d[j]<d[j-a[i]]*p[i])                  {d[j]=d[j-a[i]]*p[i];}                  for(i=sum;i>=0;i--)                    if(d[i]-n>1e-8)                    {cout<<i<<endl;                    break;}    }    return 0;}

 
0 0
原创粉丝点击