Call a Cab
Time limit5sMemory limit256 MB
Partition the ordered points into the fewest rides where each ride meets one type's minimum total distance and heading range limit.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Segment tree, Sliding window, Prefix sum
- Solved
- No attempts yet
Problem
A group of tourists visits points in a valley, in exactly that order. Transportation comes in several types (car, rickshaw, donkey cart), and every type is available on call only. Deep in the valley the phone works at the points and nowhere else, so the type of transportation can change only at a point.
Segment runs from to . Its length is , and it turns the heading by from the previous segment. The first segment counts as a turn of as well, so the heading of segment is .
A driver of type takes the group from to () in one ride only when both conditions hold.
- Minimum distance: the total distance is at least . A shorter ride is not worth the driver's time.
- Maximum heading range: the largest of the headings minus the smallest one is at most . Drivers find anything other than going straight annoying.
Split the whole itinerary into consecutive groups of segments and call one driver for each group. The same type may be used again, but once a driver has had enough you hail a new one, and that adds another call even when the type is the same. Find the minimum number of calls needed to start at and visit every point up to in the given order.
Input
The first line has the number of transportation types () and the number of points (), separated by a space.
Each of the next lines describes one type with two non-negative integers. The first integer () is the minimum distance that type requires, and the second integer () is the maximum heading range it allows.
Each of the next lines has the length () and the turn () of segment . There are no such lines when .
All angles are given in thousandths of a degree.
Output
Print one line with the minimum number of calls needed to visit through in the given order. Print IMPOSSIBLE when no such split exists. Print 0 when , because no travel is needed.