给定一个整数N ,一个二进制字符串S和一个数组W [] 。 S表示N * 2人进入大厅的顺序,其中0表示男孩,而1表示女孩。 W []表示每排座位的宽度,其中每排正好由2个座位组成。任务是找到进入大厅的人员的可能安排,以便满足以下条件:
- 一个男孩选择两个座位都空着的行。在所有此类行中,他选择宽度最小的行。
- 一个女孩选择一个男孩占据一个座位的行。在所有此类行中,她选择宽度最大的行。
例子:
Input: N = 3, W[] = {2, 1, 3}, S = “001011”
Output: 2 1 1 3 3 2
Explanation:
People enter the hall in the following way:
Person 1: First person is a boy, and all the seats are vacant at this time. So, he can select any row and take a seat. Among all rows, the one with minimum width of seat is 2nd. So, the first person sits on a seat in the 2nd row.
Person 2: Second person is also a boy, and now 2 seats are vacant with width of seats 2 & 3. So, he takes the seat in row 1 as it has minimum width of seats.
Person 3: Third one is a girl, and she can sit in a row in which one seat is already occupied, i.e, row 1 and row 2. Among these rows, the girl chooses to sit in the row with maximum width, so she sits in row 1.
Person 4: Fourth one is a boy, and he can sit only in row 3, as only 3rd row is vacant now.
Person 5: Fifth one is a girl, and she can sit in row 2 or row 3, which have one of the seats occupied. So she chooses row 3 as it has a seat with width more than that in row 2.
Person 6: Finally a girl comes, and she can take a seat only in row 2.
Input: N = 3, W[] = {1, 3, 2}, S = “010011”
Output: 1 1 3 2 2 3
天真的方法:最简单的方法是获取一对大小为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)