On Storing Clothes
Time limit1sMemory limit1024 MB
Simulate a circular rail of hooks, depositing batches into the first fitting empty block and withdrawing them by ticket, printing freed hooks.
- Level
Medium4 of 10
- Topics
- Simulation, Array, Implementation
- Solved
- No attempts yet
Problem
In a laundry, clothes are hung on coat hangers placed on hooks that are fixed to a circular rail rotated electrically by a computer. The hooks are numbered so that any garment is easy to find, and the rail turns to bring a chosen hook in front of a fixed mark.
Model the rail as a circular array of hooks whose indices are taken modulo . To store a batch of garments, the operator types . Starting from the hook currently in front of the mark and scanning to the right (increasing indices, modulo ), the computer finds the first block of usable hooks . Hook and hook become separators and are not used for garments; the garments are hung on hooks . The rail then turns so that hook comes in front of the mark, and the operator hands the customer ticket number . Hooks holding a customer's garments stay assigned to that customer, even while the garments are being cleaned.
A separator may be shared: the block's two end hooks may reuse hooks that are already separators. The middle hooks, however, must be empty.
When the customer returns with ticket , the operator types ; the rail turns so that separator hook of that batch is in front of the mark (the rail does not move while the batch is handed back). Every hook that held one of that batch's garments becomes free. A separator hook of the removed batch also becomes free if both of its neighbors are free. (As the note goes: once both neighbors of a separator are empty, that separator may be reused for any purpose.)
At the start the rail is empty and hook is in front of the mark. Only garments that have been deposited can be withdrawn.
Input
The first line contains the number of hooks (). The second line contains the number of command lines that follow. Each of the next lines has one of the two forms:
D n
deposit a batch of garments, or
W k
withdraw the batch with ticket ().
Output
For each command, print the corresponding messages.
On a deposit, if no block of suitable hooks exists, print
No space left, please come back later.
otherwise, when ticket is issued, print
The launderer gives ticket k.
On a withdrawal of ticket , print
The launderer gives back batch k.
and free every hook of that batch. Whenever hooks become free (a contiguous block, indices modulo ), print
i is freed.
for each from to in order.