#概率01背包# HDU 2955 Robberies

题目链接

Robberies

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

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

 

30.04 31 0.022 0.033 0.050.06 32 0.032 0.033 0.050.10 31 0.032 0.023 0.05

Sample Output

 

24


 

题目描述:

T组数据,第一行给出一个总安全概率P和n家银行,接下来n行,每行给出一个价值v和一个概率p分别表示每家银行的价值和不被抓的概率,在保证总概率小于等于总安全概率P的情况下尽可能获得的最大价值。

Solution:

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

代码:

#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;
}

猜你喜欢

转载自blog.csdn.net/Jasmineaha/article/details/81048539