HDU2955 背包DP

Robberies

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

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

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

Sample Output

2

4

6

Source

IDI Open 2009

题意:

一人去抢银行,n个银行,在每个银行能够得到的钱mj,被抓住的概率pj,抢所有银行被抓住的概率p,问在总概率不超过p时得到的最大钱数。

代码:

 1 //由于存在概率的乘法,用普通的01背包肯定不行,可以以总钱数作为背包的容量,求不被抓到的最大概率,最后for语句,钱数递减找到第一个符合的概率即可。
 2 //注意初始化背包时f[0]=1,其他的是0;被抓的概率是1减去不被抓的概率。
 3 #include<iostream>
 4 #include<cstdio>
 5 using namespace std;
 6 int t,n;
 7 double p,pj[105];
 8 int mj[105];
 9 double f[10004];
10 int main()
11 {
12     scanf("%d",&t);
13     while(t--)
14     {
15         int sum=0;
16         for(int i=1;i<=10000;i++)
17         f[i]=0;
18         f[0]=1;
19         scanf("%lf%d",&p,&n);
20         for(int i=1;i<=n;i++)
21         {
22             scanf("%d%lf",&mj[i],&pj[i]);
23             sum+=mj[i];
24             pj[i]=1-pj[i];
25         }
26         for(int i=1;i<=n;i++)
27         {
28             for(int k=sum;k>=mj[i];k--)
29             {
30                 f[k]=max(f[k],f[k-mj[i]]*pj[i]);
31             }
32         }
33         for(int i=sum;i>=0;i--)
34         {
35             if(1-f[i]<=p)
36             {
37                 printf("%d\n",i);
38                 break;
39             }
40         }
41     }
42     return 0;
43 }
时间: 2024-10-29 05:07:00

HDU2955 背包DP的相关文章

[BZOJ 1025] 游戏 置换群 背包DP

题意 对于一个 $n$ 阶置换群 $A$ , 它的循环节大小分别为 $a_1, a_2, ..., a_m$ , 则有 $\sum_{i = 1} ^ m a_i = n$ . 定义 $f(A)$ 为它的所有循环节的最小公倍数, 即 $f(A) = [a_1, a_2, ..., a_m]$ . 求在所有 $n$ 阶置换群中, $f(A)$ 有多少种取值. $n \le 1000$ . 分析 判断 $K$ 可不可取. $K = \prod_{i = 1} ^ r {s_r} ^ {t_r}$ 可

hdu 5234 Happy birthday 背包 dp

Happy birthday Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5234 Description 今天是Gorwin的生日.所以她的妈妈要实现她的一个愿望.Gorwin说她想吃很多蛋糕.所以他妈妈带她来到了蛋糕园. 这个园子被分成了n*m个方格子.在每一个格子里面,有一个蛋糕.第i行,第j列的格子中有一个重量为wij千克的蛋糕,Gorwin从左上角(1,1

hdu 1171 Big Event in HDU(背包DP)

题意: 杭电搬迁,有N种设备,每种设备有个价值V,数量M,要求将这些设备平分,使得平分后两边的总价值尽可能地相等. 输出两边各自的总价值. 思路: 背包DP后,P=所有的总价值/2,然后从P开始往两边找到第一个满足的价值. 可以降维,但是要注意for循环的顺序. 看代码. 代码: int v[55], m[55]; bool dp[250005]; int main(){ int n; while(scanf("%d",&n)!=EOF && n>=0){

POJ 1384 Piggy-Bank 背包DP

所谓的完全背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 其实和一般01背包没多少区别,不过数量可以无穷大,那么就可以利用一个物品累加到总容量结尾就可以了. 本题要求装满的,故此增加个限制就可以了. #include <stdio.h> #include <stdlib.h> #include <string.h> inline int min(int a, int b) { return a < b? a : b; } c

BZOJ 1042 硬币购物(完全背包+DP)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1042 题意:给出四种面值的硬币c1,c2,c3,c4.n个询问.每次询问用d1.d2.d3.d4个相应的硬币能够拼出多少种总和为s? 思路:(1)首先,用完全背包求出f[i]表示四种硬币的数量无限制拼出i的方案数. (2)接着我们来理解 x=f[s]-f[s-(d1+1)*c1]的含义:x表示c1硬币的数量不超过d1个而其他三种硬币的数量不限制拼成s的方案数.我们举着例子来说明, 假设

HDU 5616 Jam&#39;s balance 背包DP

Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell whether things on different side are the same weight.Weights can be put on left side or right side arbitrarily.Please tell whether the balance can me

hdu1561:树形背包dp

给定n个地点,每个地点藏有cost[i]的宝物,取得某些宝物有时需要先取其他宝物,现在让我们选m个地点问最多可以选多少宝物? 还是挺裸的树形背包dp吧,不难,关键还是中间dp的部分.可以做模板了->_-> 注意点:多组数据的话如果第一组对了然后其他都错了,那么很有可能是初始化的时候漏了.这次找可很久才知道差了e[0].clear().平时的习惯都是从1开始. --------------------------------------------------------------------

背包DP HDOJ 5410 CRB and His Birthday

题目传送门 题意:有n个商店,有m金钱,一个商店买x件商品需要x*w[i]的金钱,得到a[i] * x + b[i]件商品(x > 0),问最多能买到多少件商品 01背包+完全背包:首先x == 1时,得到a[i] + b[i],若再买得到的是a[i],那么x == 1的情况用01背包思想,x > 1时就是在01的基础上的完全背包.背包dp没刷过专题,这么简单的题也做不出来:( /************************************************* Author

codeforces 148E Aragorn&#39;s Story 背包DP

Aragorn's Story Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/148/E Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who