任务分配问题 背包问题_努力与不努力的任务问题

任务分配问题 背包问题

Problem statement:

问题陈述:

A list of n days is given to you and there are three columns. One is the day, the second one is high-effort value, and the another one is low-effort value. At a time you can pick only one either high-effort value or low-effort value. You can pick a high-effort task only when you don't take any of the tasks at the day before. You have to find out the maximum task is possible in n days.

列出了n天的清单,共有三列。 一个是天,第二个是高努力值,另一个是低努力值。 一次只能选择高努力值或低努力值之一。 只有在前一天不执行任何任务时,您才能选择一项繁重的任务。 您必须找出在n天内可能完成的最大任务。

Input:
T Test case
T no. of input string will be given to you.

E.g.
2

Day	High	Low
1	4	3
2	5	6
3	9	4
4	12	5
5	5	4

Day	High	Low
1	2	3
2	4	2
3	7	8
4	9	7
5	10	6

Constrain:
1≤ n ≤50

Output:
Print the maximum task is possible in n days.

Example

T=2

Input:
5
4 5 9 12 5
3 6 4 5 4 

Output:
26 ( 4+ 6+ 12+ 4)

Input:
5
2 4 7 9 10
3 2 8 7 6

Output:
26 (3+2+8+7+6)

Explanation with example:

举例说明:

To solve this task problem using dynamic programming approach, we consider these two conditions,

为了使用动态编程方法解决此任务问题,我们考虑了以下两个条件:

  1. If we will choose the low effort task then we have to add up this value with the value at (i-1)th index of the calculating array.

    如果我们选择省力的任务,则必须将该值与计算数组 (i-1) 索引处的值相加。

  2. If we will choose the high effort task then we have to add up this value with the value at (i-2)th index of the calculating array.

    如果我们将选择繁重的工作,则必须将该值与计算数组 (i-2) 索引处的值相加。

After getting the value we will choose the maximum value between them.

获得值后,我们将在它们之间选择最大值。

Example:

例:

5
4 5 9 12 5
3 6 4 5 4


High-effort vs. Low-effort Task Problem

C++ Implementation:

C ++实现:

#include <iostream>
using namespace std;

int max_amount(int* high, int* low, int n)
{
    int arr[n + 1] = { 0 };
    arr[1] = max(high[0], low[0]);

    for (int i = 2; i <= n; i++) {
        arr[i] = max(arr[i - 1] + low[i - 1], arr[i - 2] + high[i - 1]);
    }

    return arr[n];
}

int main()
{
    int t;

    cout << "Test Case : ";
    cin >> t;

    while (t--) {
        int n;

        cout << "Enter the number of elements : ";
        cin >> n;

        cout << "Enter the High values : ";
        int high[n], low[n];
        for (int i = 0; i < n; i++) {
            cin >> high[i];
        }

        cout << "Enter the low values : ";
        for (int i = 0; i < n; i++) {
            cin >> low[i];
        }

        cout << "Maximum values : " << max_amount(high, low, n) << endl;
    }

    return 0;
}

Output

输出量

Test Case : 2
Enter the number of elements : 5
Enter the High values : 4 5 9 12 5
Enter the low values : 3 6 4 5 4
Maximum values : 26
Enter the number of elements : 5
Enter the High values : 2 4 7 9 10
Enter the low values : 3 2 8 7 6
Maximum values : 26


翻译自: https://www.includehelp.com/icp/high-effort-vs-low-effort-task-problem.aspx

任务分配问题 背包问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值