Want to try solving this problem? You can submit your code online if you log in or register.
Input File: aflin.txt
Output File: aflout.txt
Time Limit: 0.2 seconds
It's football season, and the rush is on. You have the unfortunate job of trying to arrange seating in the stadium for the horde of fans phoning in to the ticket office.
Luckily you are only responsible for a single row of seats. For each football fan who phones in with a booking for k people, you have to find a continuous block of k empty seats in the row in which they can sit. To save yourself the hassle of having to decide each time which block of k empty seats to book, you decide upon the following strategy.
Each time a fan phones in with a booking for k people, you find the longest continuous sequence of empty seats in the row (if there is a tie then you choose the leftmost longest sequence). You then book the first k seats in this sequence (and hope that the sequence of empty seats is long enough). These seats are then marked as taken, and you move on to answer the next phone call.
As an example, consider a row of 10 seats numbered 1,2,...,10. Say that seats 3 and 8 have already been taken before you start making bookings. The initial state of the row is illustrated in the following diagram, where the shaded seats have already been taken.
Suppose that the first phone call is for a booking of size 1. The longest continuous sequence of empty seats is from seats 4 to 7, so you place the booking at the beginning of this sequence, claiming seat 4.
Suppose that the next request is for a booking of size 2. The longest continuous sequence of empty seats is now from seats 5 to 7, so the booking is made for seats 5 and 6.
Let the next request be another booking of size 1. There are now two longest continuous sequences of empty seats, each of length two. One is from seats 1 to 2 and the other is from seats 9 to 10. You choose the leftmost of these longest sequences and book seat 1.
And so on. After a few days of carrying out this procedure it is becoming rather tedious, so you decide to write a computer program that can carry it out for you. Grabbing your laptop and an AFL-branded meat pie, you set to work.
The first line of input will contain the single integer n representing the total number of seats in the row (1 <= n <= 30,000). These seats are numbered 1,2,...,n from left to right.
The second line of input will contain the single integer t representing the total number of seats that have already been taken (0 <= t <= n). Following this will be t lines, each containing the number of a seat that has been taken (no two of these seat numbers will be identical).
The next line of input will contain the single integer b representing the total number of bookings that you must make (0 <= b <= n). Following this will be b lines, each describing a single booking. Each of these lines will contain a single integer k representing the number of seats to be booked (1 <= k <= n). The bookings will be presented in order from the first phone call to the last.
For each booking, your program should write a single line of output. This line should contain a single integer describing the leftmost seat that was booked.
You may assume that it is possible to successfully make all of the requested bookings using the strategy described above.
The following sample data corresponds to the example discussed earlier.
10 2 3 8 3 1 2 1
4 5 1
For each input file, let b be the total number of bookings. If your program correctly places r of these bookings, it shall be awarded r/b of the available marks for that input file.
© Australian Mathematics Trust 2001-2023
Page generated: 25 March 2023, 6:24pm AEDT