zoukankan      html  css  js  c++  java
  • HDU 1203 【01背包/小数/概率DP】

    I NEED A OFFER!
    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 33247 Accepted Submission(s): 13449

    Problem Description
    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 '%'.

    Author
    Speakless

    Source
    Gardon-DYGG Contest 2

    【分析】:
    可以拿到至少一份offer的最大概率p1。这句话需要转化为不拿到offer的最小概率p2 (dp[i]表示花费i万元得不到offer的最小概率 )。p1 = 1 - p2 ,这样就可以避免分类的情况。

    第二:最大背包因为状态转移变成了最小背包,注意变化。(PS:1-w[i]是不拿到offer, min是最小概率

    状态转移方程:dp[j]=min(dp[j],dp[j-w[i]]*(1.0–v[i]));

    第三:输入的最后有两个0。按题意是m!=0&&n!=0 ,而实际上n||m才能过QAQ

    【代码】:

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,n,x) for(int i=(x); i<(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int,int> Pr;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e18;
    const int MAXN =  1e3 + 5;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int maxn = 1e5+5;
    const int mod=1e9+7;
    int  w[maxn];
    double dp[maxn],p[maxn];
    int n,m;
    int main()//dp[i]表示花费i万元得不到offer的最小概率
    
    {
        while(cin >> m >> n, n||m)
        {
            for(int i=0;i<=m;i++) dp[i]=1;//初始化为1.0
            for(int i=1;i<=n;i++){
                cin >> w[i] >> p[i];
                p[i] = 1.0-p[i];
            }
            for(int i=1; i<=n; i++){
                for(int j=m; j>=w[i]; j--){
                   dp[j]=min(dp[j], dp[j-w[i]]*p[i]);
                }
            }
            printf("%.1f%%
    ",(1.0-dp[m])*100);
        }
    }
    /*
    10 3
    4 0.1
    4 0.2
    5 0.3
    0 0
    */
    
    
  • 相关阅读:
    Rest Project Performace Pressure Test
    Tomcat APR & Linux Optimization
    关于启用 HTTPS 的一些经验分享(二)
    关于启用 HTTPS 的一些经验分享(一)
    JVM垃圾回收机制总结:调优方法
    LVS+Keepalived搭建高可用负载均衡
    LVS搭建负载均衡(二)DR模型
    LVS搭建负载均衡(一)NAT模型
    编译安装Nginx和PHP(带编译mysql)
    centos7下利用httpd2.4配置svn并使用Ldap用户认证
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9061849.html
Copyright © 2011-2022 走看看