Lintcode-背包问题IX

作者: 爱秋刀鱼的猫 | 来源:发表于2018-04-07 22:11 被阅读10次
题目

You have a total of 10 * n thousand yuan, hoping to apply for a university abroad. The application is required to pay a certain fee. Give the cost of each university application and the probability of getting the University's offer, and the number of university is m. If the economy allows, you can apply for multiple universities. Find the highest probability of receiving at least one offer.
你总共有10 * n 元,希望申请国外大学。 该申请需要支付一定的费用。 给出每个大学申请的费用和获得大学录取通知书的可能性,大学数量为m。 如果经济允许,你可以申请多所大学。 找到接收至少一个offer的最高概率。

样例
n = 10
prices = [4,4,5]
probability = [0.1,0.2,0.3]
Return:0.440
分析

这道题的关键在于题干中给出的至少两个字。在概率论中经常会碰到这样的题,就是求一个事物至少发生一次的概率。我们经常的做法是求这个事件的对立事件:求一次都没有发生过的概率P,那么原事件就变为了:1-P。
对于我们这个问题,就变为了求没有收到一所大学的offer概率。这道题目转化为背包问题就是:有n个大学,每一个大学的费用和不给offer的概率已知,求没有收到一个大学的offer的最小概率。(没有收到offer概率最小就是说至少收到一所大学offer概率最大)
这时候,我们的问题就变为了0-1背包问题,每一个dp[i]表示的是前i个学校,都没有收到offer的最小概率。
状态转移方程是:
dp[j] = min(dp[j],dp[j-prices[i]]*probability[I]);
根据这个方程,代码如下:

class Solution {
public:
    /**
     * @param n: Your money
     * @param prices: Cost of each university application
     * @param probability: Probability of getting the University's offer
     * @return: the  highest probability
     */
    double backpackIX(int n, vector<int> &prices, vector<double> &probability) {
        // write your code here
        int len = prices.size();
        for(int i =0;i<len;++i)
        {
            probability[i]=1- probability[i];//没有收到offer的概率
        }
        vector<double> dp(n+1,1);
        for (int i = 0; i < len; ++i)
        {//0-1背包问题
            for(int j = prices[i];j>=prices[i];j--)
            {
                dp[j]=min(dp[j],dp[j-prices[i]] *probability[i]);
            }
        }
        return 1-dp[n];
    }
};

相关文章

  • Lintcode-背包问题IX

    题目 You have a total of 10 * n thousand yuan, hoping to ap...

  • LintCode-背包问题I、II、III、IV、V、VII、V

    I 描述 在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i] 你不可以将...

  • 背包问题(完全背包)

    动态规划合集: 1.矩阵链乘法2.投资组合问题3.完全背包问题4.01背包问题5.最长公共子序列 例题3——背包问...

  • Python Pandas库 常见使用错误总结

    DataFrame对象的.ix[idx] 与 .ix[[idx]] 区别

  • Algorithm进阶计划 -- 动态规划(下)

    经典动态规划背包问题最长子序列问题 1. 背包问题 1.1 0-1 背包问题 0-1 背包问题,描述如下: 上面...

  • 背包问题

    0-1背包问题 问题 n个物体,它们各自有重量和价值,给定一个有容量的背包,如何让背包里装入的物体价值总和最大? ...

  • 背包问题

    问题描述 假如有一个能装下4磅的背包,如何从下列商品列表中选择商品填充背包,使价值达到最大: 动态规划 要解决背包...

  • 背包问题

    (1)0-1背包容量为10的背包,有5种物品,每种物品只有一个,其重量分别为5,4,3,2,1,其价值分别为1,2...

  • 背包问题

  • 背包问题

    01背包(物品只有一个) 有N件物品和一个容量为V的背包。第i建物品的费用是w[i],价值是v[i]。求解将哪些物...

网友评论

    本文标题:Lintcode-背包问题IX

    本文链接:https://www.haomeiwen.com/subject/gxruhftx.html