HDU 2955 Robberies 01背包概率DP 表格详解

Robberies

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20669Accepted Submission(s): 7656

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.

Alt

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

AC

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

struct bank {
	int v;
	double p;
} banks[105];

double dp[10005];//注意这里一定要考虑金钱最多的情况,100*100 

int main() {
	int cas,m,sum;
	double rt;
	scanf("%d",&cas);
	while(cas--) {
		
		sum=0;
		memset(dp,0,sizeof dp);
		memset(banks,0,sizeof banks);
		
		scanf("%lf %d",&rt,&m);
		for(int i=1; i<=m; i++) {
			scanf("%d %lf", &banks[i].v , &banks[i].p );
			
			banks[i].p=1-banks[i].p;
			
			sum+=banks[i].v;
		}
		//dp[j]  表示抢到 j 钱逃脱的最大概率
		
		
		dp[0] = 1;//不抢钱不被抓
		
		for (int i = 1; i <= m; i++) {
			for (int j = sum; j >= banks[i].v; j--) {
				dp[j] = max(dp[j], dp[j - banks[i].v] * banks[i].p);
			}
		}

//			for (int j = sum; j >= 0; j--) {
//				printf("%-16lf",dp[j]);
//			}
//			printf("\n");
		for (int i = sum; i >= 0; i--) {
			if (dp[i] >= 1-rt) {
				printf("%d\n",i);
				break;
			}
		}
	}
}

思路:
直接拿概率当背包,拿银行当物品套dp模板不行,即便我们用概率p x 100得到整数好让 j++, 虽然这样做给的样例数据是对的,但是保不齐某个概率的位数很多,像0.03333,我们乘100的话只能达到33,但是这个精度有问题啊,万一题目给的概率精度很高呢,就好比浮点型的数据比大小,位数少还好,位数多了精度要求就高,所以直接用概率p x 100是牵强的做法。

换种思路:
用偷的钱 i 做背包,我们来找最大的逃脱概率 dp[i]。只要在保证能逃脱的前提下偷的钱最大,就是我们的解。

一开始可能不好理解,因为这个太逆向了,所以画个表格就好懂了。

先看要偷的钱那一排,我们拿第一组样例来说明:目前有三个银行,总的钱数是sum=6(1+2+3),我们先不考虑被不被抓的问题,我们小偷先考虑我到底要偷多少钱,如果我要偷6万,那么三个银行都得偷,那么我要逃脱起来就可能很困难,要三个银行都不被抓,是三个事件发生的概率相乘,等于
banks[1].p 乘上 banks[2].p 乘上 banks[3].p
比允许的逃脱概率0.96低啊,就会被抓。所以我们回头考虑偷5万,发现偷5万也不安全,再看4万,3,万,,哦终于发现了,只偷2万才能脱身(小偷:MMP),所以能偷的最大钱数是2万。

具体数据看表格

在这里插入图片描述

这道题一开始做也是想直接套dp,就是抱着侥幸心理认为所有概率都是两位小数,结果就WA了,看来做题不能有这种侥幸心理啊

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值