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. 在Salesforce中通过编写C#程序调用dataloadercliq的bat文件取触发调用data loader来批量处理数据

    通过这篇文章 http://www.cnblogs.com/mingmingruyuedlut/p/3413903.html 我们已经知道了Data Loader可以对Salesforce的Objec ...

  2. Java学习随笔4:Java的IO操作

    1. IO流的作用是读写设备上的数据,如硬盘文件.内存.键盘.网络等.根据数据走向,可分为:输入流和输出流:根据处理的数据类型,可分为:字节流和字符流.字节流可以处理所有类型的数据,如MP3.图片.视 ...

  3. html css js 一些记录.

    webstorm 的基本使用 webstorm 格式化 html 代码: Ctrl+Alt+L js html css 基本使用 注意 dom 的 innerHTML会刷新dom,所以里面包含的事件绑 ...

  4. 【java基础】重载与重写

    前言 : 很早的时候,我就知道这两个东西,但是,也仅仅是停留在知道的程度而已,对于什么是重写,什么事重载,还是感到十分的迷惑,迷茫.正好,在软考复习时又经历这两个东西,细心一点,探究了一下,有点收获, ...

  5. AngularJS学习之Select(选择框)

    1.AngularJS可以使用数组或对象创建一个下拉列表选项: 2.在AngularJS中我们可以使用ng-option指令创建一个下拉列表:列表项通过对象和数组循环输出: <div ng-ap ...

  6. 【分块】【树上莫队】bzoj1086 bzoj3052

    1086 http://vfleaking.blog.163.com/blog/static/174807634201231684436977/ 3052 http://vfleaking.blog. ...

  7. word多级编号,如何让第一级为大写“一”,其他级别均为小写1.

    自定义里面设置了第一级为大写,2级.3级首字跟着变为大写,是因为2.3级没有勾选"正规形式编号",如图:

  8. 向 ViewPager 中添加 包含 ListView 的 Fragment

    对与fragment就不说什么了,直接看API手册吧,亲. 向 ViewPager 中添加 包含 ListView 的 Fragment 的过程比较麻烦.他所表现的效果就是新闻客户端的滑动翻页效果. ...

  9. ROW_NUMBER() OVER函数的基本用法

    转自:http://www.cnblogs.com/icebutterfly/archive/2009/08/05/1539657.html 语法:ROW_NUMBER() OVER(PARTITIO ...

  10. ACM The Famous Clock

    The Famous Clock 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 Mr. B, Mr. G and Mr. M are now in Warsaw, ...