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
 题意:
一人去抢银行,n个银行,在每个银行能够得到的钱mj,被抓住的概率pj,抢所有银行被抓住的概率p,问在总概率不超过p时得到的最大钱数。
代码:
 //由于存在概率的乘法,用普通的01背包肯定不行,可以以总钱数作为背包的容量,求不被抓到的最大概率,最后for语句,钱数递减找到第一个符合的概率即可。
//注意初始化背包时f[0]=1,其他的是0;被抓的概率是1减去不被抓的概率。
#include<iostream>
#include<cstdio>
using namespace std;
int t,n;
double p,pj[];
int mj[];
double f[];
int main()
{
scanf("%d",&t);
while(t--)
{
int sum=;
for(int i=;i<=;i++)
f[i]=;
f[]=;
scanf("%lf%d",&p,&n);
for(int i=;i<=n;i++)
{
scanf("%d%lf",&mj[i],&pj[i]);
sum+=mj[i];
pj[i]=-pj[i];
}
for(int i=;i<=n;i++)
{
for(int k=sum;k>=mj[i];k--)
{
f[k]=max(f[k],f[k-mj[i]]*pj[i]);
}
}
for(int i=sum;i>=;i--)
{
if(-f[i]<=p)
{
printf("%d\n",i);
break;
}
}
}
return ;
}

HDU2955 背包DP的更多相关文章

  1. 背包dp整理

    01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...

  2. hdu 5534 Partial Tree 背包DP

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  3. HDU 5501 The Highest Mark 背包dp

    The Highest Mark Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  4. Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp

    B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...

  5. noj [1479] How many (01背包||DP||DFS)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...

  6. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  7. BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )

    题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...

  8. G - Surf Gym - 100819S -逆向背包DP

    G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...

  9. 树形DP和状压DP和背包DP

    树形DP和状压DP和背包DP 树形\(DP\)和状压\(DP\)虽然在\(NOIp\)中考的不多,但是仍然是一个比较常用的算法,因此学好这两个\(DP\)也是很重要的.而背包\(DP\)虽然以前考的次 ...

随机推荐

  1. FZU Problem 2082 过路费 树链剖分

    Problem 2082 过路费    Problem Description 有n座城市,由n-1条路相连通,使得任意两座城市之间可达.每条路有过路费,要交过路费才能通过.每条路的过路费经常会更新, ...

  2. unable to access android sdk add-on list

    在bin\properties里添加disable.android.first.run=true

  3. 软引用SoftReference异步加载图片

    HashMap<String, SoftReference<Drawable>> imageCache 关于SoftReference这个类多少知道些机制,会用就ok了. 机制 ...

  4. python 文件操作总结

    Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...

  5. 利用myeclipse配置数据库连接池

    作为一个习惯使用myeclipse的人来说,即使是数据库连接池也肯定是用ide配置了. 下面说一下用数据库连接池的配置. 1 创建工程.不多说了. 2 添加数据库连接程序驱动包.直接放到lib目录下即 ...

  6. [工作中的设计模式]原型模式prototype

    一.模式解析 提起prototype,最近看多了js相关的内容,第一印象首先是js的原型 var Person=function(name){ this.name=name; } Person.pro ...

  7. node EventEmitter 注册事件

    var EventEmitter = require("events").EventEmitter var event = new EventEmitter() event.on( ...

  8. Linux profile File

    umask 022alias vim="/pd/vim/7.1/bin/vim"if [ -e /usr/bin/vim ]; then alias vim="/usr/ ...

  9. 线段树(区间合并) LA 3989 "Ray, Pass me the dishes!"

    题目传送门 题意:动态最大连续子序列和,静态的题目 分析:nlogn的归并思想.线段树维护结点的三个信息,最大前缀和,最大后缀和,该区间的最大和的两个端点,然后答案是三个的better.书上用pair ...

  10. jquery toastr introduction

    1.资源 http://www.jq22.com/jquery-info476 http://www.jq22.com/yanshi476 Nuget Install-Package toastr 官 ...