01背包,概率

原题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): 11820    Accepted Submission(s): 4398

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
//题目大意,给你一个概率,然后n组数据,每组数据的整数代表钱,小数代表被抓的概率
//问,在不被抓的概率的前提下,最多能拿多少钱。
//思路,开个dp数组,代表钱在n的时候被抓的不被抓的概率,
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define N 100 + 10
double dp[10009];
int sum[N];
double p[N];

double max(double a,double b)
{
    return a>b?a:b;
}

int main()
{
    int T,n,i;
    double v;

    while(~scanf("%d",&T))
    {
        while(T--)
        {
            memset(dp,0,sizeof(dp));
            dp[0] = 1;
            memset(sum,0,sizeof(sum));
            memset(p,0,sizeof(p));
            scanf("%lf%d",&v,&n);
            int mark = 0;
            for(i=1; i<=n; i++)
            {
                scanf("%d%lf",&sum[i],&p[i]);
                mark+=sum[i];
                p[i] = 1-p[i];
            }
            int j;
            for(i=0;i<=mark;i++){
                dp[i] = 0;
            }
            dp[0] =1;//当抢了0元时,肯定是安全的。所以初始化为1
            for(i=1; i<=n; i++)
            {
                for(j=mark; j>=sum[i]; j--)
                {
                    dp[j] = max(dp[j],dp[j-sum[i]]*(p[i]));
                }
            }
            v = 1-v;
            for(i=mark; i>=0; i--)
            {
                if(dp[i] >= v){
                    printf("%d\n",i);
                    break;

                }

            }
        }
    }

    return 0;
}

01背包,概率

时间: 2024-10-23 17:12:04

01背包,概率的相关文章

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

/*Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13854 Accepted Submission(s): 5111 Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that the

HDU 1203 I NEED A OFFER! 01背包 概率运算预处理。

题目大意:中问题就不说了 ^—^~ 题目思路:从题目来看是很明显的01背包问题,被录取的概率记为v[],申请费用记为w[].但是我们可以预先做个处理,使问题解决起来更方便:v[]数组保留不被录取的概率,则dp[j]则代表在j元费用下,不被录取的最低概率是多少,最后用1减去dp[n]即可. dp式子为:dp[j]=min(dp[j],dp[j-w[i]]*v[i]); (j表示共有j元申请费用). 详细看代码注释 #include<cstdio> #include<stdio.h>

hdoj 1203 I NEED A OFFER! 【另类01背包】【概率背包】

题意:... 策略:动态规划. 因为是求至少能得到一个offer的概率,那我们可以反着求,求得不到一个offer的概率,最后用1减去就好了. 代码: #include<string.h> #include<stdio.h> double dp[10010]; struct node{ int a; double b; }s[10010]; int main() { int n, m, i, j; while(scanf("%d%d", &n, &

LightOJ1079---Just another Robbery (概率做01背包)

As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - Hermione and Ron

hdu2955---Robberies(概率做01背包)

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

(概率 01背包) Just another Robbery -- LightOJ -- 1079

http://lightoj.com/volume_showproblem.php?problem=1079 Just another Robbery As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated r

HDU 2955 Robberies --01背包变形

这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即转化为01背包问题. 此时dp[v]表示抢劫到v块钱成功逃跑的概率,概率相乘. 最后从大到小枚举v,找出概率大于逃跑概率的最大v值,即为最大抢劫的金额. 代码: #include <iostream> #include <cstdio> #include <cstring>

hdu 2955 Robberies(01背包)

Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17723    Accepted Submission(s): 6544 Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that

杭电1203--I NEED A OFFER!(01背包)

I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20582    Accepted Submission(s): 8214 Problem Description Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了.要申请国外的任何大学,你都要