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

📅  最后修改于: 2021-05-17 18:44:01             🧑  作者: 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)