HDU 2955 Robberies(01背包变形)
Robberies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16522 Accepted Submission(s): 6065
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.
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
4
6
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345
#define M 12 float f[N],p[N],P;
int m[N],n; int main()
{
// freopen("1.txt", "r", stdin);
// freopen("2.txt", "w", stdout);
int T;cin>>T;
while(T--)
{
int sum=;
scanf("%f%d",&P,&n);
for(int i=;i<=n;i++)
{
scanf("%d%f",&m[i],&p[i]);
sum+=m[i];
}
memset(f,,sizeof(f));//初始化
f[]=1.0;//边界,抢劫0钱不被抓的概率是1 for(int i=;i<=n;i++)
{
for(int j=sum;j>=m[i];j--)
{
f[j]=max(f[j], f[j-m[i]]*(-p[i]) );//独立事件相乘
}
} for(int i=sum;i>=;i--)
{
// printf("%f ",f[i]);
if(f[i]>=(-P))//找到第一不被抓概率大于等于(1-P)的i就是答案
{
printf("%d\n",i);
break;
}
}
}
return ;
}
2)
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345 float f[N],p[N],P;
int m[N],n; int main()
{
int T;cin>>T;
while(T--)
{
int sum=;
scanf("%f%d",&P,&n);
for(int i=;i<=n;i++)
{
scanf("%d%f",&m[i],&p[i]);
sum+=m[i];
} for(int i=;i<N-;i++)//初始化
f[i]=1.0;
f[]=0.0;//边界,抢劫0钱的被抓概率是0 for(int i=;i<=n;i++)
{
for(int j=sum;j>=m[i];j--)
{
f[j]=min(f[j], -(-f[j-m[i]])*(-p[i]) );//概率知识,独立事件,对立事件
}
} for(int i=sum;i>=;i--)
{
// printf("%f ",f[i]);
if(f[i]<=P)//找到第一个概率小于等于安全概率P的i,就是答案
{
printf("%d\n",i);
break;
}
}
}
return ;
}
HDU 2955 Robberies(01背包变形)的更多相关文章
- HDU 2955 Robberies --01背包变形
这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即 ...
- hdu 2955 Robberies (01背包)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:一开始看急了,以为概率是直接相加的,wa了无数发,这道题目给的是被抓的概率,我们应该先求出总的 ...
- 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 (0-1背包)
题意:有N个银行,每抢一个银行,可以获得\(v_i\)的前,但是会有\(p_i\)的概率被抓.现在要把被抓概率控制在\(P\)之下,求最多能抢到多少钱. 分析:0-1背包的变形,把重量变成了概率,因为 ...
- 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 Problem Description The aspiring Roy the Robber has seen a lot of American movies, and kno ...
- HDOJ 2955 Robberies (01背包)
10397780 2014-03-26 00:13:51 Accepted 2955 46MS 480K 676 B C++ 泽泽 http://acm.hdu.edu.cn/showproblem. ...
- HDU 2955 【01背包/小数/概率DP】
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDOJ.2955 Robberies (01背包+概率问题)
Robberies 算法学习-–动态规划初探 题意分析 有一个小偷去抢劫银行,给出来银行的个数n,和一个概率p为能够逃跑的临界概率,接下来有n行分别是这个银行所有拥有的钱数mi和抢劫后被抓的概率pi, ...
随机推荐
- Spark 2.0.0 SPARK-SQL returns NPE Error
com.esotericsoftware.kryo.KryoException: java.lang.NullPointerExceptionSerialization trace:underlyin ...
- eval() 函数 解析json对象
eval在js中用来运行以js源码组成的字符串. 可以用来改变全局或者局部变量,例如: var globalEval = eval; //定义全局eval函数别名 var a ='global', b ...
- wei UI使用
1.前言 通过前面系列文章的学习与讲解,相信大家已经对微信的开发有了一个全新的认识.后端基本能够基于盛派的第三方sdk搞定大部分事宜,剩下的就是前端了.关于手机端的浏览器的兼容性问题相信一直是开发者们 ...
- Linux快捷键列表
Linux快捷键列表 快捷键 功能描述 快捷键 功能描述 control+p 查询命令历史纪录的上一条命令 方向上键 查询命令历史纪录的上一条命令 control+n 查询命令历史纪录的下一条命令 方 ...
- eclipse逆向生成实体类
(转自:http://blog.csdn.net/wangpeng047/article/details/6877720) 做项目必然要先进行数据库表设计,然后根据数据库设计建立实体类(VO),这是理 ...
- OpenJ_Bailian——4115鸣人和佐助(带状态的A*)
鸣人和佐助 Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit Status Desc ...
- BZOJ 4503 两个串 ——FFT
[题目分析] 定义两个字符之间的距离为 (ai-bi)^2*ai*bi 如果能够匹配,从i到i+m的位置的和一定为0 但这和暴力没有什么区别. 发现把b字符串反过来就可以卷积用FFT了. 听说KMP+ ...
- [USACO08DEC]Trick or Treat on the Farm (拓扑排序,DP)
题目描述 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N个牛棚里转 悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大,所以约翰要想尽法子让奶牛们得到快乐. ...
- [MFC] TabControl选项卡的使用
MFC中,因项目需要使用TabControl ,使用过程中发现,MFC中的TabControl与C#的TabControl不同,不能通过属性来创建选项页,只能代码生成绑定. 以下为具体的实现方法步骤: ...
- android本地存储SharedPreferences
SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...