HDU-2995

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.

 

先将p和q[i]都取他们被1减去的,也就是没被抓的概率,然后将所有银行的 钱加起来当做sum,通过n和sum来进行for循环,转移方程是:dp[j]=max(dp[j],dp[j-a[i]]*q[i]);就是计算偷那几家银行逃跑概率超过1-p并且钱是最多的,循环完了再从sum往回循环找第一个大于1-p的i,这个i就是符合条件的最大金额。

这道题是概率+01背包,做为DP初学者感觉怪难的,看题解才会,仔细想了想还是题做少了。。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
float f[20000];

int main(){
	int t,n,a[200];float p,q[200];
	cin>>t;
	while(t--){
        int sum = 0;
        memset(f,0,sizeof(f));
		cin>>p>>n;//p是低于的概率,以及整数N,他有计划的银行的数量
        p = 1 - p;//大于p则ok
        for(int i=1;i<=n;i++){
			cin>>a[i]>>q[i];//第i银行有a[i]百万元,被抢概率为q[i]
            sum += a[i];
            q[i] = 1 - q[i];//q[i]是不被抓的概率
        }
        f[0] = 1;
        for (int i = 1;i<=n;i++){
            for (int j = sum; j >= a[i];j--){
                f[j] = max(f[j], f[j - a[i]] * q[i]);
            }
        }
        int i = 0;
        for (i = sum;i>=0;i--)
            if(f[i]>=p)
                break;
        cout << i << endl;
    } 
    return 0;
} 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值