从N个项目中选择M个项目,以便完成这些M个项目的任务所需的时间最短。

huangapple go评论91阅读模式
英文:

Select M items from N items such that completing these M item's tasks take the minimum time

问题

我正在尝试解决以下问题:
给你 N 个物品。每个物品包含三个任务 A、B 和 C。完成任务 A 所需的时间是 TA,任务 B 所需的时间是 TB,任务 C 所需的时间是 TC。现在,我们必须选择 M 个物品,使得完成这 M 个物品的任务所需的时间最少。以下是规则:

  1. 所选的 M 个物品同时操作,即这 M 个物品的任务同时进行
  2. 除非所有 M 个物品的任务 A 完成,否则不能开始所选物品的任务 B
  3. 除非所有 M 个物品的任务 B 完成,否则不能开始所选物品的任务 C

例如:

假设 N = 3,M = 2(表示我们必须从总共 3 个物品中选择 M 个物品)
         任务:A  B  C
       物品 1: 1  2  2
       物品 2: 3  4  1
       物品 3: 3  1  2

如果我们选择物品 1 和物品 3,物品 1 和物品 3 的任务 A 在 3 个单位时间后完成(物品 1 等待物品 3 完成),然后在接下来的 2 个单位时间内完成了任务 B。类似地,任务 C 在 2 个单位时间后完成。因此,总时间为 7(这是我们可以找到的最小可能组合)

我已经尝试思考动态规划的解决方案来解决这个问题。但是我无法得到一个确切的解决方案。是否有人可以通过给出一个有效的问题解决方案来帮助我。

PS:请不要编写代码。我只是在寻找解决思路。

提前谢谢你的帮助。

英文:

I'm trying to solve the following problem:
You are given N items. Each item contains three tasks A, B and C. The time required to complete task A is TA, task B is TB, task C is TC. Now, we must select M items such that completing these M item's tasks take the minimum time. And here are the rules:

  1. All selected M items are operated simultaneously, i.e. tasks of all the M items are operated at the same moment
  2. The task B of any selected items cannot be started unless task A of all M items is complete
  3. The task C of any selected items cannot be started unless task B of all M items is complete

For example:

if say N = 3 and M = 2 (it means we must select M items out of 3 items in total)
         Tasks: A  B  C
       item 1 : 1  2  2
       item 2 : 3  4  1
       item 3 : 3  1  2

If we select item 1 and item 3, task A of both items gets completed after 3 units(item 1 waits for item 3 to finish), then task B of both the items gets completed after the next 2 units time. Similarly, task C gets completed after 2 units time. Hence the total time is 7(which is the minimum possible combination we can find)

I have tried thinking of a Dynammic programming solution to the problem. But am unable to get a concrete solution to the problem. Could anyone help me out by giving a valid solution to the problem.

PS: Please don't write the code. I am just looking for the logic here.

Thank you in advance.

答案1

得分: 3

贪婪法解决方案(权重计算 + 截止时间排序)

这是解决这个问题的贪婪方法,希望对您有所帮助。祝您好运!

由于每个项目中的每个任务需要时间T来完成,我们可以将这些视为这些任务(A、B和C)的“截止时间”。我们可以将这些截止时间想象成数组/时间轴中的“插槽”。

为了可视化这些截止时间,请考虑以下示例:

项目2的任务A;

0__A__1__A__2__A__3

项目1的任务C;

0__C__1__C__2

现在考虑一下:我们手中有K个“插槽”0__1__2__...__K,问题要求我们尽可能少地使用插槽。

为了更好地理解问题,让我们从您的解释中选择项目1和项目3的示例,我们的插槽将变成这种形式;

项目1 + 项目3 "截止时间插槽占用"

0_A_1_A_2_A_3_B_4_B_5_C_6_C_7

前三个插槽被占用,因为项目3的任务A所需的时间比项目1长3个单位。只有在完成了这个“较长”的任务A后,任务B才能开始,因此从插槽编号3开始。

因此,问题变成了:用最少的插槽填满我们的插槽。因此,我们将采取贪婪的方法来解决这个问题。

  • 找到我们想要从N个项目中选择的M个项目的各自“截止时间插槽”

在您提供的示例中;

对于项目1;

0_A_1_B_2_B_3_C_4_C_5

占用了5个插槽

对于项目2;
占用了8个插槽

对于项目3;
占用了6个插槽

对于项目X;
占用了P个插槽,依此类推......

