HDU 2955 Robberies(01背包变形)

时间:2021-03-23 23:11:04

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.

HDU 2955 Robberies(01背包变形)
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 &lpar;01背包)

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

  3. hdu 2955 Robberies 0-1背包&sol;概率初始化

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

  4. HDU——2955 Robberies &lpar;0-1背包&rpar;

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

  5. hdu 2955 Robberies &lpar;01背包好题&rpar;

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

  6. HDU 2955 Robberies&lpar;01背包&rpar;

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

  7. HDOJ 2955 Robberies &lpar;01背包&rpar;

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

  8. HDU 2955 【01背包&sol;小数&sol;概率DP】

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

  9. HDOJ&period;2955 Robberies &lpar;01背包&plus;概率问题&rpar;

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

随机推荐

  1. MySQL基础之索引

    这段时间看了好多东西却没有总结,今天在这里写一写 关于索引 索引是一种提高查询效率的方法,它是B+树的结构,从根到中间节点在到叶子节点,无需遍历全部就可以查到所需的东西 关于索引的创建 一般有俩种方式 ...

  2. 隐藏原生html5 video controls

    隐藏原生html5 video controls 凤凰视频焦点项目mobile html5播放器测试时bug,由于没有用原生的controls而是自己写的custom controls,虽然设置了co ...

  3. JavaScript的面向对象编程(OOP)(三)——聚合

    之前写过了类和原型,这里再说聚合,在写关于聚合之前,对与继承我再总结一下.JavaScript中关于继承的方式一共有三种,之前写了两种,但是没有说明,这里补充说明一下. 1.类式继承:通过在函数对象内 ...

  4. Maven如何传递系统属性变量到TestNG

    本文介绍如何传递Maven pom.xml里的系统属性参数到TestNG,文章沿用笔者一贯的风格--例子驱动. 解决什么问题 1. 用过WebDriver的都知道,当你启动Chrome或IE的时候都需 ...

  5. HTML5 canvas beginPath&lpar;&rpar; 方法

    beginPath() 方法开始一条路径,或重置当前的路径.w3school上的解释! 路径是canvas里很重要的一个概念,刚开始学canvas的人对路径理解不是很深,他们在用canvas的时候会乱 ...

  6. iptables之链之间的跳转

    创建一个新的链     按照管理,用户自定义的链用小写来区分它们 iptables -N newchain 可以在这个链的尾部跳转到INPUT链 iptables -A newchain -j INP ...

  7. ExtJS001HelloWorld弹窗

    html页面 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...

  8. PCB行业版特色功能展示

    普实PCB行业版,专为PCB行业需求而定制.秉承一体化.集团化.移动化为设计理念,采用互联网技术.云计算技术.移动应用技术开发的新一代系统帮助PCB企业创新管理模式.引领商业变革!系统从接到订单开始, ...

  9. 【图文】如何在centos上安装tomcat

    先到tomcat官网下载安装包(随便下载你想要的版本) 假设你现在使用的是windows系统 那么就把你下载来的压缩包解压,放到一个目录中 在你本地的windows系统中安装个xshell和xftp ...

  10. 万能五笔输入法&quot&semi;&commat;相反

    经过查找资料,发现是因为键盘使用的英国导致,改为美国解决,其他输入法没发现这个问题.