HDU_2955 Robberies

该博客讨论了一个与概率和优化相关的问题,即如何在不超过特定被捕概率的情况下,从多个银行中选择抢劫以获得最大收益。通过将每次抢劫的金额视为背包问题中的物品重量,将不被捕的概率视为物品价值,可以将问题转化为01背包问题。博客提供了样例输入和输出,并强调了独立事件概率的处理。解决方案涉及动态规划,寻找能够使总金额最大化且被捕概率低于给定阈值的抢劫组合。

Robberies

链接

HDU_2955 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<T100
0.0 ≤ P ≤ 1.0 0.0 \le P \le 1.0 0.0P1.0
0 < N ≤ 100 0 < N \le 100 0<N100
0 < M j ≤ 100 0 < M_j \le 100 0<Mj100
0.0 ≤ P j ≤ 1.0 0.0 \le P_j \le 1.0 0.0Pj1.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 1pj )当做“物品”的“价值”,这个问题就是一个简单的01背包问题。最后,所有满足 f j ≤ 1 − p f_j \le 1-p fj1p 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(1pj),而不是 ∑ j = 1 n ( 1 − p j ) \sum_{j=1}^n(1-p_j) j=1n(1pj)

代码

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值