在了解每个项目的任务所需插槽数量之后,我们将检查M个减法,作为从N个项目中选择的项目任务时间的组合,以获得最小可能的数值。

示例;
对于选择M个项目时,当M=2;

项目1-项目2 = 5;

项目1-项目3 = 3;

项目2-项目3 = 4;

**编辑;项目1 - 项目2 对应于所选项目数量的组合中的减法的绝对值;例如,如果M=2;|a1-a2| + |b1-b2| + |c1-c2| ...

因此,对于M=2的选择,我们选择了结果为3的最小结果,这导致我们选择了项目1和项目3作为解决方案。

这个数字将给我们使用的最小插槽数。因此,这将引导我们找到解决方案。

英文:

Solution via Greedy Method (Weight Calculation + Deadline Sequencing)

Here is a greedy approach for the solution of this problem, I hope it helps. Good luck!

Since each task within an item takes time T to complete, we can think of these as "Deadlines" for these tasks (A, B and C) . And we can visualise these deadlines as if they were "slots" within an array/train of slots.

In order to visualize these deadlines consider these examples;

Task A of item 2;

0__A__1__A__2__A__3

Task C of item 1;

0__C__1__C__2

Let's consider this now; We have an K amount of "slots" within our hand 0__1__2__ ... __K and the problem asks us to spend minimum amount of slots as possible.

Another example from your explanation for better visualization of the problem, when you chose the item1 and item3 our slot took this form;

item1 + item3 "deadline slot occupation"

0_A_1_A_2_A_3_B_4_B_5_C_6_C_7

First three slots are occupied because item3's task A takes 3 units longer than item1. Task B can only start when this "longer" task A is done therefore starts from the slot number 3.

Therefore the problem becomes this; Fill our slot with the MINIMUM amount of slots spent. Therefore we will take a greedy approach into this problem.

  • Find individual "deadline slots" for M number of items we want to choose from N items

In the example you have provided;

For item1;

0_A_1_B_2_B_3_C_4_C_5

5 slots occupied

For item2;
8 slots occupied

For item3;
6 slots occupied

For itemX;
P slots occupied and so on....

After knowing the number of slots each item demands on task times
we will check M number of Subtractions as combinations of items within N number of item task times to get the SMALLEST number possible.

Example;
For M items to choose when M=2;

Item1-Item2 = 5;

Item1-Item3 = 3;

Item2-Item3 = 4;

**edit; Item1 - Item2 corresponds to absolute value of subtractions within the combinations of chosen number of items; such as if M=2; |a1-a2| + |b1-b2| + |c1-c2| ...

Therefore for M=2 choices we take the minimum result of 3 which leads us to choosing Item1 and Item3 as solution.

This number will give us the minimum number of slots used. Hence, this leads us to the solution.

答案2

得分: 0

基于优先队列或排序的解决方案

假设我们选择了一些M个元素,它们的最大值分别是A、B、C的最大值,那么我们可以确定数组中一定有一些元素满足A = Amax的条件,B和C同理,因此我们可以说A、B、C最多有N个不同的值。因此,对于(Amax, Bmax, Cmax)的可能组合最多为N^3。然后,我们可以检查数组中是否至少有M个元素满足每个组合的约束条件,并使用ans = max(ans, Amax + Bmax + Cmax)更新答案值。这种方法的时间复杂度为O(N^4)。

但是,我们可以通过对Cmax级别使用排序或优先队列将时间复杂度减少到O(N^3logN)。基本上,找到所有满足Amax和Bmax约束的元素,并存储所有这些元素的C值,然后在Cmax的数组中找到第M小的值。

英文:

Solution using priority queue or sorting

let's say we choose some M elements and their maximum values of A, B, C are Amax, Bmax and Cmax respectively, then we are sure that there must be some element in array with A = Amax similarly for B and C, so we can say that there are at maximum N different values for A, B, C. Hence possible combination for (Amax, Bmax, Cmax) are at most N^3. Then we can check if there are at least M elements in the array satisfying this constraint for each combination and update the answer value using ans = max(ans, Amax + Bmax + Cmax). This approach takes O(N^4) time.

But we can reduce the time complexity to O(N^3logN) using sorting or priority queue for Cmax level. Basically, find all elements satisfying Amax and Bmax constraint and store C values for all such elements then find Mth smallest value in array for Cmax.

huangapple
  • 本文由 发表于 2020年8月30日 13:39:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63654327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定