java 工作排序(Job Sequencing Problem)

        给定一个作业数组,其中每个作业都有一个截止期限,如果作业在截止期限之前完成,则可获得相关利润。此外,每个作业都占用一个单位时间,因此任何作业的最小可能截止期限都是 1。如果一次只能安排一项作业,则最大化总利润。

例子: 
输入:四个工作,截止日期和利润如下
JobID 截止期限 利润
  一 4 20   
  二 1 10
  三 1 40  
  四 1 30

输出:以下是工作利润最大的序列:c、a   

输入:  五项工作,截止日期和利润如下

JobID 截止期限 利润

  a 2 100
  b 1 19
  c 2 27
  d 1 25
  e 3 15

输出:以下是工作利润最大的序列:c,a,e

朴素方法:要解决问题,请遵循以下想法:

生成给定作业集的所有子集,并检查各个子集是否可行。跟踪所有可行子集中的最大利润。

作业排序问题的贪婪方法:
贪婪地首先选择利润最高的工作,方法是按利润降序对工作进行排序。这将有助于最大化总利润,因为为每个时间段选择利润最高的工作最终将最大化总利润

按照给定的步骤解决问题:

按利润的降序对所有工作进行排序。 
按利润递减的顺序对工作进行迭代。对于每项工作,执行以下操作: 
找到一个时间段 i,使得时间段为空、i < 截止时间且 i 最大。将作业放入 
此时间段并将此时间段标记为已填充。 
如果不存在这样的 i,则忽略该工作。 
下面是上述方法的实现: 

// Java code for the above approach 
 
import java.util.*;
 
class Job {
   
    // Each job has a unique-id,profit and deadline
    char id;
    int deadline, profit;
 
    // Constructors
    public Job() {}
 
    public Job(char id, int deadline, int profit)
    {
        this.id = id;
        this.deadline = deadline;
        this.profit = profit;
    }
 
    // Function to schedule the jobs take 2 arguments
    // arraylist and no of jobs to schedule
    void printJobScheduling(ArrayList<Job> arr, int t)
    {
        // Length of array
        int n = arr.size();
       
        // Sort all jobs according to decreasing order of
        // profit
        Collections.sort(arr,
                         (a, b) -> b.profit - a.profit);
 
        // To keep track of free time slots
        boolean result[] = new boolean[t];
 
        // To store result (Sequence of jobs)
        char job[] = new char[t];
 
        // Iterate through all given jobs
        for (int i = 0; i < n; i++) {
            // Find a free slot for this job (Note that we
            // start from the last possible slot)
            for (int j
                 = Math.min(t - 1, arr.get(i).deadline - 1);
                 j >= 0; j--) {
                // Free slot found
                if (result[j] == false) {
                    result[j] = true;
                    job[j] = arr.get(i).id;
                    break;
                }
            }
        }
 
        // Print the sequence
        for (char jb : job)
            System.out.print(jb + " ");
        System.out.println();
    }
 
    // Driver's code
    public static void main(String args[])
    {
        ArrayList<Job> arr = new ArrayList<Job>();
        arr.add(new Job('a', 2, 100));
        arr.add(new Job('b', 1, 19));
        arr.add(new Job('c', 2, 27));
        arr.add(new Job('d', 1, 25));
        arr.add(new Job('e', 3, 15));
 
        System.out.println(
            "Following is maximum profit sequence of jobs");
 
        Job job = new Job();
 
        // Function call
        job.printJobScheduling(arr, 3);
    }
}
 
// This code is contributed by Aditya Kumar (adityakumar129)  

输出
以下是工作的最大利润序列

c a e 

计算机辅助设计
时间复杂度: O(N 2 )
辅助空间: O(N)

使用优先级队列(最大堆)的作业排序问题:
按截止日期的升序对作业进行排序,然后从末尾开始迭代,计算每两个连续截止日期之间的可用时隙。当空时隙可用且堆不为空时,将作业的利润包含在最大堆的根部,因为这有助于为每组可用时隙选择利润最大的作业。

下面是上述方法的实现:

// Java implementation of above approach
 
// Program to find the maximum profit
// job sequence from a given array
// of jobs with deadlines and profits
import java.util.*;
 
public class GFG {
 
    // a class to represent job
    static class Job {
        char job_id;
        int deadline;
        int profit;
        Job(char job_id, int deadline, int profit)
        {
            this.deadline = deadline;
            this.job_id = job_id;
            this.profit = profit;
        }
    }
 
    static void printJobScheduling(ArrayList<Job> arr)
    {
        int n = arr.size();
 
        // sorting the array on the
        // basis of their deadlines
        Collections.sort(arr, (a, b) -> {
            return a.deadline - b.deadline;
        });
 
        // initialise the result array and maxHeap
        ArrayList<Job> result = new ArrayList<>();
        PriorityQueue<Job> maxHeap = new PriorityQueue<>(
            (a, b) -> { return b.profit - a.profit; });
 
        // starting the iteration from the end
        for (int i = n - 1; i > -1; i--) {
            int slot_available;
           
            // calculate slots between two deadlines
            if (i == 0) {
                slot_available = arr.get(i).deadline;
            }
            else {
                slot_available = arr.get(i).deadline
                                 - arr.get(i - 1).deadline;
            }
 
            // include the profit of job(as priority),
            // deadline and job_id in maxHeap
            maxHeap.add(arr.get(i));
 
            while (slot_available > 0
                   && maxHeap.size() > 0) {
 
                // get the job with max_profit
                Job job = maxHeap.remove();
 
                // reduce the slots
                slot_available--;
 
                // include the job in the result array
                result.add(job);
            }
        }
 
        // jobs included might be shuffled
        // sort the result array by their deadlines
        Collections.sort(result, (a, b) -> {
            return a.deadline - b.deadline;
        });
       
        for (Job job : result) {
            System.out.print(job.job_id + " ");
        }
       
        System.out.println();
    }
 
    // Driver's Code
    public static void main(String[] args)
    {
        ArrayList<Job> arr = new ArrayList<Job>();
 
        arr.add(new Job('a', 2, 100));
        arr.add(new Job('b', 1, 19));
        arr.add(new Job('c', 2, 27));
        arr.add(new Job('d', 1, 25));
        arr.add(new Job('e', 3, 15));
       
        System.out.println("Following is maximum "
                           + "profit sequence of jobs");
 
        // Function call
        printJobScheduling(arr);
    }
}
 
// This code is contributed by Karandeep Singh 

输出
以下是作业的最大利润序列

a c e 

时间复杂度: O(N log N)
辅助空间: O(N) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值