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. RPC 框架原理详解

    首先了解什么叫RPC,为什么要RPC,RPC是指远程过程调用?也就是说两台服务器A,B,一个应用部署在A服务器上,想要调用B服务器上应用提供的函数/方法,由于不在一个内存空间,不能直接调用,需要通过网 ...

  2. 反编译与调试APK

    0×01前言 这年头,apk全都是加密啊,加壳啊,反调试啊,小伙伴们表示已经不能愉快的玩耍了.静态分析越来越不靠谱了,apktool.ApkIDE.jd GUI.dex2jar等已经无法满足大家的需求 ...

  3. 用SecureCRT连接虚拟机

    1.Root用户进入虚拟机系统 2.打开控制台 3.永久关闭防火墙,打开sshd,这样SecureCRT才能连接 chkconfig iptables off;service sshd start 4 ...

  4. app兼容性测试的几种方案

    1.统计自己的应用被使用的数据 通过友盟或Flurry等在应用嵌入,得到应用在哪些机型上被安装了,排名前十的就是测试的重点机型 2.可参考兼容性测试平台的测试结果 比如Testin或百度的MTC平台, ...

  5. Linux 操作命令列表记录

    Linux 操作命令列表记录 SSH登录 登录 ## 范式 ssh [username]@[host] ## 例 ssh -p 1222 root@10.0.0.1 使用非默认端口(ssh默认端口22 ...

  6. 在ubuntu下设置eclipse开发STM32等嵌入式设备

    之前为了能够让ROS与底层能够顺利通讯,我采用可开源开发板arduino ,因为arduino有ROS的库,能够按照ROS wiki上所给的教程就可以顺利的开发,但由于arduino的局限性,我觉得是 ...

  7. java实现线性表

    /** * 线性表 * @author zyyt * */ public  class LinkList {//框架级别的大师级 private int size;//链表的实际大小 private ...

  8. 史上最详细SharePoint 2013安装步骤图解新手教程

    来源:// http://www.itexamprep.com/cn/microsoft/soft/sharepoint2013/2013/0408/2866.html 文章就是SharePoint2 ...

  9. CUDA编程入门,Dim3变量

    dim3是NVIDIA的CUDA编程中一种自定义的整型向量类型,基于用于指定维度的uint3. 例如:dim3 grid(num1,num2,num3): dim3类型最终设置的是一个三维向量,三维参 ...

  10. linux内存管理(repost)

    一 为什么需要使用虚拟内存 大家都知道,进程需要使用的代码和数据都放在内存中,比放在外存中要快很多.问题是内存空间太小了,不能满足进程的需求,而且现在都是多进程,情况更加糟糕.所以提出了虚拟内存,使得 ...