DP训练~Robberies~解题报告

Robberies

文章目录

    • Robberies
    • 题目描述:
    • Input:
    • Output:
    • Sample Input:
    • Sample Output:
    • 题目大意:
    • 思路分析:
    • 代码:

题目描述:

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

题目大意:

有一个人,他想抢劫银行,在抢劫前研究了一下抢劫银行被抓的概率以及能抢的钱,他的母亲同意他抢劫银行,但是必须被抓的概率必须小于一个值。那么这个人在不抓的情况,能偷的钱最大金额是多少

思路分析:

这道题我一开始按照01背包思路去写,发现真的自闭了,这个概率不是加上去,而是相乘,并且概率在数组中精确度比较低,所以把数组元素含义定义为所抢的钱,而不是概率,并且有被抓的概率,那么就有逃跑概率,那么可以计算抢的钱最多的逃跑概率下大于规定的逃跑概率就可以了,思路大致是这样,结合代码来看更清除。

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int W[11000];//金钱 
double V[11000];//被抓概率 
int T;
double dp[11000];//逃跑概率 
double pried;
//这里dp的含义为,在拿了多少钱的下的逃跑概率 
void LemonDP(int sum,int num)//DP开始 
{
	for(int i=0;i<num;i++)//抢哪一个银行 
	{
		for(int j=sum;j>=W[i];j--)//从金额最大开始,决定要抢哪一个 
		{
			dp[j]=max(dp[j],dp[j-W[i]]*(1-V[i]));
			//这里说一下抢钱有2个选择,抢或者不抢,抢了之后,那么就要计算逃跑概率 
		}
	 }
	 for(int i=sum;i>=0;i--)//从金额大到小开始判断 
	 {
	 	if(dp[i]>(1-pried))//如果满足要求,说明该数字最大 
	 	{
	 		cout << i << endl;
	 		break;
		 }
	 }
	 return;
}
int main()
{
	int t;
	int num;
	cin >> t;//输入样例 
	while(t--)
	{
		memset(dp, 0, sizeof(dp));
        dp[0] = 1;//初始化DP数组 
		int sum=0;//统一金钱最大量 
		scanf("%lf %d",&pried,&num);
		for(int i=0;i<num;i++)
		{
			cin >> W[i] >> V[i];
			sum+=W[i];
		}
		LemonDP(sum,num);
	}
}


你可能感兴趣的:(DP训练~Robberies~解题报告)