hdu 2955 Robberies 01背包 ★★☆

来源:互联网 发布:win10美化mac os 编辑:程序博客网 时间:2024/06/07 11:27


Robberies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17691    Accepted Submission(s): 6531


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.


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.


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 . 
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.

Notes and Constraints
0 < T <= 100
0.0 <= P <= 1.0
0 < N <= 100
0 < Mj <= 100
0.0 <= Pj <= 1.0
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
 

Source
IDI Open 2009
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  2602 1203 2159 2844 1171 
 



01背包,被抓住的概率应该是一连串概率的乘积,因为把概率当作背包体积不好计算,这个题目把得到的钱数当作背包的体积,生还的概率当作价值。






#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<cctype>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI (4.0*atan(1.0))#define eps 1e-7#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   ind<<1,le,mid#define rson    ind<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mk    make_pair#define _f     first#define _s     secondusing namespace std;//const int INF=    ;typedef long long ll;//const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);const int INF =0x3f3f3f3f;const int maxn=100+20    ;//const int maxm=    ;int V,n;double up;double p[maxn];int cost[maxn];double dp[100000+20];int main(){    int T;scanf("%d",&T);    while(T--)    {      scanf("%lf%d",&up,&n);      V=0;      // V=(int )tmp*100000;这样是错的      // V=(int )(tmp*100000);这样是对的    //  cout<<V<<endl;      for(int i=1;i<=n;i++)      {          scanf("%d%lf",&cost[i],&p[i]);          V+=cost[i];      }      memset(dp,0, (V+1)*sizeof (dp[0]) );//(V+1)      dp[0]=1;      for(int i=1;i<=n;i++)      {          for(int v=V;v>=cost[i];v--)          {              dp[v]=max(dp[v],dp[v-cost[i]]*(1-p[i]) );          }      }         for(int v=V;v>=0;v--)      {          if(1-dp[v]<=up)          {            printf("%d\n",v);            break;          }      }    }    return 0;}


0 0
原创粉丝点击