Measuring Traffic
Time limit2sMemory limit512 MB
Given sensor readings for each mile as inflow, outflow, or highway flow ranges, find the tightest possible interval for the flow before mile 1 and after mile N.
- Level
Medium5 of 10
- Topics
- Intervals, Simulation, Implementation, Greedy
- Solved
- No attempts yet
Problem
The highway adjacent to Farmer John's farm has recently seen a dramatic increase in traffic, or at least so it seems to Farmer John. To be sure, he wants to measure the traffic flow on the highway with a set of sensors, each capable of measuring the rate of flow of traffic on a segment of road.
Unfortunately, while walking through the barn one day, Farmer John trips and drops his box of sensors into a large vat of milk, after which they no longer work as well. Instead of producing a single exact reading of traffic flow rate, each sensor now outputs a range of possible values. For example, a sensor might output the range , indicating that the rate of traffic flow on a segment of road is no less than 7, and no greater than 13.
The highway runs for miles next to the farm, and traffic on the highway only flows in one direction, from mile 1 towards mile . Farmer John wants to install sensors, one for each 1-mile segment of the highway. In some of these segments, there are on-ramps that allow traffic to enter the highway. In each of these cases, Farmer John installs his sensor on the on-ramp to measure (approximately) the amount of incoming traffic. In some segments there are off-ramps that allow traffic to exit the highway. In each of these cases, Farmer John installs his sensor on the off-ramp. Each segment contains at most one ramp. If there is no on-ramp or off-ramp in a segment of highway, Farmer John installs a sensor on the main highway itself.
Given the readings from Farmer John's sensors, determine the most specific possible ranges that describe the rate of traffic flow initially on the highway prior to mile 1 and for traffic continuing on the highway past mile . These ranges must be consistent with all sensor readings.
Input
The first line of input contains (). Each of the remaining lines describes a 1-mile segment of the road, in order from mile 1 to mile . Each line contains a string that is either "on" (if there is an on-ramp on this segment), "off" (if there is an off-ramp), or "none" (if there is no ramp), followed by two integers in the range , indicating the lower and upper endpoints of the sensor range from this segment. If the segment contains a ramp, the sensor reading is from the ramp. Otherwise, it is from the main highway. At least one of the highway segments is designated as "none".
Output
The first line of output must contain two integers giving the most specific range possible for the rate of traffic flow prior to mile 1. The second line of output must contain two integers giving the most specific range possible for the rate of traffic flow after mile . A valid solution is always guaranteed to exist.
Note
In this example, the combination of readings from segments 2 and 3 tells us that the flow rate through these segments is somewhere in the range , since only this range is consistent with both the readings and . In mile 1, exactly 1 unit of flow enters on an on-ramp, so prior to mile 1, the flow rate must be in the range . In mile 4, between 2 and 3 units exit on an off-ramp, so the range of possible flow rates after this is .