hdu2955 Robberies (01背包)
转载请注明出处:http://blog.csdn.net/u012860063
题目链接: pid=2955">http://acm.hdu.edu.cn/showproblem.php?pid=2955
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.
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 .
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.
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
2
4
6
pid=1203">1203
2159 2844 1087 1505题意:
抢银行。抢每一个银行被抓的几率为caught[],互为独立事件,在容忍上限内,抢最多的钱。
01背包,但须要改变一点。须要将能抢来的最多的钱最为背包容量
代码例如以下:
/*此题关键是找到背包的容量和价值*/
#include <cstdio>
#include <cstring>
#define N 10047
int M[N];
double P[N],f[N];
double max(double a,double b)
{
if(a > b)
return a;
return b;
}
int main()
{
double p;
int T,n,i,j,sum;
scanf("%d",&T);
while(T--)
{
sum = 0;
memset(f,0,sizeof(f));//要对数组进行初始化
scanf("%lf%d",&p,&n);
p =1-p;;//成功逃走的概率
f[0] = 1;//未抢劫一分钱那么逃走的概率就为1
for(i = 0 ; i < n ; i++)
{
scanf("%d%lf",&M[i],&P[i]);
P[i] = 1-P[i];
sum+=M[i];//算出银行总钱数
}
for(i = 0 ; i < n ; i++)
{
for(j = sum ; j >= M[i] ; j--)
{
f[j] = max(f[j],f[j-M[i]]*P[i]);
}//这里的每次成功的概率须要进行乘法运算。由于是两次成功的概率所以是乘法
}
for(i = sum ; i >= 0 ; i--)
{
if(f[i] >= p)
break;//找出能够成功逃走而且逃走概率不超过警戒值
}
printf("%d\n",i);
}
return 0;
}
hdu2955 Robberies (01背包)的更多相关文章
- HDU2955 Robberies[01背包]
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Robberies(HDU2955):01背包+概率转换问题(思维转换)
Robberies HDU2955 因为题目涉及求浮点数的计算:则不能从正面使用01背包求解... 为了能够使用01背包!从唯一的整数(抢到的钱下手)... 之后就是概率的问题: 题目只是给出被抓的 ...
- 【hdu2955】 Robberies 01背包
标签:01背包 hdu2955 http://acm.hdu.edu.cn/showproblem.php?pid=2955 题意:盗贼抢银行,给出n个银行,每个银行有一定的资金和抢劫后被抓的概率,在 ...
- hdu 2955 Robberies 0-1背包/概率初始化
/*Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 2955 Robberies(01背包变形)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 2955 Robberies (01背包好题)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 2955 Robberies (01背包)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:一开始看急了,以为概率是直接相加的,wa了无数发,这道题目给的是被抓的概率,我们应该先求出总的 ...
- HDU——2955 Robberies (0-1背包)
题意:有N个银行,每抢一个银行,可以获得\(v_i\)的前,但是会有\(p_i\)的概率被抓.现在要把被抓概率控制在\(P\)之下,求最多能抢到多少钱. 分析:0-1背包的变形,把重量变成了概率,因为 ...
- HDU 2955 Robberies --01背包变形
这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即 ...
- HDU 2955 Robberies(01背包)
Robberies Problem Description The aspiring Roy the Robber has seen a lot of American movies, and kno ...
随机推荐
- Summary—【base】(HTML)
Html知识点: 1. 建议开发人员计算机基本配置 a) 显示所有文件的后缀名* b) 文件的排列方式改为详细信息,并且名称一定要能够全部显示出来 c) 使用小的任务栏 d) 将常用的工具锁定到任务栏 ...
- iOS-文本段落样式NSMutableParagraphStyle与NSParagraphStyle的使用和一些富文本处理属性
开发过程中,经常会遇到动态计算行高的问题, - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)op ...
- 【翻译】Apache软件基金会1
最近有点看不进去书,所以就找点东西翻译下,正好很想了解Apache基金会都有什么开源项目,每天找点事时间翻译翻译,还可以扩展下视野. 今天就看了两个,第一个是关于.NET的,不再兴趣范围内.第二个还挺 ...
- C#添加编译时间
using System.Reflection;using UnityEngine;using System.Collections; [assembly:AssemblyVersion( " ...
- spring+xml集成测试(准备数据和验证项的外部文件化)
Spring的集成测试 单位测试和集成测试,我想大家都做过,一般情况下,一般逻辑且不需要操作数据库的情况比较适合于单位测试了.而对于一个数据库应用来说,集成测试可能比单元测试更重要,你可以想象,一个互 ...
- react 基础语法复习3- 数据传递 & 数据变化(props&&state)
react当中的数据传递是通过 props以及 state来实现的 props <Header name="kugeliu" /> Header组件当中有个name属性 ...
- iOS - 倒计时封装
+(NSString *)countdownStartTime:(NSString *)startTime{ NSString *TIME = [startTime substringToIndex: ...
- 利用Docker搭建本地https环境的完整步骤
利用Docker搭建本地https环境的完整步骤 这篇文章主要给大家介绍了关于如何利用Docker搭建本地https环境的完整步骤,文中通过示例代码将实现的步骤介绍的非常详细,对大家的学习或者工作具有 ...
- 记忆泛型约束where
原文发布时间为:2011-03-29 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/d5x73970.aspx
- [LeetCode] Climbing Stairs 斐波那契数列
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...