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 have decided upon a tolerable probability P of getting caught. They feel that he is safe enough if the banks he robs together give a probability less than P.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a real number P, the probability Harry needs to be below, and an integer N (0 < N ≤ 100), the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj (0 < Mj ≤ 100) and a real number Pj . Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj. A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.

Output

For each case, print the case number and the maximum number of millions he can expect to get while the probability of getting caught is less than P.

Sample Input

Output for Sample Input

3

0.04 3

1 0.02

2 0.03

3 0.05

0.06 3

2 0.03

2 0.03

3 0.05

0.10 3

1 0.03

2 0.02

3 0.05

Case 1: 2

Case 2: 4

Case 3: 6

Note

For the first case, if he wants to rob bank 1 and 2, then the probability of getting caught is 0.02 + (1 - 0.02) * .03 = 0.0494 which is greater than the given probability (0.04). That’s why he has only option, just to rob rank 2.

概率做01背包

dp[i]表示抢到i元时不被抓到的最大概率

/*************************************************************************
    > File Name: d.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年04月29日 星期三 20时07分38秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 10010;
double dp[N];
double p[N];
int w[N];

int main() {
    int t;
    int icase = 1;
    scanf("%d", &t);
    while (t--) {
        int sum = 0;
        double P;
        int n;
        scanf("%lf%d", &P, &n);
        for (int i = 1; i <= n; ++i) {
            scanf("%d%lf", &w[i], &p[i]);
            p[i] = 1.0 - p[i];
            sum += w[i];
        }
        for (int i = 0; i <= sum; ++i) {
            dp[i] = 0;
        }
        dp[0] = 1.0;
        for (int i = 1; i <= n; ++i) {
            for (int j = sum; j >= w[i]; --j) {
                if (dp[j - w[i]]) {
                    dp[j] = max(dp[j], dp[j - w[i]] * p[i]);
                }
            }
        }
        printf("Case %d: ", icase++);
        for (int i = sum; i >= 0; --i) {
            if (P >= 1 - dp[i]) {
                printf("%d\n", i);
                break;
            }
        }
    }
    return 0;
}
时间: 2024-08-03 19:19:30

LightOJ1079---Just another Robbery (概率做01背包)的相关文章

hdu2955---Robberies(概率做01背包)

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

[再做01背包] POJ 3624 Charm Bracelet

接触动态规划的第一题是数塔问题,第二题就是01背包问题了. 当时看的懵懵懂懂,回过头来再看这道题还是非常简单的了. 用 dp[i][j] 表示取前i种物品,使它们总体积不超过j的最优取法取得的价值总和状态转移方程:dp[i][j] = max(dp[i-1][j],dp[i-1][j-cost[i]]+weight[i]) 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstri

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

[CF837D] Round Subset(滚动数组,01背包)

题目链接:http://codeforces.com/contest/837/problem/D 题意:n个数里选k个数,使得它们的乘积末尾0个数最多. 只需要统计每个数的2和5的数量,一个作为容量,一个作为价值.f(i,k,j)表示前i个数选k个,一共有j个2的时候,5最多有几个. 外层枚举前i个数,内层做01背包就可以.但是会MLE,所以滚动数组. 特别注意的是,滚动数组在滚动的时候要拷贝整层,原因是我在更新01背包的时候没有及时复制... 还有f要注意当前层一定要从之前存在的状态更新过来,

POJ2923--Relocation(01背包+状压dp)

果然对状压DP,我根本就不懂=.= /************************************************** Problem: 2923 User: G_lory Memory: 720K Time: 157MS Language: G++ Result: Accepted **************************************************/ #include <iostream> #include <cstring>

hdu 2670 01背包变形

题意:有n个男孩,每个男孩对女神都有一个love值Li和递减值Bi(love值每天递减这么多).女神要从这n个男孩中选出k个男孩来一起去玩耍(每天选择一个男孩),要使这k个男孩的love值之和最大. 分析:当选定的男孩一定时,肯定要尽早选择递减较快的男孩,所以先按照递减值由大到小排序,然后做01背包即可,花费是占一个人数(n个人中选择k个),价值是那一天的love值. 总结一句话就是:排序然后求一个恰好装满的01背包. 1 #include <algorithm> 2 #include <

PKU 2184 Cow Exhibition 01背包

题意: 有一些牛,每头牛有一个Si值,一个Fi值,选出一些牛,使得max( sum(Si+Fi) ) 并且 sum(Si)>=0, sum(Fi)>=0 思路: 随便选一维做容量(比如Fi),另一维做价值,然后直接做01背包. 做的时候注意一下方向. 最后,在合法解里面找一下最优解就好了. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib>

【POJ1014】Dividing 多重背包,二进制物品拆分转01背包

直接做01背包,即把物品数量累加,做20000物品的01背包指定TLE,不用我说了吧! 本文的优化是二进制优化,O(logn),至于完全背包记录已使用个数的O(n)算法本文不进行讲解,在博客的"背包"分类里. 二进制优化: 大家知道一个十进制数可以转换成二进制,那么假设某种物品有1023种,即2^10-1,二进制为111111111,则可以视为每一位分别是一个由{1,2,4,8,16,32,64,128,256,512}个物品糅合成的大物品,二进制数每一位0表示不取,1表示取,这样我们

csu 1547(01背包)

1547: Rectangle Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 996  Solved: 277[Submit][Status][Web Board] Description Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now you need find a big enough rectangle( 2 * m)