Temperature
Time limit1sMemory limit128 MB
Each day gives an interval the temperature could lie in; find the longest run of consecutive days that can be assigned non-decreasing values.
- Level
Medium6 of 10
- Topics
- Greedy, Two pointers, Intervals, Implementation
- Solved
- No attempts yet
Problem
The Byteotian Institute of Meteorology (BIM) measures the air temperature every day. The measurement is automatic and the result is printed immediately, but the printer's ink dried out long ago, so nothing was actually recorded. The staff noticed this only once the Byteotian Organisation for Meteorology (BOM) asked for the data.
Luckily, an intern named Byteasar had been diligently writing down the readings of two alcohol thermometers, one mounted on the north outside wall of the building and one on the south. It has long been known that the thermometer on the south wall never reads below the true temperature, while the one on the north wall never reads above it. So although the exact temperature on each day is unknown, the range it lay in is known.
BOM does not need the exact temperatures. It only wants the longest stretch of consecutive days during which the temperature was not dropping (each day at least as warm as the day before), and BIM would like that stretch reported as long as possible. Based on Byteasar's notes, determine the length of the longest such stretch. In other words, if the true temperature of each day may be chosen freely within that day's range, find the maximum number of consecutive days over which the temperatures can be made non-decreasing.
Input
The first line contains a single integer (), the number of days for which Byteasar took notes. Each of the next lines describes one day: the -th of them holds two integers and (), the minimum and maximum possible temperature on day as reported by the two thermometers.
In some of the inputs the temperature always stays between and degrees Celsius ().
Output
Print a single integer: the maximum number of consecutive days during which the temperature in Byteotia could have been non-dropping.
Hint
