HDU 1203 I NEED A OFFER! 动态规划01背包

http://acm.hdu.edu.cn/showproblem.php?pid=1203

题意:

  一道01背包的水题,每一行分别是申请所需的费用和被录取的概率,Speakless只有一定的钱,要在这些规定的钱内找到至少能被一所学校

录取的概率的最大值。

 

坑爹:

  才刚开始看01背包问题,对初始化的的操作不是很清楚,而且这个在计算f[n]的时候要尽量的小,而达到1-f[n]最大。

 

解法:

  套用01背包的公式,而且将 f 数组全部初始化为1,从而保证没有一所学校录取的时候 1 - f [n] 为 0.

 

View Code
 1 #include<iostream>
 2 using namespace std;
 3 
 4 const int maxn = 100000 +10;
 5 
 6 struct node
 7 {
 8     int price;
 9     double chance;
10 };
11 struct node offer[maxn];
12 double f[maxn];
13 int n;
14 int m;
15 
16 void ZeroOnePack(int cost,double weight)
17 {
18     int j;
19     for(j=n; j>=cost; j--)
20     {
21         if(f[j]>f[j-cost]*weight)
22         {
23             f[j]=f[j-cost]*weight;
24         }
25     }
26 }
27 
28 int main()
29 {
30     while(cin>>n>>m,n+m)
31     {
32         int i;
33         for(i=0; i<=n; i++)
34         {
35             f[i]=1.0;
36         }
37         for(i=1; i<=m; i++)
38         {
39             cin>>offer[i].price>>offer[i].chance;
40             offer[i].chance=1.0-offer[i].chance;
41         }
42         for(i=1; i<=m; i++)
43         {
44             ZeroOnePack(offer[i].price,offer[i].chance);
45         }
46         printf("%.1lf%%\n",(1.0-f[n])*100);
47     }
48     return 0;
49 }

 

转载于:https://www.cnblogs.com/pcpcpc/archive/2012/09/05/2672427.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值