zoukankan      html  css  js  c++  java
  • 01背包(小数)

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

    Robberies

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 37202    Accepted Submission(s): 13323


    Problem Description
    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
     
    Source
     
    Recommend
    gaojie   |   We have carefully selected several similar problems for you:  1203 2159 2844 1864 1231
     
    题意:母亲允许的最大被捉的概率为p , n家银行,给出每家银行可以偷窃的金额pj , 和被捉的概率。
    求在母亲允许的最大被捉概率下,小偷能窃取的最大金额。
     
    题解:因为背包容量为小数,所以以价值为容量。
    dp[j]表示价值为j时,最大能逃脱的概率。
    最后输出从后往前遍历价值,如果该价值的最大逃跑概率大于题目要求即输出。
    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdio.h>
    #include <queue>
    #include <stack>;
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 1000000007
    #define PI acos(-1)
    using namespace std;
    typedef long long ll ;
    int  val[10009];
    double w[10009] , dp[10009] ;
    
    int main()
    {
        int t;
        scanf("%d" , &t);
        while(t--)
        {
            int n ;
            double v ;
            scanf("%lf%d" , &v , &n);
            int sum = 0 ;
            for(int i = 0 ; i < n ; i++)
            {
                scanf("%d%lf" , &val[i] , &w[i]);
                sum += val[i];
            }
            memset(dp , 0 , sizeof(dp));
            dp[0] = 1 ;
            for(int i = 0 ; i < n ; i++)
            {
                for(int j = sum ; j >= val[i] ; j--)
                {
                    dp[j] = max(dp[j] , dp[j-val[i]]*(1-w[i]));
                }
            }
            for(int i = sum ; i >= 0 ; i--)
            {
                if(dp[i] >= (1-v))
                {
                    printf("%d
    " , i);
                    break ;
                }
            }
        }
    
        return 0;
    }

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

    I NEED A OFFER!

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 41113    Accepted Submission(s): 16313


    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
    题意:有n元,m个工作,每个工作需要val元,该工作可能拿到offer的概率。
    问该n元,最少拿到一份offer的最大概率
     
     
     
     题解:dp【j】表示花费j元时,最小没有offer的概率。
     
    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <stdio.h>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 1000000007
    #define PI acos(-1)
    using namespace std;
    typedef long long ll ;
    int val[10009];
    double dp[10009];
    double w[10009];
    
    int main()
    {
        /*#ifdef ONLINE_JUDGE
        #else
            freopen("D:/c++/in.txt", "r", stdin);
            freopen("D:/c++/out.txt", "w", stdout);
        #endif*/
        int n , m ;
        while(~scanf("%d%d" , &n , &m) && n + m)
        {
            for(int i = 1 ; i <= m ; i++)
            {
                scanf("%d%lf" , &val[i] , &w[i]);
            }
            for(int i = 0 ; i <= n ; i++)//一开始没花钱,所以一定没有offer的概率为1.
            {
                dp[i] = 1 ;
            }
            for(int i = 1 ; i <= m ; i++)
            {
                for(int j = n ; j >= val[i] ; j--)
                {
                    dp[j] = min(dp[j] , dp[j-val[i]] * (1 - w[i]));
                }
            }
            printf("%.1f%%
    " , (1-dp[n])*100);
        }
    
    
    
    
        return 0 ;
    }
    
  • 相关阅读:
    @ControllerAdvice 全局异常处理
    SpringBoot 单文件和多文件上传
    Springboot application 本地HTTPS配置
    不使用spring-boot-starter-parent进行依赖的版本管理
    构造函数和函数式接口
    函数式接口和Lambda表达式
    使用FunctionalInterface提供工厂方法
    Future 和 CompletableFuture 异步任务 in Java
    单例
    使用私有仓库(Docker Registry 2.0)管理镜像
  • 原文地址:https://www.cnblogs.com/nonames/p/11679869.html
Copyright © 2011-2022 走看看