Robberies

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
 
Wrong Answer
一开始按照一般的做法打出来发现编译报错,原来是忘了下标是double的了,那就把概率乘个100,变成int,然并卵。。精度并不是.2。。再废话一句,cin超时了。。。
 
Answer
参考其他人的,把银行的钱作为体积,概率作为价值,所有银行的钱作为背包容量,因为已经算了逃跑率,方程就是这样:
dp[j]=max(dp[j],dp[j-v[i].vo]*v[i].va);//v是vector的意思,不是体积。
输出的时候从dp[sum]//sum是总钱数)开始循环,遇到的第一个能逃跑的//逃跑率不大于1-p(给出的被抓率)),输出那个下标。
 
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
#define msd memset(dp,0,sizeof(dp))
using namespace std;
#define LOCAL
double dp[];
struct Node
{
int vo;//钱
double va;//[逃跑]概率
}v[];
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif // LOCAL
//ios::sync_with_stdio(false);
int N;
cin>>N;
while(N--)
{
double p;
int n,sum=;//sum是钱的总数,即背包容量
msd,dp[]=;//什么都不抢,逃跑率100%
scanf("%lf%d",&p,&n);
for(int i=;i<=n;i++)
{scanf("%d%lf",&v[i].vo,&v[i].va),v[i].va=-v[i].va;
sum+=v[i].vo;} for(int i=;i<=n;i++)
for(int j=sum;j>=;j--)
dp[j]=max(dp[j],dp[j-v[i].vo]*v[i].va); for(int i=sum;i>=;i--)
{
if(dp[i]>-p)
{
printf("%d\n",i);
break;
}
}
}
return ;
}

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

  1. hdu 2955 Robberies (01背包)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:一开始看急了,以为概率是直接相加的,wa了无数发,这道题目给的是被抓的概率,我们应该先求出总的 ...

  2. hdu 2955 Robberies 0-1背包/概率初始化

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

  3. HDU 2955 Robberies(01背包变形)

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

  4. hdu 2955 Robberies (01背包好题)

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

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

    题意:有N个银行,每抢一个银行,可以获得\(v_i\)的前,但是会有\(p_i\)的概率被抓.现在要把被抓概率控制在\(P\)之下,求最多能抢到多少钱. 分析:0-1背包的变形,把重量变成了概率,因为 ...

  6. HDU 2955 Robberies --01背包变形

    这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即 ...

  7. HDOJ 2955 Robberies (01背包)

    10397780 2014-03-26 00:13:51 Accepted 2955 46MS 480K 676 B C++ 泽泽 http://acm.hdu.edu.cn/showproblem. ...

  8. HDU 2955 【01背包/小数/概率DP】

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

  9. HDOJ.2955 Robberies (01背包+概率问题)

    Robberies 算法学习-–动态规划初探 题意分析 有一个小偷去抢劫银行,给出来银行的个数n,和一个概率p为能够逃跑的临界概率,接下来有n行分别是这个银行所有拥有的钱数mi和抢劫后被抓的概率pi, ...

随机推荐

  1. mysql 保存emoji时报,数据库报错:Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x82\xF0\x9F...' for column 'review' at row 1

    错误原因:我们可以看到错误提示中的字符0xF0 0x9F 0x98 0x84 ,这对应UTF-8编码格式中的4字节编码(UTF-8编码规范).正常的汉字一般不会超过3个字节,为什么为出现4个字节呢?实 ...

  2. Spark编程模型及RDD操作

    转载自:http://blog.csdn.net/liuwenbo0920/article/details/45243775 1. Spark中的基本概念 在Spark中,有下面的基本概念.Appli ...

  3. tcp异常终止连接

    服务端: #include <sys/socket.h> #include <unistd.h> #include <sys/types.h> #include & ...

  4. 让IE的Button自适应文字宽度兼容

    width:auto; overflow:visible; 这样将可以去掉IE按钮文字两边多余的空格,并按照自己设置的padding 来显示

  5. 【IIS】windows2008 ii7 设置访问网站提示帐号密码登录

    3个步骤: 1.添加windows身份验证: windows2008默认是不启用的,需要我们自己去启动,在管理工具 - 服务器管理- 角色 ,拉下去,下面有个[添加角色服务],安全性- Windows ...

  6. Windows端口转发

    1. PortTunnel 2. windows 自带的 netsh -----windows下也有一个小工具:portforward.exe,图形界面容易操作,个人平常使用可以,但是也没有办法实现与 ...

  7. C#中的委托到底是什么概念??

    委托,简单理解是这样的.比如您要管您的孩子,把孩子送进了幼儿园.OK.此时您就把您的孩子委托给了幼儿园.当幼儿园放学,将孩子交还给您的手中.则是委托的回调.当然我这里的例子是说异步委托调用.您也可以同 ...

  8. <T> List<T>前面<T>的意思

    先看例子: import java.util.*; class Fruit { public String toString() { return "Fruit"; } } cla ...

  9. wpf xmal基础

    1.名称空间的引用 比如想使用System.Windows.Controls名称空间 首先需要把改名称空间所在的程序集presentationFramework.dll引用到项目里 然后在根元素的起始 ...

  10. CNN- 数据预处理

    对于CNN输入的数据,常见的有三种处理方式: 1.Mean subtraction.  将数据的每一维特征都减去平均值.在numpy 中 X -= np.mean(X, axis = 0) 2.Nor ...