HDU 2955 DP

原创 2018年04月15日 11:46:18

Robberies

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


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
30.04 31 0.022 0.033 0.050.06 32 0.032 0.033 0.050.10 31 0.032 0.023 0.05
 

Sample Output
246

废话不多说直接上代码:

#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<map>
#include<queue>
#include<stack>
using namespace std;
/*
用金钱作为背包,把被抓到的概率做价值,每次更新就好
*/
int main(){
    int t,n,i,j,m[110];
    float P,p[110],dp[10010];//dp[]数组下标为此时抢到的钱,值为被抓的概率
    cin>>t;
    while(t--){
        memset(dp,0,sizeof(dp));
        int sum=0;
        cin>>P>>n;
        P=1-P;
        for(i=1;i<=n;i++){
            cin>>m[i]>>p[i];
            p[i]=1.0-p[i];
            sum+=m[i];
        }
        dp[0]=1;
        for( i=1;i<=n;i++){
            for(j=sum;j>=m[i];j--){
                dp[j]=max(dp[j],dp[j-m[i]]*p[i]);
                /*
                因为每次被抓到的这一事件的相互独立的,所以要用乘法
                第一次只有dp[0]有值
                第二次只有dp[0],dp[m[1]],有值
                第三次只有dp[0],dp[m[1]],dp[m[2]],dp[m[1]+m[2]],有值
                ...
                其他的均为0
                */
            }
        }
        for(j=sum;j>=0;j--){
            if(dp[j]>=P) break;
        }
        cout<<j<<endl;;
    }
    
    return 0;
}

DP专题:HDU_2955-Robberies

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 题目描述: 输入T组数据,每组数据的第一行有一个浮点数p,表示允许被抓的最大概率,还有...
  • qq_35323001
  • qq_35323001
  • 2016-08-17 14:11:10
  • 225

HDU2955_Robberies【01背包】

Robberies 题目大意:有一个强盗要去几个银行偷盗,他既想多投点钱,又想尽量不被抓到。已知各个银行 的金钱数和被抓的概率,以及强盗能容忍的最大被抓概率。求他最多能偷到多少钱? 思路:背包问题,原...
  • u011676797
  • u011676797
  • 2014-10-17 19:33:49
  • 2106

hdu2955 Robberies(01背包)

这题我是看题解做的。。。 刚开始一看浮点数遍历,乘100后再算果断WA(和我想的一样)。然后就不会了,看了才知道要转化背包,而且这个概率算的话是相乘,求的是最大逃跑概率,真是从头错到尾啊。。。还有...
  • Flynn_curry
  • Flynn_curry
  • 2016-03-21 22:18:04
  • 2573

hdu2955 Robberies 01背包 dp

链接:点我 题意:一个强盗要去抢劫银行,对于每个银行来说,都有一个不被抓的概率p,和能抢劫到的钱数money,每个银行最多只可以被抢劫一次。问在不被抓的总概率P下,怎样得到最大价值的钱数。 分析:...
  • liuqiyao_01
  • liuqiyao_01
  • 2013-04-01 19:15:40
  • 3330

HDU DP动态规划

  • 2010年03月31日 10:48
  • 454KB
  • 下载

hdu(2955)——Robberies

题意: 现在一个人想去抢劫银行,如果他被抓的概率低于P的话,那么他就是安全的。 然后给出N,代表他想抢劫的银行的个数,然后N行,有Mj,Pj,代表的是银行有Mj这么多钱,然后被抓的概率是Pj。 然后问...
  • ACMer_hades
  • ACMer_hades
  • 2015-09-20 23:00:10
  • 445

JAVA(HDU)杭电ACM 2955

动态规划题目 注意概率是相乘而不是相加import java.util.Scanner;public class Main { public static void main(String a...
  • skyonefly
  • skyonefly
  • 2016-04-17 14:07:18
  • 553

HDU 2955 Robberies 01背包

HDU 2955 Robberies Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su...
  • lwt36
  • lwt36
  • 2015-02-06 14:42:50
  • 358

HDU2955 Robberies

题目链接实质是01背包问题,关键还是找出转移方程。 顺着题意做非常困难,需要转换思维,把最大风险指数改成最小逃跑所需指数,各银行的被抓指数也改成能逃跑的指数pi=1-pi,因此想要成功逃跑,总逃跑指...
  • L954688947
  • L954688947
  • 2015-10-20 21:48:35
  • 293

hdu 2955 Robberies解题报告

/*题意:(01背包) Roy想要抢劫银行,每家银行多有一定的金额和被抓到的概率,知道Roy被抓的最大概率P, 求Roy在被抓的情况下,抢劫最多。 分析:被抓概率可以转换成安全概率,Roy的安全...
  • qq_21899803
  • qq_21899803
  • 2015-07-27 18:24:51
  • 246
收藏助手
不良信息举报
您举报文章:HDU 2955 DP
举报原因:
原因补充:

(最多只允许输入30个字)