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 ;
}

HDU 2955 Robberies(01背包变形)的更多相关文章

  1. HDU 2955 Robberies --01背包变形

    这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即 ...

  2. hdu 2955 Robberies (01背包)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:一开始看急了,以为概率是直接相加的,wa了无数发,这道题目给的是被抓的概率,我们应该先求出总的 ...

  3. hdu 2955 Robberies 0-1背包/概率初始化

    /*Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  4. HDU——2955 Robberies (0-1背包)

    题意:有N个银行,每抢一个银行,可以获得\(v_i\)的前,但是会有\(p_i\)的概率被抓.现在要把被抓概率控制在\(P\)之下,求最多能抢到多少钱. 分析:0-1背包的变形,把重量变成了概率,因为 ...

  5. hdu 2955 Robberies (01背包好题)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. HDU 2955 Robberies(01背包)

    Robberies Problem Description The aspiring Roy the Robber has seen a lot of American movies, and kno ...

  7. HDOJ 2955 Robberies (01背包)

    10397780 2014-03-26 00:13:51 Accepted 2955 46MS 480K 676 B C++ 泽泽 http://acm.hdu.edu.cn/showproblem. ...

  8. HDU 2955 【01背包/小数/概率DP】

    Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  9. HDOJ.2955 Robberies (01背包+概率问题)

    Robberies 算法学习-–动态规划初探 题意分析 有一个小偷去抢劫银行,给出来银行的个数n,和一个概率p为能够逃跑的临界概率,接下来有n行分别是这个银行所有拥有的钱数mi和抢劫后被抓的概率pi, ...

随机推荐

  1. 关于django form验证是否用户名已存在

    想通过django的Form模块进行数据库中是否已存在用户名的验证,首先我先调用了数据库用户名字段所有的值,发现是个queryset对象. 随后经过查询后发现queryset查询集对象可以调用list ...

  2. Matplotlib基本图形之条形图2

    Matplotlib基本图形之条形图2 1.绘制多条条形图 示例代码: import time import numpy as np import matplotlib.pyplot as plt b ...

  3. HDU-1392 Surround the Trees,凸包入门!

    Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...

  4. 九度oj 题目1537:买卖股票

    题目描述: 给定一个大小为n的数组,数组的元素a[i]代表第i天的股票价格. 设计一个算法,计算在最多允许买卖k次(一买一卖记为一次)的条件下的最大收益. 需要注意的是,你不能同时拥有两份股票.也就是 ...

  5. Tomcat基础配置(一)

    详情请看散尽浮华的tomcat相关配置技巧梳理 本次只用于自己的查看,谢谢作者的谅解. tomcat常用架构:1)nginx+tomcat:即前端放一台nginx,然后通过nginx反向代理到tomc ...

  6. hdfs api读写文写件个人练习

    看下hdfs的读写原理,主要是打开FileSystem,获得InputStream or OutputStream: 那么主要用到的FileSystem类是一个实现了文件系统的抽象类,继承来自org. ...

  7. POJ 1741 Tree ——点分治

    [题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...

  8. PHP中的验证码类(准备篇)

    <!--code.php内容--> <?php //开启session session_start(); include "vcode.class.php"; / ...

  9. msp430入门学习41

    msp430的其他九 msp430入门学习

  10. HDU 1358字符串循环节问题 ,next数组

    求字符串循环节,要求每前i个字符串前缀是否循环,有的话打印出来. 我对j=next[i]数组(未优化,从0开始,第一个为-1,)理解:字符s[i]的前面的字符串,最长的相同的前缀和后缀 的长度,因此, ...