01背包+卡精度 Hdu 2955

<span style="color:#3333ff;">/*
—————————————————————————————————————————————————————————————————————————————
    author   :     Grant Yuan
    time     :     2014.7.19
    aldorithm:     01背包+卡精度

—————————————————————————————————————————————————————————————————————————————
E - 01背包 基础
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit

Status
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
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

int p[105];
double f[105];
int n,t;
double  ff;
double dp[100005];//抢到价值为i的钱逃走的最大概率
int sum;

int main()
{
	cin>>t;
	while(t--){
	   sum=0;
	   scanf("%lf%d",&ff,&n);
	   for(int i=0;i<n;i++){
		scanf("%d%lf",&p[i],&f[i]);
		sum+=p[i];}

	   memset(dp,0,sizeof(dp));
	   dp[0]=1;
	   for(int i=0;i<n;i++)
		   for(int  j=sum;j>=p[i];j--)
		   {
		   	dp[j]=max(dp[j],dp[j-p[i]]*(1-f[i]));
		   }
        int m=0;
		for(int i=sum;i>=0;i--)
		{
			if(dp[i]>=1-ff){
					m=i;
				break;}
		}
		cout<<m<<endl;
	}
}
</span>
时间: 2024-12-29 04:37:33

01背包+卡精度 Hdu 2955的相关文章

(背包dp)HDU - 2955 Robberies

原题链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2955 (可能失效) 题意:小偷抢劫n个银行,每个银行有两个属性,m为拥有的库存金额,p为小偷偷这个银行被抓的概率.小偷抢银行的总被抓概率不能超过V.求最多抢到的金额. 分析:这题拿到手很容易想到把概率*100,以总概率V为背包容积,以抢到的金额为价值跑01背包. 但是确实本题坑之所在.WA了好多发. 看了大神博客才明白,这里需要一个转化,直接跑以总金额sum为容积,不被抓的概率为价值的背

HDU 3591 The trouble of Xiaoqian(多重背包+01背包)

HDU 3591 The trouble of Xiaoqian(多重背包+01背包) http://acm.hdu.edu.cn/showproblem.php?pid=3591 题意: 有一个具有n种货币的货币系统, 每种货币的面值为val[i]. 现在小杰手上拿着num[1],num[2],-num[n]个第1种,第2种-第n种货币去买价值为T(T<=20000)的商品, 他给售货员总价值>=T的货币,然后售货员(可能,如果小杰给的钱>T, 那肯定找钱)找钱给他. 售货员每次总是用

有关货币问题的动态规划题目--有关01背包,完全背包,多重背包

背包dp:参考背包九讲以及给出一些题目 01背包 (先枚举物品,再逆序枚举容量) 给定n件物品和一个容量为V的背包,每件物品的体积是w[i],价值是va[i](1<=i<=n),求在不超过背包的容量的情况下,怎么选择装这些物品使得得到的价值最大? 例如:有5件物品,体积分别是{2,2,6,5,4},价值分别是{6,3,5,4,6} 递归式:F(i,v)=max(F(i-1,v), F(i-1,v-w[i])+va[i]),其中F(i,v)表示把前i种物品恰放入背包容量为v时取得的最大价值 把这

HDU 2955 Robberies --01背包变形

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

HDU 2546 饭卡(01 背包)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 思路:需要首先处理一下的的01背包,当饭卡余额大于等于5时,是什么都能买的,所以题目要饭卡余额最小,那预留5元(相当于饭卡余额为5)来买最贵的菜 然后对剩下n-1进行01背包dp才是正确的.但是还存在一个问题,那就饭卡初始余额小于5时,也要处理掉. 下面讲01背包(原型可以看大牛的背包九讲,本人也正在学习),定义dp[i][j]为买前i种菜品剩下j元时的最大消费值等于下面两中情况之一的值 有两种来

HDU 2955 Robberies (01背包)

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

HDU 2546 饭卡(01背包裸题)

饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28562    Accepted Submission(s): 9876 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无

hdu 2546 饭卡(0-1背包)

题目来源:hdu 2546 饭卡 饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16645 Accepted Submission(s): 5797 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后

Robberies hdu 2955 01背包

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