📜  等候坐在大厅的人的可能安排

📅  最后修改于: 2021-09-03 03:46:42             🧑  作者: Mango

给定一个整数N 、一个二进制字符串S和一个数组W[]S表示N*2人进入大厅的顺序,其中0表示男孩, 1表示女孩。 W[]表示每排座位的宽度,其中每排正好由2个座位组成。任务是找到进入大厅的人员的可能安排,以满足以下条件:

  • 一个男孩选择一排两个座位都空着的。在所有这些行中,他选择宽度最小的那一行。
  • 一个女孩选择一排,其中一个座位被一个男孩占据。在所有这些行中,她选择宽度最大的行。

例子:

朴素的方法:最简单的方法是取一组大小为N的对,如果两个对值都标记为空且具有最小宽度,则在每个男孩进入公共汽车时将其标记为已占用。同样,仅当单个值被标记为已占用且具有最大宽度时,才在女孩的条目上标记已占用。

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

高效的方法:为了优化上述方法,想法是使用优先级队列。按照以下步骤也可以解决问题:

  • 按升序对座位宽度的给定数组arr[]进行排序。
  • 创建一个优先级队列并保留一个索引变量来跟踪arr[] 中男孩占用的行数。
  • 创建一个向量ans[]来存储结果。
  • 遍历字符串s 的每个字符,如果s[i]0 ,则将arr[ind]的索引推入 向量并将arr[ind]推入队列。
  • 否则,弹出队列的顶部元素,然后推送该元素在向量中的索引。
  • 经过以上步骤。打印ans[] 中存储的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the arrangement
// of seating
void findTheOrder(int arr[], string s,
                  int N)
{
    // Stores the row in which the
    // ith person sits
    vector ans;
 
    // Stores the width of seats along
    // with their index or row number
    pair A[N];
 
    for (int i = 0; i < N; i++)
        A[i] = { arr[i], i + 1 };
 
    // Sort the array
    sort(A, A + N);
 
    // Store the seats and row for
    // boy's seat
    priority_queue > q;
 
    // Stores the index of row upto
    // which boys have taken seat
    int index = 0;
 
    // Iterate the string
    for (int i = 0; i < 2 * N; i++) {
        if (s[i] == '0') {
 
            // Push the row number at
            // index in vector and heap
            ans.push_back(A[index].second);
            q.push(A[index]);
 
            // Increment the index to let
            // the next boy in the next
            // minimum width vacant row
            index++;
        }
 
        // Otherwise
        else {
 
            // If girl then take top of
            // element of the max heap
            ans.push_back(q.top().second);
 
            // Pop from queue
            q.pop();
        }
    }
 
    // Print the values
    for (auto i : ans) {
        cout << i << " ";
    }
}
 
// Driver Code
int main()
{
    // Given N
    int N = 3;
 
    // Given arr[]
    int arr[] = { 2, 1, 3 };
 
    // Given string
    string s = "001011";
 
    // Function Call
    findTheOrder(arr, s, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class Pair
{
    int first, second;
     
    Pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
}
 
class GFG{
 
// Function to find the arrangement
// of seating
static void findTheOrder(int arr[], String s,
                         int N)
{
     
    // Stores the row in which the
    // ith person sits
    List ans = new ArrayList();
 
    // Stores the width of seats along
    // with their index or row number
    List A = new ArrayList();
 
    for(int i = 0; i < N; i++)
    {
        Pair p = new Pair(arr[i], i + 1);
        A.add(p);
    }
 
    // Sort the array
    Collections.sort(A, (p1, p2) -> p1.first -
                                    p2.first);
 
    // Store the seats and row for
    // boy's seat
    PriorityQueue q = new PriorityQueue(
        N, (p1, p2) -> p2.first - p1.first);
 
    // Stores the index of row upto
    // which boys have taken seat
    int index = 0;
 
    // Iterate the string
    for(int i = 0; i < 2 * N; i++)
    {
        if (s.charAt(i) == '0')
        {
             
            // Push the row number at
            // index in vector and heap
            ans.add(A.get(index).second);
            q.add(A.get(index));
 
            // Increment the index to let
            // the next boy in the next
            // minimum width vacant row
            index++;
        }
 
        // Otherwise
        else
        {
             
            // If girl then take top of
            // element of the max heap
            ans.add(q.peek().second);
 
            // Pop from queue
            q.poll();
        }
    }
 
    // Print the values
    for(int i : ans)
    {
        System.out.print(i + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given N
    int N = 3;
 
    // Given arr[]
    int arr[] = { 2, 1, 3 };
 
    // Given string
    String s = "001011";
 
    // Function Call
    findTheOrder(arr, s, N);
}
}
 
// This code is contributed by jithin


Python3
# Python3 program for the above approach
 
# Function to find the arrangement
# of seating
def findTheOrder(arr, s, N):
     
    # Stores the row in which the
    # ith person sits
    ans = []
 
    # Stores the width of seats along
    # with their index or row number
    A = [[] for i in range(N)]
 
    for i in range(N):
        A[i] = [arr[i], i + 1]
 
    # Sort the array
    A = sorted(A)
 
    # Store the seats and row for
    # boy's seat
    q = []
 
    # Stores the index of row upto
    # which boys have taken seat
    index = 0
 
    # Iterate the string
    for i in range(2 * N):
        if (s[i] == '0'):
 
            # Push the row number at
            # index in vector and heap
            ans.append(A[index][1])
            q.append(A[index])
 
            # Increment the index to let
            # the next boy in the next
            # minimum width vacant row
            index += 1
 
        # Otherwise
        else:
 
            # If girl then take top of
            # element of the max heap
            ans.append(q[-1][1])
 
            # Pop from queue
            del q[-1]
             
        q = sorted(q)
 
    # Print the values
    for i in ans:
        print(i, end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    # Given N
    N = 3
 
    # Given arr[]
    arr = [ 2, 1, 3 ]
 
    # Given string
    s = "001011"
 
    # Function Call
    findTheOrder(arr, s, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
     
    // Function to find the arrangement
    // of seating
    static void findTheOrder(int[] arr, string s, int N)
    {
       
        // Stores the row in which the
        // ith person sits
        List ans = new List();
      
        // Stores the width of seats along
        // with their index or row number
        Tuple[] A = new Tuple[N];
      
        for (int i = 0; i < N; i++)
            A[i] = new Tuple(arr[i], i + 1);
      
        // Sort the array
        Array.Sort(A);
      
        // Store the seats and row for
        // boy's seat
        List> q = new List>();
      
        // Stores the index of row upto
        // which boys have taken seat
        int index = 0;
      
        // Iterate the string
        for (int i = 0; i < 2 * N; i++)
        {
            if (s[i] == '0')
            {
      
                // Push the row number at
                // index in vector and heap
                ans.Add(A[index].Item2);
                q.Add(A[index]);
                q.Sort();
                q.Reverse();
      
                // Increment the index to let
                // the next boy in the next
                // minimum width vacant row
                index++;
            }
      
            // Otherwise
            else
            {
      
                // If girl then take top of
                // element of the max heap
                ans.Add(q[0].Item2);
      
                // Pop from queue
                q.RemoveAt(0);
            }
        }
      
        // Print the values
        foreach(int i in ans)
        {
            Console.Write(i + " ");
        }
    }
 
  // Driver code
  static void Main()
  {
    // Given N
    int N = 3;
  
    // Given arr[]
    int[] arr = { 2, 1, 3 };
  
    // Given string
    string s = "001011";
  
    // Function Call
    findTheOrder(arr, s, N);
  }
}
 
// This code is contributed by divyesh072019.


输出:
2 1 1 3 3 2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live