LightOJ - 1079 Just another Robbery 概率 + dp

题目大意:harry要去抢劫银行,他手上有每个银行布局,所以他很清楚的了解到每个银行有多少钱,和去抢劫该银行被捕的概率

现在他要拟定一个计划,要求在被捕概率低于p的情况下,抢劫到最多的钱

解题思路:银行1个1个的抢过去,抢到的钱随着抢劫银行的数量增加或者不变(不变的情况就是不抢劫该银行),被捕的概率随着银行数量的增加而增大或者不变(不变的情况就是不抢劫该银行)

所以这个题有三个量在改变,一个是银行的数量,一个是抢到的钱,还有一个是被捕的概率

所以用二维dp来表示三个变量,设dp[i][j]表示抢劫了前i个银行,抢了j的钱被捕的概率

那么dp[i][j] = min(dp[i-1][j],dp[i-1][j-money[i]] + (dp[i-1][j-money]) * rob[i])

其中money[i]表示第i个银行的有多少钱,rob[i]表示抢劫第i个银行被捕的概率

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const double esp = 1e-7;
const int N = 110;
const int M = 10010;
int money[N], sum, n;
double rob[N],dp[N][M], p;
void solve() {
    for(int i = 1; i <= sum; i++)
        dp[0][i] = (double)-1;
    dp[0][0] = 0;

    for(int i = 1; i <= n; i++)
        for(int j = 0; j <= sum; j++) {
            dp[i][j] = dp[i-1][j];
            if(j - money[i] >= 0 && fabs(dp[i-1][j-money[i]] + (double)1) > esp) {
                if(fabs(dp[i][j] + (double)1) > esp)
                    dp[i][j] = min(dp[i][j], dp[i-1][j-money[i]] + (1 - dp[i-1][j-money[i]]) * rob[i]);
                else
                    dp[i][j] = dp[i-1][j-money[i]] + (1 - dp[i-1][j-money[i]]) * rob[i];
            }
        }
}

int main() {
    int test, cas = 1;
    scanf("%d", &test);
    while(test--) {
        scanf("%lf%d", &p, &n);
        sum = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d%lf", &money[i], &rob[i]);
            sum += money[i];
        }
        solve();
        for(int i = sum; i >= 0; i--) {
            if(dp[n][i] >= 0.0 && p - dp[n][i] > esp) {
                printf("Case %d: %d\n", cas++, i);
                break;
            }
        }
    }
    return 0;
}
时间: 2024-08-29 12:19:49

LightOJ - 1079 Just another Robbery 概率 + dp的相关文章

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]),我们再

LightOJ 1065 Island of Survival (概率DP?)

题意:有 t 只老虎,d只鹿,还有一个人,每天都要有两个生物碰面,1.老虎和老虎碰面,两只老虎就会同归于尽 2.老虎和人碰面或者和鹿碰面,老虎都会吃掉对方 3.人和鹿碰面,人可以选择杀或者不杀该鹿4.鹿和鹿碰面,没事问人存活下来的概率 析:最后存活肯定是老虎没了,首先可以用概率dp来解决,dp[i][j] 表示 还剩下 i 考虑, j 只鹿存活的概率是多少. 然后每次分析这几种情况即可. 还有一种思路就是只要考虑老虎没了,只要老虎没了就能存活,只要计算老虎全死完的概率就好,首先如果老虎是奇数,是

LightOJ 1265 Island of Survival 概率DP

H - Island of Survival Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1265 Description You are in a reality show, and the show is way too real that they threw into an island. Only two kinds o

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

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 1248-G - Dice (III) (概率dp)

题意:给你n个面的骰子,问扔出所有面的期望次数. 虽然这题挺简单的但还是要提一下.这题题目给出了解法. E(m)表示得到m个不同面的期望次数. E(m+1)=[((n-m)/n)*E(m)+1]+(m/n)*E(m+1); 想必((n-m)/n)*E(m)+1这个很好理解吧,当得到m个面时他有((n-m)/n)的概率得到没得到过的面 而(m/n)*E(m+1)不太好理解为什么,其实题目已经给出解释了,如果他有(m/n)的概率出到不同 面也有1-(m/n)的概率得到相同面,所以直接加上((n-m)

(概率 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

Lightoj 1038 - Race to 1 Again (概率DP)

题目链接: Lightoj  1038 - Race to 1 Again 题目描述: 给出一个数D,每次可以选择数D的一个因子,用数D除上这个因子得到一个新的数D,为数D变为1的操作次数的期望为多少? 解题思路: 概率DP咯,对于只知道期望是:E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn)的窝,拿这个题目没有一点办法.然后看了讨论版,发现总会有一些神人存在. 求操作次数的期望时,先设定第i个因子给期望的贡献为Ti,那么有:E = (T1 + T2 + T3