题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955

Robberies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 29618    Accepted Submission(s): 10834

Problem Description
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.

 
Input
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 .
 
Output
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.

 
Sample Input
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
 
Sample Output
2 4 6
 
分析:
英文题目是真的很难理解啊。。。。。
 
先是给出几组数据,每组数据第一行是总被抓概率p(最后求得的总概率必须小于他,否则被抓),然后是想抢的银行数n。然后n行,每行分别是该银行能抢的钱数m[i]和被抓的概率p[i],在成功逃跑的前提下获得的最大钱数
 
 
开始是想将被抓的概率当作背包总容量,每个银行的钱当作物品价值,被抓概率当作物品重量,这样思路确实是对的,但是这题的数据很毒,被每个银行被抓的概率精度是不确定的,不一定是小数点后两位,可能是0.00001,所以概率乘以100是没有用的,乘太大也就会超出数组的下标界限
所以转变思维:
 
将银行钱总数当作背包的容量,每个银行逃跑的概率做价值,每个银行的钱数当作重量
这样问题就转变成了怎么拿可以使得逃跑概率最大
dp[k]的意义:拿k钱的时候的成功逃跑概率
从后往前遍历dp数组,找到第一个dp[k]大于题目给出的逃跑概率限制条件,这个时候的k值就是可以获得的最大钱数
注意:
1.题目给的限制概率是被抓的概率(p),逃跑的概率=1-被抓的概率
小偷偷完之后被抓的概率要小于p,小偷偷完之后逃跑的概率要大于(1-p)
2.每个银行题目给的概率是偷每个银行被抓的概率,偷每个银行成功逃跑的概率=(1-偷每个银行被抓的概率)
3.成功逃跑的概率等于偷每个银行成功逃跑概率的乘积
 
emmm,应该注意点就这些
放代码:
#include<bits/stdc++.h>
using namespace std;
#define max_v 10005
double dp[max_v];//拿k钱的时候的成功逃跑概率
int w[max_v];//每个银行的钱数当作重量
double v[max_v];//每个银行逃跑的概率做价值
//所有银行的总钱数做背包容量
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double p;
int n;
int sum=;
scanf("%lf %d",&p,&n);
for(int i=;i<n;i++)
{
scanf("%d %lf",&w[i],&v[i]);
sum=sum+w[i];
}
memset(dp,,sizeof(dp));
dp[]=;
for(int i=;i<n;i++)
{
for(int j=sum;j>=w[i];j--)
{
dp[j]=max(dp[j],dp[j-w[i]]*(-v[i]));
}
}
for(int i=sum;i>=;i--)
{
if(dp[i]>(-p))
{
printf("%d\n",i);
break;
}
}
}
return ;
}

HDU 2955 变形较大的01背包(有意思,新思路)的更多相关文章

  1. HDU 2955 Robberies(0-1背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 题意:一个抢劫犯要去抢劫银行,给出了几家银行的资金和被抓概率,要求在被抓概率不大于给出的被抓概率的情况下, ...

  2. hdu 2955(概率转化,01背包)

    Hot~~招聘——巴卡斯(杭州),壹晨仟阳(杭州),英雄互娱(杭州) (包括2016级新生)除了校赛,还有什么途径可以申请加入ACM校队? Robberies Time Limit: 2000/100 ...

  3. HDU 2955 Robberies (01背包,思路要转换一下,推荐!)

    题意: 小A要去抢劫银行,但是抢银行是有风险的,因此给出一个float值P,当被抓的概率<=p,他妈妈才让他去冒险. 给出一个n,接下来n行,分别给出一个Mj和Pj,表示第j个银行所拥有的钱,以 ...

  4. HDU 2955 Robberies【01背包】

    解题思路:给出一个临界概率,在不超过这个概率的条件下,小偷最多能够偷到多少钱.因为对于每一个银行都只有偷与不偷两种选择,所以是01背包问题. 这里有一个小的转化,即为f[v]代表包内的钱数为v的时候, ...

  5. hdu 2955 Robberies(01背包)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. HDU 2602 Bone Collector(01背包裸题)

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. HDU 2546 饭卡(01背包裸题)

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  8. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  9. HDU 5234 Happy birthday 01背包

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5234 bc:http://bestcoder.hdu.edu.cn/contests/con ...

随机推荐

  1. php写入文件fwrite() 函数用法

    在php中,php fwrite() 函数是用于写入文件(可安全用于二进制文件).说的简单点,就是在一个文件中,添加新的内容,本篇文章收集总结了几篇关于php写入文件fwrite() 函数用法的总结, ...

  2. bootstrap-table 的必备参数

    1: 链接接口 url  请求的数据类型:dataType : "json"   后台请求方法 method  locale: 'zh-CN',//中文支持 pagination: ...

  3. 网络I/O模型--03非阻塞模式(ServerSocket与Socket的超时处理)--解除accept()、 read()方法阻塞

    对于阻塞方式的一种改进是在应用程序层面上将 “一直等待 ”的状态主动打开: 这种模式下,应用程序的线程不再一直等待操作系统的 I/O状态,而是在等待一段时间后就解除阻塞.如果没有得到想要的结果,则再次 ...

  4. 葡萄城报表介绍:Web 报表

    Web(World Wide Web)即全球广域网,也称为万维网,它是一种基于超文本和 HTTP 的.全球性的.动态交互的.跨平台的分布式图形信息系统.Web 报表是以 Web 作为信息展示平台,可以 ...

  5. overload与override的区别

    override(重写,覆盖) 1.方法名.参数.返回值相同. 2.子类方法不能缩小父类方法的访问权限. 3.子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常). 4.存在于父类和子类之 ...

  6. iOS如何解析crash文件中的地址

    1.目录中存放app文件 2.打开文件 3.执行命令otool -arch arm64 -l ./QQStock  | grep -B 1 -A 10 "LC_SEGM" | gr ...

  7. Android 进程回收

    1.Android 进程回收策略 众所周知,Android是基于Linux系统的.在Android进程回收策略中,Android进程与Linux进程根据OOM_ADJ阈值进行区分: OOM_ADJ & ...

  8. shiro web 集成

    集成方法 shiro与web集成,主要是通过配置一个ShiroFilter拦截所有URL,其中ShiroFilter类似于SpringMVC的前端控制器,是所有请求入口点,负责根据配置(如ini配置文 ...

  9. Linux学习之CentOS(一)----在VMware虚拟机中安装CentOS 7

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  10. XML与DataSet的相互转换的类

    一.XML与DataSet的相互转换的类 using System; using System.Collections.Generic; using System.Text; using System ...