Robberies
链接
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
T
T, the number of cases. For each scenario, the first line of input gives a floating point number
P
P
P, the probability Roy needs to be below, and an integer
N
N
N, the number of banks he has plans for. Then follow
N
N
N lines, where line
j
j
j gives an integer
M
j
M_j
Mj and a floating point number
P
j
P_j
Pj.
Bank
j
j
j contains Mj millions, and the probability of getting caught from robbing it is
P
j
P_j
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 < T \le 100
0<T≤100
0.0
≤
P
≤
1.0
0.0 \le P \le 1.0
0.0≤P≤1.0
0
<
N
≤
100
0 < N \le 100
0<N≤100
0
<
M
j
≤
100
0 < M_j \le 100
0<Mj≤100
0.0
≤
P
j
≤
1.0
0.0 \le P_j \le 1.0
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
解析
把每次抢劫的数目当做“物品”的“重量”,每次抢劫逃跑的概率(即 1 − p j 1-p_j 1−pj )当做“物品”的“价值”,这个问题就是一个简单的01背包问题。最后,所有满足 f j ≤ 1 − p f_j \le 1-p fj≤1−p 的 f j f_j fj 中最大的 j j j 就是答案。
特别要注意的是,由于每次抢银行事件是相互独立的,所以 n n n 次抢银行逃跑的概率是 ∏ j = 1 n ( 1 − p j ) \prod_{j=1}^n(1-p_j) ∏j=1n(1−pj),而不是 ∑ j = 1 n ( 1 − p j ) \sum_{j=1}^n(1-p_j) ∑j=1n(1−pj)。
代码
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int t,n,mon[101],tot,ans;
float p,pro[101],f[10001];
int main()
{
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
memset(f,0,sizeof(f));
tot=ans=0;
cin>>p>>n;
for(int j=1;j<=n;j++)
{
cin>>mon[j]>>pro[j];
tot+=mon[j];
}
f[0]=1;
for(int j=1;j<=n;j++)
for(int k=tot;k>=mon[j];k--)
f[k]=max(f[k],f[k-mon[j]]*(1-pro[j]));
for(int j=1;j<=tot;j++)
if(f[j]>=1-p)
ans=max(ans,j);
printf("%d\n",ans);
}
return 0;
}
该博客讨论了一个与概率和优化相关的问题,即如何在不超过特定被捕概率的情况下,从多个银行中选择抢劫以获得最大收益。通过将每次抢劫的金额视为背包问题中的物品重量,将不被捕的概率视为物品价值,可以将问题转化为01背包问题。博客提供了样例输入和输出,并强调了独立事件概率的处理。解决方案涉及动态规划,寻找能够使总金额最大化且被捕概率低于给定阈值的抢劫组合。
1万+

被折叠的 条评论
为什么被折叠?



