#P6968. Unicycle Scheduling
Unicycle Scheduling
Unicycle Scheduling
Mayor Adam East wants to improve the public transport network of Harshel city by introducing a network of stations with unicycles. Any person who owns a special card can go to a station and request a unicycle to ride or drop one.
The procedure is as follows: when a person requests a unicycle, they join a queue. If there is at least one unicycle available at that moment, the person takes one immediately and their wait time is 0. Otherwise, they wait until someone drops a unicycle. When a drop occurs, if there are waiting requests, the drop immediately satisfies the request at the front of the queue. The wait time for that person is calculated as (drop time - request time). Formally, if we denote the wait time for a served request as \(w_i = t_{drop} - t_{request}\), then the total wait time is \(\sum_{i} w_i\). If any request is never served, its wait time is considered infinity and the total wait time is reported as infinity.
You are given the entire schedule of events at the Central Station, where each event is either a request (denoted by 'R') or a drop (denoted by 'D') with an associated time. The events occur in chronological order. You are then given several queries. Each query provides a starting number of unicycles at the station. For each query, compute the total wait time of all passengers. If there is at least one request that is never served by the end of the day, print infinity
.
inputFormat
The input begins with an integer n representing the number of events. Each of the next n lines contains an integer time and a character type (either 'R' for request or 'D' for drop), separated by a space. Events are given in chronological order.
After the events, an integer q is given representing the number of queries. Each of the next q lines contains a non-negative integer denoting the starting number of unicycles at the station.
outputFormat
For each query, output the total wait time on a separate line. If at least one request does not receive a unicycle by the end of the day, output infinity
(without quotes) for that query.
sample
5
1 R
2 D
3 R
4 R
5 D
3
0
1
2
infinity
1
0
</p>