Just another Robbery LightOJ - 1079

Just another Robbery LightOJ - 1079

题目大意

有$n$家银行,每家银行被抢后抓的概率是$p_{i}$,可获得的金钱是$a_{i}$,问你在被抓几率最高为$P$的情况下可获得的最大金钱数目是多少

解题思路

将危险率转化为安全率,那么即求在安全程度最低为$1-P$的情况下可获得的最大金钱数目,将金钱看成体积,安全程度看成重量,那么问题被转换成为了概率下的01背包问题,$dp[i]$表示在获取金钱数目为$i$时的安全程度,其状态转移方程即为:

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-6
const int maxn = 1e5 + 10;
double dp[maxn];
struct Node {
int money;
double safe;
Node() {
money = 0;
safe = 0.0;
}
}a[110];
int main() {
int T;
scanf("%d", &T);
int cas = 0;
while (T--) {
double Safe;
int n;
scanf("%lf%d", &Safe, &n);
Safe = 1.0 - Safe;
int sum = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d%lf", &a[i].money, &a[i].safe);
a[i].safe = 1.0 - a[i].safe;
sum += a[i].money;
}
memset(dp, 0, sizeof(dp));
dp[0] = 1.0;
for (int i = 1; i <= n; ++i) {
for (int j = sum; j >= a[i].money; --j) {
dp[j] = max(dp[j], dp[j - a[i].money] * a[i].safe);
}
}
for (int i = sum; i >= 0; --i) {
if (dp[i] >= Safe) {
printf("Case %d: %d\n", ++cas, i);
break;
}
}
}
return 0;
}