Light Oj1079、hdu2955

Light Oj1079hdu2955

Description

AsHarry Potter series is over, Harry has no job. Since he wants to make quickmoney, (he wants everything quick!) so he decided to rob banks. He wants tomake a calculated risk, and grab as much money as possible. But his friends -Hermione and Ron have decided upon a tolerable probability P of getting caught. They feel that heis safe enough if the banks he robs together give a probability less than P.

Input

Inputstarts with an integer T (≤ 100),denoting the number of test cases.

Eachcase contains a real number P,the probability Harry needs to be below, and an integer N (0 < N ≤ 100), the number of banks he has plans for. Thenfollow N lines, where line j gives an integer Mj (0 < Mj ≤ 100)and a real number Pj. Bank j contains Mj millions, and theprobability of getting caught from robbing it is Pj. A bank goes bankrupt if it is robbed, and you mayassume that all probabilities are independent as the police have very lowfunds.

Output

Foreach case, print the case number and the maximum number of millions he canexpect to get while the probability of getting caught is less than P.

Sample Input

3

0.043

10.02

20.03

30.05

0.063

20.03

20.03

30.05

0.103

10.03

20.02

30.05

Sample Output

Case1: 2

Case2: 4

Case3: 6

注意:这题主要考查逆01背包和概率。逆01背包上面已经讲过,因此该题还需注意概率问题。

如:物品(价格,体积)

银行一;(3,0.2) 银行二(5,0.4)

b[1]=0;

b[2]=0;

b[3]=0.2;

b[4]=0;  

b[5]=0.4;

b[6]=0;

b[7]=0;

b[8]=b[3]+b[5]*(1-b[3])=0.52; 1-b[3]表示在偷完第一个银行后没有被抓的概率,只有在偷第一次没有被抓的情况下才能偷第二次,b[5]*(1-b[3])表示第二次被抓的概率)

而不是b[8]=b[3]+b[5]=0.6;(起初这样写的,后来发现错了)     

b[8]=0.52表示哈利如果8亿元那么被抓的概率为0.52

 

My solution

/*2016.3.1*/

#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
double   b[10100],v[150]; 
int  val[150];//val[]存放价值;v[]存放概率(即:体积) 
int main()
{
	int i,j,n,m,k,t,h,sum;
	double p;
	scanf("%d",&t);
	for(h=1;h<=t;h++) 
	{
		sum=0;
		memset(b,0,sizeof(b));//背包初始化为0 
		scanf("%lf",&p);
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%d%lf",&val[i],&v[i]);
			sum+=val[i];//sum记录所有银行的总价值量,从而确定b[]数组的规模 
		}
		for(i=1;i<=n;i++)
		{
			for(j=sum;j>0;j--)//b[]数组下标最大只能为sum,不可能比sum还大 
			{
				if(b[j-val[i]]!=0)//相当于上面例子中的b[8-val[2]]=b[3]=0.2!=0(val[2]=5) 
				{
				  if(b[j]==0)//如果在填充前,b[8]=0,可以直接填充 
					b[j]=v[i]*(1-b[j-val[i]])+b[j-val[i]];
					else 
					b[j]=min(b[j],v[i]*(1-b[j-val[i]])+b[j-val[i]]);
//如果b[8]之前已经填充过,此次填充时,需比较后取最小值,如:b[8]=0.43, 此时经过比较后b[8]取0.43,不是0.52。
				}
				else
				{
					if(j==val[i])
					{
						if(b[j]!=0)      //例如:银行一:(3,0.4) 银行二:(3,0.02)
						b[j]=min(v[i],b[j]);   //则b[3]=min(b[3],0.02)=0.02
						else
					    b[j]=v[i];
					}
				 }
			}
		}
		for(j=sum;j>0;j--)
		if(b[j]<=p&&b[j]!=0)//注意:b[j]!=0这点容易忽略
		break;
		printf("Case %d: %d\n",h,j);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值