Deer bounce on a line segment [0,T], each with strength; a statue at x falls when the net force of deer that have reached it exceeds W. Maximize the fall time over x.
Hard9MathSimulationPrefix sumSortingNo attempts yetTime limit3sMemory limit128 MBAfter a long undergraduate career, Taekhee left the campus. Dohyun decided to put up a statue in his honor and picked the ground for it. The ground forms a number line, so the statue goes on a single point of that line, and the point does not have to be an integer coordinate.
While the statue was being finished, N deer came down from a nearby hill. The deer run along the same number line at a constant speed and never look back. A deer that reaches the left end turns right, a deer that reaches the right end turns left, and no deer leaves the ground. Dohyun decided to put up the statue anyway. He wants it to stand as long as possible before it falls.
Dohyun found out the following.
The moment the statue falls depends on where it stands. Find how long the statue lasts when the position is chosen as well as possible.
The first line contains three integers N, T, W: the number of deer, the largest coordinate of the number line, and the durability of the statue. (1≤N≤7000, 1≤T≤1018, 0≤W≤109)
The smallest coordinate of the number line is always 0.
The i-th of the next N lines contains three integers Pi, Di, Si: the starting position, the initial facing direction, and the strength of deer i. Deer i faces left if Di=0 and right if Di=1. (0≤Pi≤T, Di=0 or Di=1, 0≤Si≤109)
Every deer runs at the same speed of distance 1 per second.
Fix a position x for the statue and the moment the statue falls is fixed as well. Call that moment f(x). It is the first moment the force on the statue exceeds W, and f(x)=∞ when no such moment exists.
If f(x)=∞ for at least one position x, print inf on the first line.
Otherwise print on the first line the supremum, that is the least upper bound, of f(x) over all real positions x with 0≤x≤T. Moving the statue a little can bring f(x) arbitrarily close to a value that no position reaches exactly. That value is still the answer.
The answer is always a multiple of 0.5, so print it with exactly one digit after the decimal point. Print 9.5 for an answer of nine and a half, and 10.0 for an answer of ten.
In the first example, put the statue at 6.5. At time 2.5 deer 3 and deer 4 arrive together and leave a force of 4 from the right to the left. At time 5.5 deer 2 arrives and the force drops to 0. At time 9.5 deer 1 arrives, the force becomes 20, and the statue falls. No position lasts longer.
In the second example, put the statue at 10. At time 3 deer 1 and deer 2 arrive together and leave a force of 4 from the right to the left. At time 7 deer 3 arrives from the left and deer 4 arrives from the right, so the force becomes 21−(4+7)=10 from the left to the right. The durability is 10, so the statue does not fall, and no deer is left to arrive.
In the third example every deer has strength 3 and the durability is 2, so the statue falls the moment any single deer reaches it. For a position x with 1<x<3, the first deer to arrive comes at time min(x+1,3−x), and this value approaches 2 as x approaches 1. At exactly x=1, deer 2 stands on the statue and pushes it right away, so the statue falls at time 0. No position lasts a full 2 seconds, and the answer is 2.0.