lightoj 1079 Just another Robbery 概率 背包

题目中给的都是被逮捕的概率p,并不方便计算,所以统统通过1-p,变成q,表示安全的概率。

然后这么多银行可以选择,有些类似背包问题。开始下意识认为应该是安全概率算体积,金钱算价值,更符合直观想法。但安全概率不是整数,这样子没法dp。

但是背包有个技巧,有时候体积可能是直观上的价值,我们不妨把金钱作为体积试试。

dp[i][j]表示,考虑前i个银行,拿了j的金钱,的最大安全概率。

dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - v[i]] * q[i]),我们再滚动数组一下就好了。倒叙遍历dp[i],输出第一个大于1-P即可。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 int n,T,cas;
 7 int v[105];
 8 double P;
 9 double p[105],q[105],dp[10005];
10 int main()
11 {
12     for (scanf("%d",&T);T != 0;T--)
13     {
14         cas++;
15         memset(dp,0,sizeof(dp));
16         scanf("%lf%d",&P,&n);
17         for (int i = 1;i <= n;i++)
18         {
19             scanf("%d%lf",&v[i],&p[i]);
20             q[i] = 1 - p[i];
21         }
22         dp[0] = 1;
23         for (int i = 1;i <= n;i++)
24             for (int j = 10000;j >= 0;j--)
25                 if (j >= v[i])
26                     dp[j] = max(dp[j],dp[j - v[i]] * q[i]);
27         for (int i = 10000;i >= 0;i--)
28             if (dp[i] >= 1 - P)
29             {
30                 printf("Case %d: %d\n",cas,i);
31                 break;
32             }
33     }
34     return 0;
35 }

原文地址:https://www.cnblogs.com/iat14/p/11410379.html

时间: 2024-08-29 00:25:56

lightoj 1079 Just another Robbery 概率 背包的相关文章

LightOJ 1079 Just another Robbery (01背包)

题意:给定一个人抢劫每个银行的被抓的概率和该银行的钱数,问你在他在不被抓的情况下,能抢劫的最多数量. 析:01背包,用钱数作背包容量,dp[j] = max(dp[j], dp[j-a[i] * (1.0 - pp[i])),dp[i] 表示不被抓的最大概率,在能抢劫到 i 个钱. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <stri

LightOJ - 1079 Just another Robbery 概率 + dp

题目大意:harry要去抢劫银行,他手上有每个银行布局,所以他很清楚的了解到每个银行有多少钱,和去抢劫该银行被捕的概率 现在他要拟定一个计划,要求在被捕概率低于p的情况下,抢劫到最多的钱 解题思路:银行1个1个的抢过去,抢到的钱随着抢劫银行的数量增加或者不变(不变的情况就是不抢劫该银行),被捕的概率随着银行数量的增加而增大或者不变(不变的情况就是不抢劫该银行) 所以这个题有三个量在改变,一个是银行的数量,一个是抢到的钱,还有一个是被捕的概率 所以用二维dp来表示三个变量,设dp[i][j]表示抢

lightoj 1079 Just another Robbery 解题心得

原题: Description As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - H

[LightOJ 1079] Just another Robbery

Just another Robbery As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friend

(概率 01背包) Just another Robbery -- LightOJ -- 1079

http://lightoj.com/volume_showproblem.php?problem=1079 Just another Robbery As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated r

hdoj 1203 I NEED A OFFER! 【另类01背包】【概率背包】

题意:... 策略:动态规划. 因为是求至少能得到一个offer的概率,那我们可以反着求,求得不到一个offer的概率,最后用1减去就好了. 代码: #include<string.h> #include<stdio.h> double dp[10010]; struct node{ int a; double b; }s[10010]; int main() { int n, m, i, j; while(scanf("%d%d", &n, &

LightOJ1079---Just another Robbery (概率做01背包)

As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - Hermione and Ron

LightOJ - 1079 概率dp

题意:n个银行,每个有价值和被抓概率,要求找被抓概率不超过p的最大价值 题解:dp[i][j]表示前i个取j价值的所需最小概率,01背包处理,转移方程dp[i][j]=min(dp[i-1][j],dp[i-1][j-v[i]]+(1-dp[i-1][j-v[i]])*p) #include<bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define pi

hdu 2955 Robberies(概率背包)

Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22658    Accepted Submission(s): 8358 Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that