HDU 2995 Robberies 概率背包

来源:互联网 发布:win32 串口编程 异步 编辑:程序博客网 时间:2024/05/16 14:40

HDU 2995 Robberies 概率背包
编辑删除转载 2016-10-21 15:47:52
标签:it
Robberies

Time Limit: 2000/1000 MS ( Memory Limit: 32768/32768 K (: 21326 Accepted Submission(s): 7889

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 Constraints0 < T <= 1000.0 <= P <= 1.00 < N <= 1000 < Mj <= 1000.0 <= Pj <= 1.0A 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

题意:

一个小偷去银行偷东西,给定一个p要求被抓住的概率必须小于p,然后给定n个银行的mj和pj,分别表示​小偷在每个银行能偷到的钱和会被抓住的概率。要求小偷在被抓住的概率小于p的情况下,最多能偷多少钱。(����看来小偷也要会编程,不会编程偷不到钱。无业程序员有出路了��)​ :) :) :)

分析:

错误解法:暴力的直接套用01背包的状态转移方程,dp[i] = max{dp[i], dp[i - v[j].p] + v[j].w} 这真是太stupid了,概率不是用来加的,概率和在这里没有任何意义(好气呀,样例用概率加也能过,正式太阴险了)

正确解法:概率应该是相乘的,然而在这里直接算被抓住的概率过于麻烦,于是反过来考虑,算能成功逃跑的概率的最大情况。那么用dp[i]表示偷i的钱能逃跑的最大概率。

得到状态转移方程:dp[i] =max{dp[i], dp[i - v[j].w] * (1 - v[j].p)}
最后找出逃跑概率大于等于p的最大的i

代码:

////  main.cpp//  HDU-Fighting////  Created by sun000 on 16/10/21.//  Copyright © 2016年 sun000. All rights reserved.//#include <cstdio>#include <algorithm>#include <cstring>using namespace std;double dp[10010];//偷钱i时能逃跑的最大概率struct node{    int w;    double p;}v[105];int main(void){    int t, n, sum;    double p;    scanf("%d", &t);    while(t--)    {        memset(dp, 0, sizeof(dp));        dp[0] = 1;        sum = 0;        scanf("%lf%d", &p, &n);        for(int i = 1; i <= n; i++)        {            scanf("%d%lf", &v[i].w, &v[i].p);            sum += v[i].w;        }        for(int i = 1; i <= n; i++)//01背包先循环物品,再循环容量            for(int j = sum; j >= v[i].w; j--)//01背包逆序循环,完全背包正序循环            {                dp[j] = max(dp[j], dp[j - v[i].w] * (1 - v[i].p));                //printf("dp[%d] = %lf\n", j, dp[j]);            }        for(int i = sum; i >= 0; i--)            if(dp[i] >= 1 - p)            {                printf("%d\n", i);                break;            }    }    return0;}​
0 0
原创粉丝点击