I NEED AN OFFER! - 01背包问题

本文介绍了一个留学申请场景下的最大概率获得Offer的算法实现。通过01背包问题的变形,求解在有限预算内如何选择学校才能最大化获得至少一份Offer的概率。文章提供了完整的C++代码示例。

Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了。要申请国外的任何大学,你都要交纳一定的申请费用,这可是很惊人的。Speakless没有多少钱,总共只攒了n万美元。他将在m个学校中选择若干的(当然要在他的经济承受范围内)。每个学校都有不同的申请费用a(万美元),并且Speakless估计了他得到这个学校offer的可能性b。不同学校之间是否得到offer不会互相影响。“I NEED A OFFER”,他大叫一声。帮帮这个可怜的人吧,帮助他计算一下,他可以收到至少一份offer的最大概率。(如果Speakless选择了多个学校,得到任意一个学校的offer都可以)。 
Input输入有若干组数据,每组数据的第一行有两个正整数n,m(0<=n<=10000,0<=m<=10000) 
后面的m行,每行都有两个数据ai(整型),bi(实型)分别表示第i个学校的申请费用和可能拿到offer的概率。 
输入的最后有两个0。 
Output每组数据都对应一个输出,表示Speakless可能得到至少一份offer的最大概率。用百分数表示,精确到小数点后一位。 
Sample Input
10 3
4 0.1
4 0.2
5 0.3
0 0
Sample Output
44.0%


        
  
Hint
You should use printf("%%") to print a '%'.


分析:

若是按照普通的01背包:dp[i][j]=max(dp[i][j],dp[i][j-a[i]*b[i])表示拿到所有offer的最大概率

dp[i][j]=min(dp[i][j],dp[i][j-a[i]]*(1-b[i]))表示拿不到所有offer的最小概率,既为题中拿到至少1个offer的最大概率

注意开始时dp[i]要全部设为1。

#include<iostream>  
#include<cstdio>  
#include<algorithm>  
#include<cstring>  
  
using namespace std;  
double b[10005];  
int a[10005];  
double dp[10005];  
  
int main(){  
    int n,m;  
    while(scanf("%d%d",&n,&m)!=EOF&&m+n){  
        fill(dp,dp+10005,1);  
        for(int i=0;i<m;i++){  
            scanf("%d %lf",&a[i],&b[i]);  
        }  
        for(int i=0;i<m;i++){  
            for(int j=n;j>=a[i];j--){
                dp[j]=min(dp[j],dp[j-a[i]]*(1-b[i]));
            }  
        }  
        printf("%.1lf%%\n",(1-dp[n])*100);  
    }  
}  
<?xml version="1.0"?> <package> <name>uav_utils</name> <version>0.0.0</version> <description>The uav_utils package</description> <!-- One maintainer tag required, multiple allowed, one person per tag --> <!-- Example: --> <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> --> <maintainer email="liutianbocn@gmail.com">LIU Tianbo</maintainer> <!-- One license tag required, multiple allowed, one license per tag --> <!-- Commonly used license strings: --> <!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 --> <license>LGPLv3</license> <!-- Url tags are optional, but mutiple are allowed, one per tag --> <!-- Optional attribute type can be: website, bugtracker, or repository --> <!-- Example: --> <!-- <url type="website">http://wiki.ros.org/geometry_utils</url> --> <!-- Author tags are optional, mutiple are allowed, one per tag --> <!-- Authors do not have to be maintianers, but could be --> <!-- Example: --> <!-- <author email="jane.doe@example.com">Jane Doe</author> --> <!-- The *_depend tags are used to specify dependencies --> <!-- Dependencies can be catkin packages or system dependencies --> <!-- Examples: --> <!-- Use build_depend for packages you need at compile time: --> <!-- <build_depend>message_generation</build_depend> --> <!-- Use buildtool_depend for build tool packages: --> <!-- <buildtool_depend>catkin</buildtool_depend> --> <!-- Use run_depend for packages you need at runtime: --> <!-- <run_depend>message_runtime</run_depend> --> <!-- Use test_depend for packages you need only for testing: --> <!-- <test_depend>gtest</test_depend> --> <buildtool_depend>catkin</buildtool_depend> <build_depend>roscpp </build_depend> <build_depend>rospy </build_depend> <build_depend>cmake_modules </build_depend> <build_depend>cmake_utils </build_depend> <run_depend>roscpp </run_depend> <run_depend>rospy </run_depend> <run_depend>cmake_modules </run_depend> <run_depend>cmake_utils </run_depend> <!-- The export tag contains other, unspecified, tags --> <export> <!-- Other tools can request additional information be placed here --> </export> </package> 注释上述代码保留原注释
最新发布
09-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值