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
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.
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 .
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.
//由于存在概率的乘法,用普通的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的更多相关文章
- 背包dp整理
01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...
- hdu 5534 Partial Tree 背包DP
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- 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 ...
- 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/ ...
- noj [1479] How many (01背包||DP||DFS)
http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...
- HDU 1011 树形背包(DP) Starship Troopers
题目链接: HDU 1011 树形背包(DP) Starship Troopers 题意: 地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...
- BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )
题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...
- G - Surf Gym - 100819S -逆向背包DP
G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...
- 树形DP和状压DP和背包DP
树形DP和状压DP和背包DP 树形\(DP\)和状压\(DP\)虽然在\(NOIp\)中考的不多,但是仍然是一个比较常用的算法,因此学好这两个\(DP\)也是很重要的.而背包\(DP\)虽然以前考的次 ...
随机推荐
- 处理FF margin-top下降问题
处理DIV子级ZImargin-top下降,父级更着下降问题 html结构如下 <div id="top"> <div id="zi"> ...
- SSH框架应用解析
一.什么是SSH SSH 不仅仅只是一个框架,而是由多个框架集成而来,是 struts+spring+hibernate的一个集成框架,是目前较流行的一种Web应用程序开源框架,结构清晰.可复用性好. ...
- POJ 3349 HASH
题目链接:http://poj.org/problem?id=3349 题意:你可能听说话世界上没有两片相同的雪花,我们定义一个雪花有6个瓣,如果存在有2个雪花相同[雪花是环形的,所以相同可以是旋转过 ...
- 关于jquery中 跳出each循环的方法
最近在项目中用带了jquery,在使用each循环遍历时在满足一定条件就跳出,发现break不好使,最终原来 用 return false;便可解决.
- 在字符界面tty1~tty6中使用鼠标,并用其复制粘贴
1. 安装 无意间看到gpm这个服务可以让你在tty1~tty6 环境中使用鼠标. 先用 rpm -qa gpm 查看是否已经安装此服务,如果提示以安装,则可以直接开启: 否则就要通过 yum ins ...
- wordpress安装步骤
步骤1.因为安装Wordpress需要用到Apache和Mysql数据库,可以选择单独安装这两个软件,但配置参数设置起来可能会遇到一些困扰,建议大家下载现成的PHP和Mysql的集成安装包,比如XAM ...
- 【原】iOS学习之Swift之语法2(精简版)
1.可选类型和强制解包(?和!) 1> 可选类型(?)和强制解包(!) 在swift中,可选类型(?) 其根源是一个 枚举型,里面有 None 和 Some 两种类型.其实所谓的 nil 就是 ...
- BZOJ4231 : 回忆树
一个长度为$|S|$的串在树上匹配有两种情况: 1.在LCA处转弯,那么这种情况只有$O(|S|)$次,暴力提取出长度为$2|S|$的链进行KMP即可. 2.不转弯,那么可以拆成两个到根路径的询问. ...
- Hadoop执行作业时报错:java.lang.OutOfMemoryError: Java heap space
常常被一些用户问到,说“为什么我的mapreduce作业总是运行到某个阶段就报出如下错误,然后失败呢?以前同一个作业没出现过的呀?” 10/01/10 12:48:01 INFO mapred.Job ...
- FastDFS 自动部署和配置脚本
写了一个自动安装和配置FastDFS的脚本,还没有写好关于nginx的配置.先贴上,如下: 自动安装FastDFS,(这部分是之前同事写好的) #!/bin/bash #instll gcc echo ...