hdu 2955 Robberies (01背包好题)
Robberies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31769 Accepted Submission(s): 11527
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.
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.
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
题目大意:
Roy是一个盗贼。现在有n个银行成了他的下手目标。对第i个银行下手,他会获得mi元钱,同时有pi的概率被抓。问在被抓概率不超过p的情况下,他最多能得到多少钱。
01背包好题。
这题仔细想想还是很有趣的。获得的钱其实是背包容量。获得这些钱安全的概率才是dp存储的东西。要好好想想。
此外,对double类型变量输入输出是 printf&%f scanf&%lf,注意。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack> using namespace std; const int maxn=; int money[maxn+];
double safe[maxn+];//不被抓的概率作为重量
double dp[maxn*maxn+];//获得i钱,不被抓的概率 int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double p;int n;
scanf("%lf%d",&p,&n);
p=-p;
int sum=;//最多获得多少钱
for(int i=;i<=n;i++)
{
scanf("%d%lf",money+i,safe+i);
safe[i]=-safe[i];
sum+=money[i];
} for(int i=;i<=sum;i++)
dp[i]=;
dp[]=;
for(int i=;i<=n;i++)
{
for(int j=sum;j>=money[i];j--)
{
dp[j]=max(dp[j],dp[j-money[i]]*safe[i]);
}
} for(int i=sum;i>=;i--)
if(dp[i]>p)
{
printf("%d\n",i);
break;
}
}
return ;
}
hdu 2955 Robberies (01背包好题)的更多相关文章
- hdu 2955 Robberies (01背包)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:一开始看急了,以为概率是直接相加的,wa了无数发,这道题目给的是被抓的概率,我们应该先求出总的 ...
- HDU 2955 Robberies(01背包变形)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 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背包变形
这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即 ...
- HDU 2955 Robberies(01背包)
Robberies Problem Description The aspiring Roy the Robber has seen a lot of American movies, and kno ...
- Jam's balance HDU - 5616 (01背包基础题)
Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side ...
- 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背包)
10397780 2014-03-26 00:13:51 Accepted 2955 46MS 480K 676 B C++ 泽泽 http://acm.hdu.edu.cn/showproblem. ...
随机推荐
- JavaWeb03-请求和响应
请求响应流程图 response 1 response概述 response是Servlet.service方法的一个参数,类型为javax.servlet.http.HttpServl ...
- mac如何开启两个vmware虚拟机
转载链接:https://blog.csdn.net/aifore/article/details/87833088
- Linux root设置初始值的方法
Linux root设置初始值的方法 ubuntu默认不允许使用root登录,因此初始root账户是不能使用的,需要在普通账户下利用sudo权限修改root密码. 在终端输入sudo passwd r ...
- Hadoop简述
Haddop是什么? Hadoop是一个由Apache基金会所开发的分布式系统基础架构 主要解决,海量数据的存储和海量数据的分析计算问题. Hadoop三大发行版本 Apache版本最原始(最基础)的 ...
- Java基础面试题及答案(五)
Java Web 64. jsp 和 servlet 有什么区别? jsp经编译后就变成了Servlet.(JSP的本质就是Servlet,JVM只能识别java的类,不能识别JSP的代码,Web容器 ...
- usermod命令、用户密码管理、mkpasswd命令 使用介绍
第3周第2次课(4月3日) 课程内容:3.4 usermod命令3.5 用户密码管理3.6 mkpasswd命令 3.4 usermod命令 usermod可以修改用户的UID和GID 命令使用格式: ...
- Block循环引用问题
根控制器没办法销毁,除非程序退出 从一个控制器跳到另外一个控制器,调用该控制器的pop方法才会销毁该控制器 self是一个强指针 在block中使用self时要注意循环引用的问题 最好将当前block ...
- 记录我的 python 学习历程-Day03 数据类型 str切片 for循环
一.啥是数据类型 我们人类可以很容易的分清数字与字符的区别,但是计算机并不能呀,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,"汉"是文字,否则它是分 ...
- ActiveMQ配置策略
1.消息发送 1.异步发送 消息生产者使用持久(persistent)传递模式发送消息的时候,Producer.send() 方法会被阻塞,直到 broker 发送一个确认消息给生产者,这个确认消息暗 ...
- CentOS下永久修改主机名
永久修改主机名 [root@centos7 ~]# vim /etc/hostname 打开之后将原来的名字改成你想换的名字 [root@centos7 ~]# cat /etc/hostname 查 ...