Events Are Approaching (Hard)
Time limit4sMemory limit1024 MB
Given N timed events that forbid positions strictly between A and B at each time T, find the minimum total distance Minjeong moves to dodge them all, or -1 if impossible.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Intervals, Sorting
- Solved
- No attempts yet
Problem
Minjeong is currently at the origin (position 0) of a number line. At every moment, Minjeong can move left or right along the number line at a speed of 1 per second. Of course, Minjeong can also stay still.
Minjeong knows that the following event will occur times.
- If Minjeong is at a position strictly greater than and strictly less than after seconds, Minjeong gets hit by Karina's body bang.
Positions and are safe.
Minjeong wants to move so as to avoid Karina's body bang. Determine whether Minjeong can avoid it by moving, and if so, minimize the total distance moved.
Input
The first line contains the number of events .
Each of the next lines contains , , and for one event, separated by spaces.
Output
If Minjeong cannot avoid Karina's body bang, print -1.
If Minjeong can avoid it, print the minimum distance Minjeong must move.
Constraints
- All given numbers are integers.
- The of each event is distinct.