Possible arrangement of persons waiting to sit in a hall
Last Updated :
21 Jun, 2021
Given an integer N, a binary string S and an array W[]. S denotes the sequence of N * 2 persons entering the hall, where 0 denotes a boy and 1 denotes a girl. W[] denotes the width of seats in each row, where each row consists of exactly 2 seats. The task is to find a possible arrangement of the persons entering the hall such that following conditions are satisfied:
- A boy chooses a row in which both seats are empty. Among all such rows, he chooses the one with the minimum width.
- A girl chooses a row in which one seat is occupied by a boy. Among all such rows, she chooses the one with the maximum width.
Examples:
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
Naive Approach: The simplest approach is to take an array of pairs of size N and mark it occupied on every boy’s entry into the bus if both pair values are marked vacant and have the minimum width. Similarly, mark occupied on girl’s entry only when a single value is marked occupied and having maximum width.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use a priority queue. Follow the steps below too solve the problem:
- Sort the given array arr[] of the width of seats in ascending order.
- Create a priority queue and keep an index variable to keep track of the number of rows occupied by a boy in arr[].
- Create a vector ans[] to store the result.
- Iterate through each character of the string s and if s[i] is 0, then push the index of arr[ind] in the vector and push arr[ind] into the queue.
- Otherwise, pop the top element of the queue, and then push the index of this element in the vector.
- After the above steps. print the value stored in ans[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findTheOrder( int arr[], string s,
int N)
{
vector< int > ans;
pair< int , int > A[N];
for ( int i = 0; i < N; i++)
A[i] = { arr[i], i + 1 };
sort(A, A + N);
priority_queue<pair< int , int > > q;
int index = 0;
for ( int i = 0; i < 2 * N; i++) {
if (s[i] == '0' ) {
ans.push_back(A[index].second);
q.push(A[index]);
index++;
}
else {
ans.push_back(q.top().second);
q.pop();
}
}
for ( auto i : ans) {
cout << i << " " ;
}
}
int main()
{
int N = 3;
int arr[] = { 2, 1, 3 };
string s = "001011" ;
findTheOrder(arr, s, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Pair
{
int first, second;
Pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
class GFG{
static void findTheOrder( int arr[], String s,
int N)
{
List<Integer> ans = new ArrayList<Integer>();
List<Pair> A = new ArrayList<Pair>();
for ( int i = 0 ; i < N; i++)
{
Pair p = new Pair(arr[i], i + 1 );
A.add(p);
}
Collections.sort(A, (p1, p2) -> p1.first -
p2.first);
PriorityQueue<Pair> q = new PriorityQueue<Pair>(
N, (p1, p2) -> p2.first - p1.first);
int index = 0 ;
for ( int i = 0 ; i < 2 * N; i++)
{
if (s.charAt(i) == '0' )
{
ans.add(A.get(index).second);
q.add(A.get(index));
index++;
}
else
{
ans.add(q.peek().second);
q.poll();
}
}
for ( int i : ans)
{
System.out.print(i + " " );
}
}
public static void main(String[] args)
{
int N = 3 ;
int arr[] = { 2 , 1 , 3 };
String s = "001011" ;
findTheOrder(arr, s, N);
}
}
|
Python3
def findTheOrder(arr, s, N):
ans = []
A = [[] for i in range (N)]
for i in range (N):
A[i] = [arr[i], i + 1 ]
A = sorted (A)
q = []
index = 0
for i in range ( 2 * N):
if (s[i] = = '0' ):
ans.append(A[index][ 1 ])
q.append(A[index])
index + = 1
else :
ans.append(q[ - 1 ][ 1 ])
del q[ - 1 ]
q = sorted (q)
for i in ans:
print (i, end = " " )
if __name__ = = '__main__' :
N = 3
arr = [ 2 , 1 , 3 ]
s = "001011"
findTheOrder(arr, s, N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void findTheOrder( int [] arr, string s, int N)
{
List< int > ans = new List< int >();
Tuple< int , int >[] A = new Tuple< int , int >[N];
for ( int i = 0; i < N; i++)
A[i] = new Tuple< int , int >(arr[i], i + 1);
Array.Sort(A);
List<Tuple< int , int >> q = new List<Tuple< int , int >>();
int index = 0;
for ( int i = 0; i < 2 * N; i++)
{
if (s[i] == '0' )
{
ans.Add(A[index].Item2);
q.Add(A[index]);
q.Sort();
q.Reverse();
index++;
}
else
{
ans.Add(q[0].Item2);
q.RemoveAt(0);
}
}
foreach ( int i in ans)
{
Console.Write(i + " " );
}
}
static void Main()
{
int N = 3;
int [] arr = { 2, 1, 3 };
string s = "001011" ;
findTheOrder(arr, s, N);
}
}
|
Javascript
<script>
function findTheOrder(arr, s, N)
{
let ans = [];
let A = [];
for (let i = 0; i < N; i++)
{
let p = [arr[i], i + 1];
A.push(p);
}
A.sort( function (p1, p2){ return p1[0]-p2[0];});
let q = [];
let index = 0;
for (let i = 0; i < 2 * N; i++)
{
if (s[i] == '0')
{
ans.push(A[index][1]);
q.push(A[index]);
index++;
}
else
{
ans.push(q[0][1]);
q.shift();
}
q.sort( function (a,b){ return b[0]-a[0];});
}
for (let i=0;i< ans.length;i++)
{
document.write(ans[i] + " " );
}
}
let N = 3;
let arr = [ 2, 1, 3 ];
let s = "001011" ;
findTheOrder(arr, s, N);
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...