#概率01背包# HDU 2955 Robberies

题目链接

Robberies

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.

 

Description

T组数据,每组数据第一行是总被抓概率p(最后求得的总概率必须小于他,否则被抓),然后是想抢的银行数n。然后n行,每行分别是该银行能抢的钱数m[i]和被抓的概率p[i],求最大逃跑概率。被抓的概率越大,逃跑概率越小。

 

Solution

01背包的变形题:dp[i][j] 表示抢前 i 个银行,抢了 j 钱时的最大安全概率

dp[i][j] = max(dp[i][j], dp[i][j - val[i]] * (1 - p[i]))

Code:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MaxN = 100005;

int val[MaxN];
double p[MaxN], dp[MaxN];
double P;
int n, v;

void init() {
	scanf("%lf %d", &P, &n);
	v = 0;  //所有银行的总价值
	for(int i = 1; i <= n; i++) {
		scanf("%d %lf", &val[i], &p[i]);
		v += val[i];
	}
	for(int i = 0; i <= v; i++) dp[i] = 0;
	dp[0] = 1; //没抢劫银行时是绝对安全的,概率为1
}

int main() {
	int t; scanf("%d", &t);
	while(t--) {
		init();
		for(int i = 1; i <= n; i++) {
			for(int j = v; j >= val[i]; j--) {
				dp[j] = max(dp[j], dp[j - val[i]] * (1 - p[i])); //偷到j钱时的最大安全概率
			}
		}
		for(int i = v; i >= 0; i--) { //保证在安全的前提下抢到的最大价值
			if((1 - dp[i]) <= P) {
				printf("%d\n", i);
				break;
			}
		}
	}
	return 0;
}

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值