HDU2955-Robberies
描述:
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.
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 .
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.
代码:
首先得转换思路,因为概率值是浮点数,没有办法作为背包的容量。
这里选择抢劫的钱数作为背包容量,dp[i]代表抢劫的钱数为i时,不被抓住的最大的概率(这里比较绕-_-)。只要有一次被抓住,就算被抓住,所以选择计算不被抓住比较方便,只需将1-p[i]累乘即可。当不被抓住的概率最大时,被抓住的概率就最小,使得i值尽可能的大,得到最优解。
初始化时,只有dp[0]=1,其余均为0,因为dp代表确实抢到的数目,而不是最大值,也就是这里要求恰好装满。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define N 105
#define M 10000 int main(){
int T,n,money[N],sum;
double p,dp[M],pos[N];//pos为抢劫一个银行被抓的概率,dp为抢劫该数目的钱一次都不被抓的概率
scanf("%d",&T);
while( T-- ){
scanf("%lf%d",&p,&n);
sum=;
for( int i=;i<=n;i++ ){
scanf("%d%lf",&money[i],&pos[i]);
sum+=money[i];
}
memset(dp,0.0,sizeof(dp));
dp[]=;//只有没有抢到钱,此时有解的概率为1。其余均为0,代表无解
for( int i=;i<=n;i++ ){
for( int j=sum;j>=money[i];j-- ){
dp[j]=max(dp[j],dp[j-money[i]]*(-pos[i]));//j值代表抢钱的数目(并不是最大的数目,而是确实抢到的数目),j值一定,希望dp值越大越好
}
}
for( int i=sum;i>=;i-- ){
if( dp[i]>=-p ){//不被抓的概率大于预期
printf("%d\n",i);
break;
}
}
}
system("pause");
return ;
}
HDU2955-Robberies的更多相关文章
- HDU2955 Robberies[01背包]
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu2955 Robberies(背包)
https://vjudge.net/problem/HDU-2955 概率是浮点数,只能做值(而且这里是累乘,也不能化成整数),这里注意要化成安全概率(1-p[i]),求安全概率的最大值. 钱数作二 ...
- HDU-2955 Robberies 浮点数01背包 自变量和因变量位置互换
题目链接:https://cn.vjudge.net/problem/HDU-2955 题意 突然想找几个银行抢钱. 给出各银行的钱数和被抓的概率,以及能容忍的最大被抓概率. 问他最多能抢到多少钱? ...
- hdu2955 Robberies 01背包+概率
link:http://acm.hdu.edu.cn/showproblem.php?pid=2955 首先,这个题目的背包容量不能是概率.1.精度不清楚.2.把概率相加有什么意义呢?所以,转换一下, ...
- 01标题背包水章 HDU2955——Robberies
原来是dp[i],它代表的不被抓的概率i这最大的钱抢(可能1-100) 客是dp[i]表示抢了i钱最大的不被抓概率,嗯~,弱菜水题都刷不动. 那么状态转移方程就是 dp[i]=max(dp[i],dp ...
- hdu2955 Robberies (01背包)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=2955">http://acm.hdu.edu.cn/showproblem.php ...
- 【题解】 hdu2955 Robberies
有抱负的罗伊·劫匪已经看过很多美国电影,他知道坏人通常会被抓住,经常是因为他们太贪心了.他决定在银行抢劫案中工作一段时间,然后退休后到一所大学从事一份舒适的工作. 题目: 罗伊去几个银行偷盗,他既想多 ...
- Robberies(HDU2955):01背包+概率转换问题(思维转换)
Robberies HDU2955 因为题目涉及求浮点数的计算:则不能从正面使用01背包求解... 为了能够使用01背包!从唯一的整数(抢到的钱下手)... 之后就是概率的问题: 题目只是给出被抓的 ...
- Robberies(简单的01背包 HDU2955)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- 【hdu2955】 Robberies 01背包
标签:01背包 hdu2955 http://acm.hdu.edu.cn/showproblem.php?pid=2955 题意:盗贼抢银行,给出n个银行,每个银行有一定的资金和抢劫后被抓的概率,在 ...
随机推荐
- JFinal开发web项目出现故障小记
导读 作为中国优秀的开源项目之中的一个JFinal有着极速开发的优点,是中小型应用开发的首选.在导师的建议下.我使用了JFinal来开发一个Java服务端应用,官方教程非常easy.就几十页(当然是中 ...
- Java压缩技术的学习
由于工作的需要,经常要手动去打上线安装包,为了方便,自己写程序去帮助打包.使用过Unix或者Linux的人都基本上都用过tar打包以及gzip压缩,但在Windows下使用得最多的压缩还是RAR和Zi ...
- T-SQL流程控制
常用的T-SQL流程控制有三种,case ... when ... then...(else)...end (as) ... 判断句式,if判断句式和while循环句式. case...when .. ...
- Properties读写资源文件
Java中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XML文件 3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要 ...
- PHP的接口类(interface)和抽象类(abstract)的区别
<?php /** * 接口类:interface * 其实他们的作用很简单,当有很多人一起开发一个项目时,可能都会去调用别人写的一些类, * 那你就会问,我怎么知道他的某个功能的实现方法是怎么 ...
- sheelエラー、オブジェクトを解析中にエラーが発生しました。
- Hibernate之dynamic-update
问题:设置了dynamic-update, 可是事实上并没有按照期望进行了update. 案例代码如下: 1.持久化对象 package com.jdw.hibernate.entities; imp ...
- redmine和svn server的部署
作为一个程序猿,想要很好的管理自己项目和代码,我们需要一些工具做辅助. 项目管理工具redmine和代码版本管理工具 SVN(Subversion). 我们选择在虚拟机里面安装windows部署这两套 ...
- java 执行linux命令
原文地址: http://blog.csdn.net/xh16319/article/details/17302947 package scut.cs.cwh; import java.io.Inpu ...
- VS Code调试.NET Core
VS Code调试.NET Core应用遇到的坑 为什么会有”坑“ 博客园里有好多介绍怎么使用VS Code以及调试.NET Core的文章,但是都是基于直接构建Asp.Net Core Mvc单项目 ...