Seating Arrangement
Practice
4.3 (25 votes)
Data structures
Easy
Hash maps
Hash tables
Heaps
Priority queue
Trees
Problem
58% Success 3807 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

There are \(N\) chairs arranged in a row. \(K\) people come in a line and start occupying the chairs. Each person wants to be as far as possible from every other person. So, every person arriving looks for the largest empty continuous sequence of unoccupied chairs and occupies the middle position. They have a preference indicating whether they would choose the left or the right chair if there are two chairs at the middle to chose from (else the preference does not matter, since there is only 1 chair at the center). If there are multiple largest empty sequences, then the person chooses the sequence which appears first from left side. You are asked to answer \(Q\) queries. Determine which person has occupied the queried position.

Input Format
The first line of every test file are \(2\) integers \(N\) and \(K\).

The next line contains a string \(S\) of length \(K\). The \(i^{th}\) character would be '\(L\)' or '\(R\)' indicating the preference of the \(i^{th}\) person - the left or the right seat respectively. 

Next line contains an integer \(Q\) - the number of queries.

Next \(Q\) lines contain an integer \(Q_{i}\) - the queried position.

Output Format
For each query, output the persons' entry number if it is occupied, else print '\(-1\)' without quotes in a new line.

Constraints

\(1 \leq N \leq 10^{18}\)

\(1 \leq K \leq min(N, \space 10^{5})\)

\(1 \leq Q \leq 10^{5}\)

\(1 \leq Q_{i} \leq N\)

\(length(S) = K\)

Sample Inputs

Input Output
3 3
RRL
3
1
3
2
2
3
1

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
114 votes
Tags:
ApprovedEasyOpenPriority queueTrees
Points:20
29 votes
Tags:
ApprovedData StructuresEasyMapsOpenPriority QueuePriority queueTrees
Points:20
9 votes
Tags:
Data StructuresPriority queueTrees