📜  动态编程|高强度与低强度任务问题

📅  最后修改于: 2021-04-29 05:19:05             🧑  作者: Mango

您有n天的时间,并且每天(di)可以执行繁重的任务(hi)或繁重的任务(li),也可以不执行任何任务,并且只有在选择了以下条件后才能选择繁重的任务前一天没有任务。编写程序以查找您在这n天内可以执行的最大任务量。
例子:

No. of days (n) = 5
Day      L.E.   H.E
1        1       3
2        5       6
3        4       8
4        5       7
5        3       6
Maximum amount of tasks 
        = 3 + 5 + 4 + 5 + 3 
        = 20

最佳子结构
为了找到直到第一天为止完成的最大任务量,我们需要比较2个选择:

  1. 那天去完成高强度的任务,然后找到直到(i – 2)天为止完成的最大任务量。
  2. 在那天进行省力的任务,找到直到第(i – 1)天完成的最大任务量。

设高[1…n]为第i天的高工作量任务的输入数组,而低[1…n]为第i天的低工作量任务的输入数组。
令max_task(high [],low [],i)为返回直到第i天已完成的最大任务量的函数,因此它将返回max(high [i] + max_task(high,low,(i – 2))) ,低[i] + max_task(高,低,(i – 1)))
因此,该问题具有最佳的子结构属性,因为可以使用子问题的解决方案来解决该问题。
重叠子问题
以下是“尽力而为”与“不尽力”任务问题的简单递归实现。该实现仅遵循上述递归结构。因此,高强度任务与低强度任务问题具有动态编程问题的两个属性。

C++
// A naive recursive C++ program to find maximum
// tasks.
#include 
using namespace std;
 
// Returns the maximum among the 2 numbers
int max(int x, int y)
{
    return (x > y ? x : y);
}
 
// Returns maximum amount of task that can be
// done till day n
int maxTasks(int high[], int low[], int n)
{
    // If n is less than equal to 0, then no
    // solution exists
    if (n <= 0)
        return 0;
 
    /* Determines which task to choose on day n,
    then returns the maximum till that day */
    return max(high[n - 1] + maxTasks(high, low, (n - 2)),
            low[n - 1] + maxTasks(high, low, (n - 1)));
}
 
// Driver code
int main()
{
    int n = 5;
    int high[] = {3, 6, 8, 7, 6};
    int low[] = {1, 5, 4, 5, 3};
    cout << maxTasks(high, low, n);
    return 0;
}
 
// This code is contributed by Shubhamsingh10


C
// A naive recursive C program to find maximum
// tasks.
#include
 
// Returns the maximum among the 2 numbers
int max(int x, int y)
{
    return (x > y ? x : y);
}
 
// Returns maximum amount of task that can be
// done till day n
int maxTasks(int high[], int low[], int n)
{
    // If n is less than equal to 0, then no
    // solution exists
    if (n <= 0)
        return 0;
 
    /* Determines which task to choose on day n,
       then returns the maximum till that day */
    return max(high[n-1] + maxTasks(high, low, (n-2)),
              low[n-1] + maxTasks(high, low, (n-1)));
}
 
// Driver program to test above function
int main()
{
    int n = 5;
    int high[] = {3, 6, 8, 7, 6};
    int low[] = {1, 5, 4, 5, 3};
    printf("%dn", maxTasks(high, low, n));
    return 0;
}


Java
// A naive recursive Java program
// to find maximum tasks.
 
class GFG{
     
    // Returns maximum amount of task
    // that can be done till day n
    static int maxTasks(int high[], int low[], int n)
    {
         
        // If n is less than equal to 0,
        // then no solution exists
        if (n <= 0)
            return 0;
 
        /* Determines which task to choose on day n,
            then returns the maximum till that day */
        return Math.max(high[n - 1] + maxTasks(high, low, (n - 2)),
                low[n - 1] + maxTasks(high, low, (n - 1)));
    }
 
    // Driver code
    public static void main(String []args)
    {
        int n = 5;
        int high[] = {3, 6, 8, 7, 6};
        int low[] = {1, 5, 4, 5, 3};
        System.out.println( maxTasks(high, low, n));
    }
}
 
