zoukankan      html  css  js  c++  java
  • Weekly Contest 78-------->808. Soup Servings

    There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There are four kinds of operations:

    1. Serve 100 ml of soup A and 0 ml of soup B
    2. Serve 75 ml of soup A and 25 ml of soup B
    3. Serve 50 ml of soup A and 50 ml of soup B
    4. Serve 25 ml of soup A and 75 ml of soup B

    When we serve some soup, we give it to someone and we no longer have it.  Each turn, we will choose from the four operations with equal probability 0.25. If the remaining volume of soup is not enough to complete the operation, we will serve as much as we can.  We stop once we no longer have some quantity of both types of soup.

    Note that we do not have the operation where all 100 ml's of soup B are used first.  

    Return the probability that soup A will be empty first, plus half the probability that A and B become empty at the same time.

    Example:
    Input: N = 50
    Output: 0.625
    Explanation: 
    If we choose the first two operations, A will become empty first. For the third operation, A and B will become empty at the same time. For the fourth operation, B will become empty first. So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.
    
    

    Notes:

    • 0 <= N <= 10^9
    • Answers within 10^-6 of the true value will be accepted as correct.

    Approach #1: C++. Using DP.

    class Solution {
    private:
        double memo[200][200];
        
    public:
        double soupServings(int N) {
            return N > 4800 ? 1.0 : solve((N+24) / 25, (N + 24) / 25);
        }
    private:
        double solve(int a, int b) {
            if (a <= 0 && b <= 0) return 0.5;
            if (a <= 0) return 1.0;
            if (b <= 0) return 0.0;
            if (memo[a][b] > 0) return memo[a][b];
            memo[a][b] = 0.25 * (solve(a-4, b) + solve(a-3, b-1) + solve(a-2, b-2) + solve(a-1, b-3));
            return memo[a][b];
        }
    };
    

      

    Approach #2: Java.

    class Solution {
        private double[][] memo = new double[200][200];
        
        public double soupServings(int N) {
            return N > 4800 ? 1.0 : solve((N + 24) / 25, (N + 24) / 25);
        }
        
        private double solve(int a, int b) {
            if (a <= 0 && b <= 0) return 0.5;
            if (a <= 0) return 1.0;
            if (b <= 0) return 0.0;
            if (memo[a][b] > 0) return memo[a][b];
            memo[a][b] = 0.25 * (solve(a-4, b) + solve(a-3, b-1) + solve(a-2, b-2) + solve(a-1, b-3));
            return memo[a][b];
        }
    }
    

      

    Approach #3: Python.

    class Solution(object):
        memo = {}
        def soupServings(self, N):
            if N > 4800: return 1
            def f(a, b):
                if (a, b) in self.memo: return self.memo[a, b]
                if a <= 0 and b <= 0: return 0.5
                if a <= 0: return 1
                if b <= 0: return 0
                self.memo[(a, b)] = 0.25 * (f(a - 4, b) + f(a - 3, b - 1) + f(a - 2, b - 2) + f(a - 1, b - 3))
                return self.memo[(a, b)]
            N = math.ceil(N / 25.0)
            return f(N, N)
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    WPF treeview 多层次绑定问题 HierarchicalDataTemplate 和 CompositeCollection
    Difference between Visibility.Collapsed and Visibility.Hidden
    WPF style的继承
    Asp.net个性化服务《系列03_购物车》
    SQLServer2005中的uniqueidentifier数据类型与NEWID()函数
    扉页语
    Asp.net个性化服务《系列01_概述》
    Asp.net个性化服务《系列02_快速的匿名用户个性化》
    2010年java课程体系
    easyui修改提示窗
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9974242.html
Copyright © 2011-2022 走看看