📌  相关文章
📜  通过将它们分配给两个不同的处理器来使间隔不重叠

📅  最后修改于: 2021-10-26 02:27:54             🧑  作者: Mango

给定一个区间列表interval[] ,其中每个区间包含两个整数LR ,任务是将区间分配给两个不同的处理器,使得它们对于每个处理器没有重叠的区间。要将 interval[i] 分配给第一个处理器,打印“F”,并将其分配给第二个处理器,打印“S”。
注意:如果没有可能的解决方案,则打印 -1。
例子:

方法:想法是使用贪心算法将间隔分配给处理器。
如果处理器的最高结束时间小于或等于某个区间的开始时间,则可以将该区间分配给该处理器。否则,检查另一个处理器。如果无法将任何间隔分配给任何处理器,则没有可能的解决方案。
下面是该方法步骤的说明:

  • 在当前的问题中,我们必须根据间隔的顺序进行打印。因此,要保存间隔的顺序,请将间隔与其索引配对。
  • 按开始时间对间隔进行排序。即L
  • 迭代间隔并将间隔分配给处理器,如下所示:
if (interval[i][0] >= firstProcessorEndTime)
    answer[interval[i]] = "F"
    firstProcessorEndTime = 
        max(firstProcessorEndTime, interval[i][0])
else if (interval[i][0] >= secondProcessorEndTime)
    answer[interval[i]] = "S"
    secondProcessorEndTime = 
        max(secondProcessorEndTime, interval[i][0])
else
    print(-1)

下面是上述方法的实现:

C++
// C++ implementation for intervals
// scheduling to two processors such
// that there are no overlapping intervals
#include 
using namespace std;
 
// Function to assign the intervals
// to two different processors
void assignIntervals(vector > interval, int n)
{
 
    //  Loop to pair the interval
    //  with their indices
    for (int i = 0; i < n; i++)
        interval[i].push_back(i);
 
    // sorting the interval by
    // their start times
    sort(interval.begin(), interval.end());
 
    int firstEndTime = -1;
    int secondEndTime = -1;
    char fin = ' ';
    bool flag = false;
 
    // Loop to iterate over the
    // intervals with their start time
    for (int i = 0; i < n; i++) {
        if (interval[i][0] >= firstEndTime) {
            firstEndTime = interval[i][1];
            interval[i].push_back('S');
        }
        else if (interval[i][0] >= secondEndTime) {
            secondEndTime = interval[i][1];
            interval[i].push_back('F');
        }
        else {
            flag = true;
            break;
        }
    }
 
    // Condition to check if there
    // is a possible solution
    if (flag)
        cout << (-1);
    else {
        vector form(n, ' ');
 
        for (int i = 0; i < n; i++) {
            int indi = interval[i][2];
            form[indi] = interval[i][3];
        }
 
        // form = ''.join(form)
        for (int i = 0; i < form.size(); i++)
            cout << form[i] << ",";
    }
}
 
// Driver Code
int main()
{
 
    vector > intervals
        = { { 360, 480 }, { 420, 540 }, { 600, 660 } };
 
    // Function Call
    assignIntervals(intervals, intervals.size());
    return 0;
}


Python3
# Python implementation for intervals
# scheduling to two processors such
# that there are no overlapping intervals
 
# Function to assign the intervals
# to two different processors
def assignIntervals(interval, n):
     
    # Loop to pair the interval
    # with their indices
    for i in range(n):
        interval[i].append(i)
         
    # sorting the interval by
    # their startb times
    interval.sort(key = lambda x: x[0])
     
    firstEndTime = -1
    secondEndTime = -1
    fin = ''
    flag = False
     
    # Loop to iterate over the
    # intervals with their start time
    for i in range(n):
        if interval[i][0] >= firstEndTime:
            firstEndTime = interval[i][1]
            interval[i].append('S')
        elif interval[i][0] >= secondEndTime:
            secondEndTime = interval[i][1]
            interval[i].append('F')
        else:
            flag = True
            break
     
    # Condition to check if there
    # is a possible solution
    if flag:
        print(-1)
    else:
        form = ['']*n
        for i in range(n):
            indi = interval[i][2]
            form[indi] = interval[i][3]
        # form = ''.join(form)
        print(form, ", ")
 
# Driver Code   
if __name__ == "__main__":
    intervals = [[360, 480], [420, 540], [600, 660]]
     
    # Function Call
    assignIntervals(intervals, len(intervals))


输出:
['S', 'F', 'S'] ,

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。