// This code is contributed by Ita_c.


Python3
# A naive recursive Python3 program to
# find maximum tasks.
 
# Returns maximum amount of task
# that can be done till day n
def maxTasks(high, low, n) :
     
    # If n is less than equal to 0,
    # then no solution exists
    if (n <= 0) :
        return 0
 
    # Determines which task to choose on day n,
    # then returns the maximum till that day
    return max(high[n - 1] + maxTasks(high, low, (n - 2)),
               low[n - 1] + maxTasks(high, low, (n - 1)));
 
# Driver Code
if __name__ == "__main__" :
 
    n = 5;
    high = [3, 6, 8, 7, 6]
    low = [1, 5, 4, 5, 3]
    print(maxTasks(high, low, n));
 
# This code is contributed by Ryuga


C#
// A naive recursive C# program
// to find maximum tasks.
using System;
 
class GFG
{
     
    // Returns maximum amount of task
    // that can be done till day n
    static int maxTasks(int[] high,
                    int[] low, int n)
    {
         
        // If n is less than equal to 0,
        // then no solution exists
        if (n <= 0)
            return 0;
 
        /* Determines which task to choose on day n,
            then returns the maximum till that day */
        return Math.Max(high[n - 1] +
            maxTasks(high, low, (n - 2)), low[n - 1] +
            maxTasks(high, low, (n - 1)));
    }
 
    // Driver code
    public static void Main()
    {
        int n = 5;
        int[] high = {3, 6, 8, 7, 6};
        int[] low = {1, 5, 4, 5, 3};
        Console.Write( maxTasks(high, low, n));
    }
}
 
// This code is contributed by Ita_c.


PHP


Javascript


C++
// A DP based C++ program to find maximum tasks.
#include
 
// Returns the maximum among the 2 numbers
int max(int x, int y)
{
    return (x > y ? x : y);
}
 
// Returns maximum amount of task that can be
// done till day n
int maxTasks(int high[], int low[], int n)
{
    // An array task_dp that stores the maximum
    // task done
    int task_dp[n+1];
 
    // If n = 0, no solution exists
    task_dp[0] = 0;
 
    // If n = 1, high effort task on that day will
    // be the solution
    task_dp[1] = high[0];
 
    // Fill the entire array determining which
    // task to choose on day i
    for (int i = 2; i <= n; i++)
        task_dp[i] = max(high[i-1] + task_dp[i-2],
                         low[i-1] + task_dp[i-1]);
    return task_dp[n];
}
 
// Driver program to test above function
int main()
{
    int n = 5;
    int high[] = {3, 6, 8, 7, 6};
    int low[] = {1, 5, 4, 5, 3};
    printf("%dn", maxTasks(high, low, n));
    return 0;
}


Java
// A DP based Java program to find maximum tasks.
class GFG
{
     
// Returns the maximum among the 2 numbers
static int max(int x, int y)
{
    return (x > y ? x : y);
}
 
// Returns maximum amount of task that can be
// done till day n
static int maxTasks(int []high, int []low, int n)
{
    // An array task_dp that stores the maximum
    // task done
    int[] task_dp = new int[n + 1];
 
    // If n = 0, no solution exists
    task_dp[0] = 0;
 
    // If n = 1, high effort task on that day will
    // be the solution
    task_dp[1] = high[0];
 
    // Fill the entire array determining which
    // task to choose on day i
    for (int i = 2; i <= n; i++)
        task_dp[i] = Math.max(high[i - 1] + task_dp[i - 2],
                        low[i - 1] + task_dp[i - 1]);
    return task_dp[n];
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    int []high = {3, 6, 8, 7, 6};
    int []low = {1, 5, 4, 5, 3};
    System.out.println(maxTasks(high, low, n));
}
}
 
// This code is contributed by Code_Mech.


Python3
# A DP based Python3 program to find maximum tasks.
# Returns the maximum among the 2 numbers
def max1(x, y):
 
    return x if(x > y) else y;
 
