Ramen Shop Seating
Time limit8sMemory limit512 MB
Simulate a ramen shop with N counters of fixed seats where arriving groups pick the best free block under a preference rule and may leave if they wait too long; report average customer satisfaction.
- Level
Hard8 of 10
- Topics
- Simulation, Implementation, Greedy, Sorting
- Solved
- No attempts yet
Problem
Ron runs a ramen shop.
He recently noticed that customers wait a long time at lunch because the shop runs out of seats. A customer who waits long is less satisfied, and some customers give up and leave. He decided to add seats, and he asked you to write a simulator of customer behavior so he can judge how many seats are enough.
Customers arrive in groups. Each group is described by four values.
- : the time the group arrives at the shop
- : the number of customers in the group
- : how long the group can wait for seats
- : how long the group spends eating
Group arrives at time with customers together. If successive empty seats are available at one counter at that time, the group sits down at once. Otherwise the group waits until such seats become free. If the group cannot sit down at a time that is within of its arrival (the endpoint counts) and strictly before the closing time, the group gives up waiting and leaves. Also, when an earlier group is waiting, a later group cannot take seats until every earlier group has either sat down or left.
The shop has counters numbered to , and counter has seats in one row. A group prefers seats far away from other customers. Precisely, the group chooses seats by the criteria below. Let be the number of successive empty seats to the left of the group after it sits down, and the number to the right. If there is no other customer on the left side, counts as infinity, and if there is no other customer on the right side, counts as infinity.
- Choose seats maximizing .
- If several choices remain, choose seats maximizing .
- If several choices still remain, choose the counter with the smallest number.
- If several choices still remain, choose the leftmost seats.
When several groups finish eating and leave at the same time and some group is waiting, assign seats to the waiting group after all the finishing groups have left.
The satisfaction of one customer is defined as follows.
- A customer of a group that leaves without eating has satisfaction .
- Otherwise the satisfaction is , where is the time group actually waited. This value is between and inclusive.
Compute the average satisfaction over all customers.
Input
The input consists of multiple datasets. Each dataset has the following format.
N M T
C_1 C_2 ... C_N
T_1 P_1 W_1 E_1
T_2 P_2 W_2 E_2
...
T_M P_M W_M E_M
is the number of counters, is the number of groups, and is the closing time. The shop always opens at time . Every input value is an integer.
, , , , , , , and .
A line holding three zeros ends the input. That line is not a dataset.
Output
For each dataset, print the average satisfaction over all customers on one line. Round the value to digits after the decimal point and print exactly digits.