M - 01背包

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

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
 
 
题解: 
        一个人想要抢劫银行,每家银行多有一定的金额和被抓到的概率,知道Roy被抓的最大概率P,求他在不被被抓的情况下,抢劫的钱最多。

        被抓概率为pj,安全概率1-pj,他的安全概率大于1-P时都是安全的。抢劫的金额为0时,肯定是安全的,所以dp[0]=1;其他金额初始为最危险的所以概率全为0;
思路:
        和背包问题做一个简单的转化:把每个银行的储钱量之和total当成背包容量C,然后不被抓的概率1-pj当成价值w来求。那么他的最大的不被抓的概率下抢劫的钱最多的情况就是乘积了,最后再枚举一下就行了,为什么要用乘积呢这里,想一想数学中涉及到概率的问题,而不是简单概率的相加吧,。这里计算的是抢劫到j钱不被抓的概率,相乘才能保存下来。

dp[ j] 表示在抢劫到钱j时不被抓概率。最后,只要找出满足要求的最大j。

代码及案例显示

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int T,n,total;
double p;
double a[],dp[];
int b[];
int main()
{
cin>>T;
while(T--)
{
total=;
cin>>p>>n;
for(int i=; i<n; i++)
{
cin>>b[i]>>a[i];
total+=b[i];
}
memset(dp,,sizeof(dp));
dp[]=; //没抢到钱时,逃脱的概率为1
for(int i=; i<n; i++)
for(int j=total; j>=a[i]; j--)
{
dp[j]=max(dp[j],dp[j-b[i]]*(-a[i]));
//cout<<d[j]<<" "<<j<<endl;
}
for(int i=total; i>=; i--)
{
if(dp[i]>-p)
{
cout<<i<<endl;
break;
}
}
}
return ;
}

下面是第一组案例的各种抢劫钱数的概率

HDU 2955(01背包问题)的更多相关文章

  1. hdu 2955 01背包

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 如果认为:1-P是背包的容量,n是物品的个数,sum是所有物品的总价值,条件就是装入背包的物品的体积和不能 ...

  2. HDU 1171 (01背包问题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 分析: 例如数据 3 10    2 20    1 30    1 获得这样一个降序的数组: ...

  3. HDU 2955 01背包(思维)

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

  4. Robberies hdu 2955 01背包

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

  5. HDU 2546 01背包问题

    这里5元是个什么意思呢.差不多就是特殊情况了. 就是说最贵的那个东西先不买.并且最后要留下5元去买那个最贵的. 也就是说对现在金钱-5 拿剩下的钱去对减去最贵的商品后的商品dp.看这些剩下的钱能买多少 ...

  6. HDU _2546 01背包问题

    A - 饭卡 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  7. HDU 1864最大报销额 01背包问题

    B - 最大报销额 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  8. HDU 2602 Bone Collector(经典01背包问题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/O ...

  9. HDU 3466 Proud Merchants 带有限制的01背包问题

    HDU 3466 Proud Merchants 带有限制的01背包问题 题意 最近,伊萨去了一个古老的国家.在这么长的时间里,它是世界上最富有.最强大的王国.因此,即使他们的国家不再那么富有,这个国 ...

  10. HDU 2602 Bone Collector (01背包问题)

    原题代号:HDU 2602 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 原题描述: Problem Description Many yea ...

随机推荐

  1. 移动UI自动化-Page Objects Pattern

    移动UI自动化,看起来美好,践行起来却难.做个目光短见的务实主义者.Page Objects Pattern是Selenium官方推崇的方式,最近研究写测试用例最佳实践之Page Objects,同时 ...

  2. Druid数据库连接池使用

    转: http://lj6684.iteye.com/blog/1770093 阿里巴巴推出的国产数据库连接池,据网上测试对比,比目前的DBCP或C3P0数据库连接池性能更好   简单使用介绍 Dru ...

  3. linux 搭建vpn (pptp)

    一.VPN服务器环境说明 操作系统:CentOS release 6.4 (Final) 本地网卡: 复制代码 代码如下: # ifconfig em1 Link encap:Ethernet HWa ...

  4. 从零单排c++ primer(17)

    (1)假设一个名字在派生类的作用域内无法正确解析,则编译器将继续在外层的基类作用域中寻找该名字的定义. (2)派生类的成员将隐藏同名的基类成员. (3)除了覆盖继承而来的虚函数之外,派生类最好不要重用 ...

  5. 开源实时视频码流分析软件:VideoEye

    本文介绍一个自己做的码流分析软件:VideoEye.为什么要起这个名字呢?感觉这个软件的主要功能就是对"视频"进行"分析".而分析是要用眼睛来看的,因此取了&q ...

  6. 开启.htaccess重写之前先来看看mod_rewrite(转)

    Apache的Mode Rewrite模块提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求.在大多数情况下,它和.htaccess文件配合使用.就是说,.htaccess文件的一个主要功能 ...

  7. Java基础知识强化之IO流笔记11:递归之递归概述和注意事项

    1. 递归: 方法定义中调用方法本身的现象. e.g: public void show(int n ) { if(n <= 0) { System.exit(0); } System.out. ...

  8. spring 读取properties的两种方法

    一:直接使用context命名空间 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi= ...

  9. codevs 1746 贪吃的九头龙

    /* 状态定义的没错 就是考试的时候傻啦吧唧的转移左右孩子 其实之转移父亲就简单多了 不用考虑那么多 还有就是偷懒没有把谁有没有找过这个信息转过去 而是搞了个全局变量…wa到挺 再就是特盘的时候还有终 ...

  10. Codeforces-Div312

    题意:给你n个数,进行*2,/2操作,求解最小操作次数能使所有数相同. 思路:因为数值在100000以内,直接枚举过去,对读入的每一个数,模拟操作,用数组s来存放累计操作步数,数组flag用来标记确 ...