# Returns maximum amount of task
# that can be done till day n
def maxTasks(high, low, n):
 
    # An array task_dp that stores
    # the maximum task done
    task_dp = [0] * (n + 1);
 
    # If n = 0, no solution exists
    task_dp[0] = 0;
 
    # If n = 1, high effort task
    # on that day will be the solution
    task_dp[1] = high[0];
 
    # Fill the entire array determining
    # which task to choose on day i
    for i in range(2, n + 1):
        task_dp[i] = max(high[i - 1] + task_dp[i - 2],
                          low[i - 1] + task_dp[i - 1]);
    return task_dp[n];
 
# Driver code
n = 5;
high = [3, 6, 8, 7, 6];
low = [1, 5, 4, 5, 3];
print(maxTasks(high, low, n));
 
# This code is contributed by mits


C#
// A DP based C# program to find maximum tasks.
using System;
 
class GFG
{
     
// Returns the maximum among the 2 numbers
static int max(int x, int y)
{
    return (x > y ? x : y);
}
 
// Returns maximum amount of task that can be
// done till day n
static int maxTasks(int []high, int []low, int n)
{
    // An array task_dp that stores the maximum
    // task done
    int[] task_dp = new int[n + 1];
 
    // If n = 0, no solution exists
    task_dp[0] = 0;
 
    // If n = 1, high effort task on that day will
    // be the solution
    task_dp[1] = high[0];
 
    // Fill the entire array determining which
    // task to choose on day i
    for (int i = 2; i <= n; i++)
        task_dp[i] = max(high[i - 1] + task_dp[i - 2],
                        low[i - 1] + task_dp[i - 1]);
    return task_dp[n];
}
 
// Driver program to test above function
static void Main()
{
    int n = 5;
    int []high = {3, 6, 8, 7, 6};
    int []low = {1, 5, 4, 5, 3};
    Console.WriteLine(maxTasks(high, low, n));
}
}
 
// This code is contributed by mits


PHP
 $y ? $x : $y);
}
 
// Returns maximum amount of task that can be
// done till day n
function maxTasks($high, $low, $n)
{
    // An array task_dp that stores the maximum
    // task done
    $task_dp = array($n + 1);
 
    // If n = 0, no solution exists
    $task_dp[0] = 0;
 
    // If n = 1, high effort task on that day will
    // be the solution
    $task_dp[1] = $high[0];
 
    // Fill the entire array determining which
    // task to choose on day i
    for ($i = 2; $i <= $n; $i++)
        $task_dp[$i] = max($high[$i - 1] + $task_dp[$i - 2],
                        $low[$i - 1] + $task_dp[$i - 1]);
    return $task_dp[$n];
}
 
// Driver code
{
    $n = 5;
    $high = array(3, 6, 8, 7, 6);
    $low = array(1, 5, 4, 5, 3);
    echo(maxTasks($high, $low, $n));
}
 
// This code is contributed by Code_Mech.


Javascript


输出 :

20

应该注意的是,上述函数一次又一次地计算相同的子问题。
因此,此问题具有重叠子问题属性。因此,“繁重的工作量与繁重的工作量”任务问题具有动态编程问题的两个属性。
动态编程解决方案

C++

// A DP based C++ program to find maximum tasks.
#include
 
// Returns the maximum among the 2 numbers
int max(int x, int y)
{
    return (x > y ? x : y);
}
 
// Returns maximum amount of task that can be
// done till day n
int maxTasks(int high[], int low[], int n)
{
    // An array task_dp that stores the maximum
    // task done
    int task_dp[n+1];
 
    // If n = 0, no solution exists
    task_dp[0] = 0;
 
    // If n = 1, high effort task on that day will
    // be the solution
    task_dp[1] = high[0];
 
    // Fill the entire array determining which
    // task to choose on day i
    for (int i = 2; i <= n; i++)
        task_dp[i] = max(high[i-1] + task_dp[i-2],
                         low[i-1] + task_dp[i-1]);
    return task_dp[n];
}
 
// Driver program to test above function
int main()
{
    int n = 5;
    int high[] = {3, 6, 8, 7, 6};
    int low[] = {1, 5, 4, 5, 3};
    printf("%dn", maxTasks(high, low, n));
    return 0;
}

Java

