Photo
Time limit1sMemory limit128 MB
Given intervals each containing exactly one marked point, find the maximum number of marked points, or -1 if no assignment is consistent.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Intervals, Prefix sum
- Solved
- No attempts yet
Problem
Farmer John wants to assemble a panoramic photo of his cows (), conveniently numbered from to . He took photos (); photo covers the contiguous range of cows from to inclusive (). The photos need not cover every cow.
Afterward, Farmer John notices something curious: every photo he took contains exactly one spotted cow. He knows his herd has some spotted cows but has never counted them. Using the photos, determine the maximum possible number of spotted cows the herd could contain. If no way of marking cows as spotted is consistent with all of the photos, output .
Input
- The first line contains two integers and .
- Each of the next lines contains two integers and , the range of cows covered by photo .
Output
- Print a single integer: the maximum possible number of spotted cows, or if no valid assignment exists.
Notes
In the sample there are cows and photos, the first covering cows through . The third photo covers cows and , so exactly one of them must be spotted; marking either one also satisfies the first two photos, giving a maximum of spotted cow.