Robberies

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

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
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
2
4
6
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  1203 2159 2844 1171 1864
 
这题是01背包的灵活变形,需要懂一点概率知识,就是独立事件的计算公式,是相乘。
样例很具有迷惑性,让你觉得只要把概率扩大100倍,然后硬套01背包就可以了,其实不是这样,数据会有很多位小数,而且同时抢几家银行被抓的概率也并不是几个概率相加所以如果拿概率当容量,最大抢钱数当价值,列出f[j]=max(f[j], f[j-p[i]]+m[i])肯定是错的。
 
所以就要转换思路,把概率当成价值,抢钱数当成容量。然后就用两种做法,1) f[i]表示抢劫i钱的最大不被抓概率, 2)f[i]表示抢劫i钱的最小被抓概率  两种最后都要通过一个循环判断出零界为止的那个i的值,就是最终答案。
 
1)

#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345
#define M 12 float f[N],p[N],P;
int m[N],n; int main()
{
// freopen("1.txt", "r", stdin);
// freopen("2.txt", "w", stdout);
int T;cin>>T;
while(T--)
{
int sum=;
scanf("%f%d",&P,&n);
for(int i=;i<=n;i++)
{
scanf("%d%f",&m[i],&p[i]);
sum+=m[i];
}
memset(f,,sizeof(f));//初始化
f[]=1.0;//边界,抢劫0钱不被抓的概率是1 for(int i=;i<=n;i++)
{
for(int j=sum;j>=m[i];j--)
{
f[j]=max(f[j], f[j-m[i]]*(-p[i]) );//独立事件相乘
}
} for(int i=sum;i>=;i--)
{
// printf("%f ",f[i]);
if(f[i]>=(-P))//找到第一不被抓概率大于等于(1-P)的i就是答案
{
printf("%d\n",i);
break;
}
}
}
return ;
}

2)

#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345 float f[N],p[N],P;
int m[N],n; int main()
{
int T;cin>>T;
while(T--)
{
int sum=;
scanf("%f%d",&P,&n);
for(int i=;i<=n;i++)
{
scanf("%d%f",&m[i],&p[i]);
sum+=m[i];
} for(int i=;i<N-;i++)//初始化
f[i]=1.0;
f[]=0.0;//边界,抢劫0钱的被抓概率是0 for(int i=;i<=n;i++)
{
for(int j=sum;j>=m[i];j--)
{
f[j]=min(f[j], -(-f[j-m[i]])*(-p[i]) );//概率知识,独立事件,对立事件
}
} for(int i=sum;i>=;i--)
{
// printf("%f ",f[i]);
if(f[i]<=P)//找到第一个概率小于等于安全概率P的i,就是答案
{
printf("%d\n",i);
break;
}
}
}
return ;
}

最新文章

  1. 【原】彻底解决WPS弹出热点广告、WPS购物图标的办法
  2. 使用R语言-RStudio快捷键
  3. Css Js Loader For Zencart
  4. Nginx启动报错: could not open error log file: open() &amp;q
  5. SequoiaDB数据库的一般概念介绍
  6. asp.net mvc4 Controller与Action执行过程的研究(学习笔记)
  7. [转]easyui常用控件及样式收藏
  8. JDNI
  9. 关于jquery选择器中:first和:first-child和:first-of-type的区别及:nth-child()和:nth-of-type()的区别
  10. CSS(三)背景 list-style display visibility opacity vertical cursor
  11. java笔记---- 获取外网(公网)的ip地址
  12. 用JS来判断版本号比如v21.2.2.2和v21.2.2.2.15
  13. 打印文章 FZOJ 5190
  14. Linux常用命令之文件搜索命令
  15. CentOS入门
  16. Postgresql - jsonb_pretty &amp; dateStyle
  17. 这款 WordPress商用插件 0day 漏洞满满,且已遭利用
  18. 使用Xutils 3 中遇到的一些问题!!!!
  19. linux运维常见英文报错中文翻译(菜鸟必知)
  20. JDBC删除表实例

热门文章

  1. bluej
  2. AbstractFactory(抽象工厂模式)
  3. python 中zip()函数的使用
  4. HDU 3341 Lost&#39;s revenge
  5. A. Test for Job
  6. 【Python】SyntaxError: Non-ASCII character &#39;\xe8&#39; in file
  7. hiho week 143
  8. 在table第一行前插入一行
  9. 九度oj题目1009:二叉搜索树
  10. [UOJ#128][BZOJ4196][Noi2015]软件包管理器