// A DP based Java program to find maximum tasks.
class GFG
{
     
// Returns the maximum among the 2 numbers
static int max(int x, int y)
{
    return (x > y ? x : y);
}
 
// Returns maximum amount of task that can be
// done till day n
static int maxTasks(int []high, int []low, int n)
{
    // An array task_dp that stores the maximum
    // task done
    int[] task_dp = new int[n + 1];
 
    // If n = 0, no solution exists
    task_dp[0] = 0;
 
    // If n = 1, high effort task on that day will
    // be the solution
    task_dp[1] = high[0];
 
    // Fill the entire array determining which
    // task to choose on day i
    for (int i = 2; i <= n; i++)
        task_dp[i] = Math.max(high[i - 1] + task_dp[i - 2],
                        low[i - 1] + task_dp[i - 1]);
    return task_dp[n];
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    int []high = {3, 6, 8, 7, 6};
    int []low = {1, 5, 4, 5, 3};
    System.out.println(maxTasks(high, low, n));
}
}
 
// This code is contributed by Code_Mech.

Python3

# A DP based Python3 program to find maximum tasks.
# Returns the maximum among the 2 numbers
def max1(x, y):
 
    return x if(x > y) else y;
 
# Returns maximum amount of task
# that can be done till day n
def maxTasks(high, low, n):
 
    # An array task_dp that stores
    # the maximum task done
    task_dp = [0] * (n + 1);
 
    # If n = 0, no solution exists
    task_dp[0] = 0;
 
    # If n = 1, high effort task
    # on that day will be the solution
    task_dp[1] = high[0];
 
    # Fill the entire array determining
    # which task to choose on day i
    for i in range(2, n + 1):
        task_dp[i] = max(high[i - 1] + task_dp[i - 2],
                          low[i - 1] + task_dp[i - 1]);
    return task_dp[n];
 
# Driver code
n = 5;
high = [3, 6, 8, 7, 6];
low = [1, 5, 4, 5, 3];
print(maxTasks(high, low, n));
 
# This code is contributed by mits

C#

// A DP based C# program to find maximum tasks.
using System;
 
class GFG
{
     
// Returns the maximum among the 2 numbers
static int max(int x, int y)
{
    return (x > y ? x : y);
}
 
// Returns maximum amount of task that can be
// done till day n
static int maxTasks(int []high, int []low, int n)
{
    // An array task_dp that stores the maximum
    // task done
    int[] task_dp = new int[n + 1];
 
    // If n = 0, no solution exists
    task_dp[0] = 0;
 
    // If n = 1, high effort task on that day will
    // be the solution
    task_dp[1] = high[0];
 
    // Fill the entire array determining which
    // task to choose on day i
    for (int i = 2; i <= n; i++)
        task_dp[i] = max(high[i - 1] + task_dp[i - 2],
                        low[i - 1] + task_dp[i - 1]);
    return task_dp[n];
}
 
// Driver program to test above function
static void Main()
{
    int n = 5;
    int []high = {3, 6, 8, 7, 6};
    int []low = {1, 5, 4, 5, 3};
    Console.WriteLine(maxTasks(high, low, n));
}
}
 
// This code is contributed by mits

的PHP

 $y ? $x : $y);
}
 
// Returns maximum amount of task that can be
// done till day n
function maxTasks($high, $low, $n)
{
    // An array task_dp that stores the maximum
    // task done
    $task_dp = array($n + 1);
 
    // If n = 0, no solution exists
    $task_dp[0] = 0;
 
    // If n = 1, high effort task on that day will
    // be the solution
    $task_dp[1] = $high[0];
 
    // Fill the entire array determining which
    // task to choose on day i
    for ($i = 2; $i <= $n; $i++)
        $task_dp[$i] = max($high[$i - 1] + $task_dp[$i - 2],
                        $low[$i - 1] + $task_dp[$i - 1]);
    return $task_dp[$n];
}
 
// Driver code
{
    $n = 5;
    $high = array(3, 6, 8, 7, 6);
    $low = array(1, 5, 4, 5, 3);
    echo(maxTasks($high, $low, $n));
}
 
// This code is contributed by Code_Mech.

Java脚本


输出:

20