HDU - 2955 Robberies 【0-1背包】

本文探讨了一个银行抢劫场景下的数学优化问题,通过将银行抢劫转化为背包问题,利用动态规划求解在限定被捕风险下,如何最大化抢劫金额。文章详细介绍了问题转换的思路与AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955

                                      Robberies

                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

 

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

解题思路

这题很容易想成:以最大被抓概率为背包最大容量,将银行作为商品,抢一个银行的被抓概率为商品体积,银行里的钱为商品价格。 这有两个问题:①被抓概率为浮点数,且不知道小数点后几位(double16位必然爆内存)无法开数组;②被抓概率不是简单的相加,(因为你第一次逃掉的概率为A,第二次逃掉的概率为B,那么两次都逃掉的概率是A*B不是A+B)。

那么我们不妨以所有银行的钱数总和为背包最大容量,以抢某个银行的钱作为商品体积,以抢完(后)这个银行后的逃跑概率作为商品价值(背包模型中数组的值代表能得到的最大价值,这里数组的值代表能跑掉的最大概率 )。那么我们从小到大枚举背包容量(钱),当商品价值(逃跑概率)大于最小逃跑概率时,所对应的背包容量(钱)就是可以得到的最优解(能抢的最多钱)。

AC代码

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct bank {
	double w;
	int c; 
}b[101];
double escape[10005];	//当抢了i元时的逃跑概率 
int main()
{
	int T,N;
	double P;
	cin >> T;
	while( T--)
	{
		cin >> P >> N;
		int sum=0;	//所有银行的钱 
		for(int i=0;i<N;i++) {
			cin >> b[i].c >> b[i].w;
			sum += b[i].c; 
		}
		memset(escape,0,sizeof escape);	//刚开始认为只要抢了钱,你就跑不掉 
		escape[0] = 1;	//啥都没抢凭啥抓你?所以逃跑概率当然是1啦 
		for(int i=0;i<N;i++) 
			for(int j=sum;j>=b[i].c;j--)	//抢你一块钱或者把你的钱都抢了你都要抓我,对强盗来说当然要全抢才可能是最优解 
				escape[j] = max(escape[j],escape[j-b[i].c]*(1-b[i].w));	//多种抢钱方法得到的钱一样,我当然去抢逃跑概率大的那种 
		
		for(int i=sum;i>=0;i--)
			if(escape[i] > double(1-P)) {	//再多抢就跑不掉了 
				cout << i << endl;
				break;
			} 
	}
} 

 

基于数据挖掘的音乐推荐系统设计与实现 需要一个代码说明,不需要论文 采用python语言,django框架,mysql数据库开发 编程环境:pycharm,mysql8.0 系统分为前台+后台模式开发 网站前台: 用户注册, 登录 搜索音乐,音乐欣赏(可以在线进行播放) 用户登陆时选择相关感兴趣的音乐风格 音乐收藏 音乐推荐算法:(重点) 本课题需要大量用户行为(如播放记录、收藏列表)、音乐特征(如音频特征、歌曲元数据)等数据 (1)根据用户之间相似性或关联性,给一个用户推荐与其相似或有关联的其他用户所感兴趣的音乐; (2)根据音乐之间的相似性或关联性,给一个用户推荐与其感兴趣的音乐相似或有关联的其他音乐。 基于用户的推荐和基于物品的推荐 其中基于用户的推荐是基于用户的相似度找出相似相似用户,然后向目标用户推荐其相似用户喜欢的东西(和你类似的人也喜欢**东西); 而基于物品的推荐是基于物品的相似度找出相似的物品做推荐(喜欢该音乐的人还喜欢了**音乐); 管理员 管理员信息管理 注册用户管理,审核 音乐爬虫(爬虫方式爬取网站音乐数据) 音乐信息管理(上传歌曲MP3,以便前台播放) 音乐收藏管理 用户 用户资料修改 我的音乐收藏 完整前后端源码,部署后可正常运行! 环境说明 开发语言:python后端 python版本:3.7 数据库:mysql 5.7+ 数据库工具:Navicat11+ 开发软件:pycharm
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值